Skip to content

Commit

Permalink
pulling in Matt Sanford's trends addition
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak committed May 15, 2009
1 parent 0a129bd commit eb2d1b1
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.swp
*.swo
2 changes: 1 addition & 1 deletion Rakefile
@@ -1,7 +1,7 @@
require 'rake'
require 'rake/testtask'

test_files_pattern = 'test/twitter_search_test.rb'
test_files_pattern = 'test/twitter_*_test.rb'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = test_files_pattern
Expand Down
36 changes: 36 additions & 0 deletions lib/trends.rb
@@ -0,0 +1,36 @@
module TwitterSearch
class Trend
VARS = [ :query, :name ]
attr_reader *VARS
attr_reader :exclude_hashtags

def initialize(opts)
@exclude_hashtags = !!opts['exclude_hashtags']
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end
end

class Trends
VARS = [:date]
attr_reader *VARS

include Enumerable

def initialize(opts)
@trends = opts['trends'].first.collect { |each| Trend.new(each) }
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end

def each(&block)
@trends.each(&block)
end

def size
@trends.size
end

def [](index)
@trends[index]
end
end
end
45 changes: 45 additions & 0 deletions lib/tweets.rb
@@ -0,0 +1,45 @@
module TwitterSearch
class Tweet
VARS = [:text, :from_user, :to_user, :to_user_id, :id, :iso_language_code, :from_user_id, :created_at, :profile_image_url, :source ]
attr_reader *VARS
attr_reader :language

def initialize(opts)
@language = opts['iso_language_code']
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end
end

class Tweets
VARS = [:since_id, :max_id, :results_per_page, :page, :query, :next_page]
attr_reader *VARS

include Enumerable

def initialize(opts)
@results = opts['results'].collect { |each| Tweet.new(each) }
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end

def each(&block)
@results.each(&block)
end

def size
@results.size
end

def [](index)
@results[index]
end

def has_next_page?
! @next_page.nil?
end

def get_next_page
client = Client.new
return client.query( CGI.parse( @next_page[1..-1] ) )
end
end
end
68 changes: 22 additions & 46 deletions lib/twitter_search.rb
Expand Up @@ -3,54 +3,14 @@
require 'json'
require 'cgi'

module TwitterSearch

class Tweet
VARS = [:text, :from_user, :to_user, :to_user_id, :id, :iso_language_code, :from_user_id, :created_at, :profile_image_url, :source ]
attr_reader *VARS
attr_reader :language

def initialize(opts)
@language = opts['iso_language_code']
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end
end

class Tweets
VARS = [:since_id, :max_id, :results_per_page, :page, :query, :next_page]
attr_reader *VARS

include Enumerable

def initialize(opts)
@results = opts['results'].collect { |each| Tweet.new(each) }
VARS.each { |each| instance_variable_set "@#{each}", opts[each.to_s] }
end

def each(&block)
@results.each(&block)
end

def size
@results.size
end

def [](index)
@results[index]
end
require File.join(File.dirname(__FILE__), 'tweets')
require File.join(File.dirname(__FILE__), 'trends')

def has_next_page?
! @next_page.nil?
end

def get_next_page
client = Client.new
return client.query( CGI.parse( @next_page[1..-1] ) )
end
end
module TwitterSearch

class Client
TWITTER_API_URL = 'http://search.twitter.com/search.json'
TWITTER_SEARCH_API_URL = 'http://search.twitter.com/search.json'
TWITTER_TRENDS_API_URL = 'http://search.twitter.com/trends/current.json'
TWITTER_API_DEFAULT_TIMEOUT = 5

attr_accessor :agent
Expand All @@ -67,7 +27,7 @@ def headers
end

def query(opts = {})
url = URI.parse(TWITTER_API_URL)
url = URI.parse(TWITTER_SEARCH_API_URL)
url.query = sanitize_query(opts)

req = Net::HTTP::Get.new(url.path)
Expand All @@ -79,6 +39,22 @@ def query(opts = {})
}.body
Tweets.new JSON.parse(json)
end

def trends(opts = {})
url = URI.parse(TWITTER_TRENDS_API_URL)
if opts['exclude_hashtags']
url.query = sanitize_query_hash({ :exclude_hashtags => opts['exclude_hashtags'] })
end

req = Net::HTTP::Get.new(url.path)
http = Net::HTTP.new(url.host, url.port)
http.read_timeout = timeout

json = http.start { |http|
http.get("#{url.path}?#{url.query}", headers)
}.body
Trends.new JSON.parse(json)
end

private

Expand Down
5 changes: 5 additions & 0 deletions test/test_helper.rb
Expand Up @@ -37,4 +37,9 @@ def self.should_return_tweets_in_sets_of(number)
end
end

def read_yaml(opts = {})
return if opts[:file].nil?
YAML.load_file File.join(File.dirname(__FILE__), 'yaml', "#{opts[:file]}.yaml")
end

end
18 changes: 6 additions & 12 deletions test/twitter_search_test.rb
Expand Up @@ -2,12 +2,11 @@

require File.join(File.dirname(__FILE__), 'test_helper')


require 'fake_web'
gem('fakeweb', '>=1.2.0')
require 'fakeweb'

FakeWeb.allow_net_connect = false # an insurance policy against hitting http://twitter.com


class TwitterSearchTest < Test::Unit::TestCase # :nodoc:

context "@client.query 'Obama'" do
Expand All @@ -21,7 +20,7 @@ class TwitterSearchTest < Test::Unit::TestCase # :nodoc:
assert @tweets.all? { |tweet| tweet.text =~ /obama/i }
end
end

context "@client.query 'twitter search'" do
setup do
@tweets = read_yaml :file => 'twitter_search'
Expand All @@ -33,7 +32,7 @@ class TwitterSearchTest < Test::Unit::TestCase # :nodoc:
assert @tweets.all?{ |t| t.text =~ /twitter/i && t.text =~ /search/i }
end
end

context "@client.query :q => 'twitter search'" do
setup do
@tweets = read_yaml :file => 'twitter_search_and'
Expand All @@ -45,7 +44,7 @@ class TwitterSearchTest < Test::Unit::TestCase # :nodoc:
assert @tweets.all?{ |t| t.text =~ /twitter/i && t.text =~ /search/i }
end
end

# TWITTER SEARCH OPERATORS

context '@client.query :q => \'"happy hour"\'' do
Expand Down Expand Up @@ -308,7 +307,7 @@ class TwitterSearchTest < Test::Unit::TestCase # :nodoc:
assert @tweets.has_next_page?

FakeWeb.register_uri( :get,
"#{TwitterSearch::Client::TWITTER_API_URL}?max_id=100&q=almost+a+Google%28or+is+it+Twitter%29whack&rpp=1&page=2",
"#{TwitterSearch::Client::TWITTER_SEARCH_API_URL}?max_id=100&q=almost+a+Google%28or+is+it+Twitter%29whack&rpp=1&page=2",
:string => '{"results":[{"text":"Boston Celtics-Los Angeles Lakers, Halftime http://tinyurl.com/673s24","from_user":"nbatube","id":858836387,"language":"en","created_at":"Tue, 15 Jul 2008 09:27:57 +0000"}],"since_id":0,"max_id":100,"results_per_page":1,"page":2,"query":"almost+a+Google%28or+is+it+Twitter%29whack"}'
)
next_page = @tweets.get_next_page
Expand Down Expand Up @@ -359,9 +358,4 @@ def hyperlinks?(str)
str.include?('http://') || str.include?('https://')
end

def read_yaml(opts = {})
return if opts[:file].nil?
YAML.load_file File.join(File.dirname(__FILE__), 'yaml', "#{opts[:file]}.yaml")
end

end
17 changes: 17 additions & 0 deletions test/twitter_search_trends_test.rb
@@ -0,0 +1,17 @@
# coding: utf-8
require File.join(File.dirname(__FILE__), 'test_helper')
require 'fake_web'

FakeWeb.allow_net_connect = false # an insurance policy against hitting http://twitter.com

class TwitterSearchTrendsTest < Test::Unit::TestCase # :nodoc:
context "@client.trends" do
setup do
@trends = read_yaml :file => 'trends'
end

should 'find a single trend' do
assert_equal 1, @trends.size
end
end
end
7 changes: 7 additions & 0 deletions test/yaml/trends.yaml
@@ -0,0 +1,7 @@
--- !ruby/object:TwitterSearch::Trends
as_of: 1242339160
trends:
"2009-05-14 22:12:40":
- !ruby/object:TwitterSearch::Trend
name: trend1
query: trend1

0 comments on commit eb2d1b1

Please sign in to comment.