Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (81 sloc) 2.78 kb
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
1 require 'rubygems'
2 require 'net/http'
3 require 'json'
4 require 'cgi'
5
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
6 require File.join(File.dirname(__FILE__), 'tweets')
7 require File.join(File.dirname(__FILE__), 'trends')
a8d9a15 @HotFusionMan Added methods to check for and retrieve the next page of search results.
HotFusionMan authored
8
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
9 module TwitterSearch
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
10 class SearchOperatorError < ArgumentError
11 end
7667459 @look I have found that complicated Twitter searches can result in 404s. This ...
look authored
12 class SearchServerError < RuntimeError
13 end
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
14
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
15 class Client
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
16 TWITTER_SEARCH_API_URL = 'http://search.twitter.com/search.json'
17 TWITTER_TRENDS_API_URL = 'http://search.twitter.com/trends/current.json'
997c8ee @croaky refactoring test suite
croaky authored
18 DEFAULT_TIMEOUT = 5
19
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
20 attr_accessor :agent
6e3dfd7 @dburkes parameterize timeout
dburkes authored
21 attr_accessor :timeout
997c8ee @croaky refactoring test suite
croaky authored
22
23 def initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT)
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
24 @agent = agent
6e3dfd7 @dburkes parameterize timeout
dburkes authored
25 @timeout = timeout
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
26 end
997c8ee @croaky refactoring test suite
croaky authored
27
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
28 def headers
29 { "Content-Type" => 'application/json',
30 "User-Agent" => @agent }
31 end
997c8ee @croaky refactoring test suite
croaky authored
32
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
33 def query(opts = {})
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
34 url = URI.parse(TWITTER_SEARCH_API_URL)
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
35 url.query = sanitize_query(opts)
a8d9a15 @HotFusionMan Added methods to check for and retrieve the next page of search results.
HotFusionMan authored
36
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
37 ensure_no_location_operators(url.query)
38
d1d6ccb @croaky bump the gemspec: 0.5.2
croaky authored
39 req = Net::HTTP::Get.new(url.path)
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
40 http = Net::HTTP.new(url.host, url.port)
41 http.read_timeout = timeout
997c8ee @croaky refactoring test suite
croaky authored
42
7667459 @look I have found that complicated Twitter searches can result in 404s. This ...
look authored
43 res = http.start { |http|
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
44 http.get("#{url.path}?#{url.query}", headers)
7667459 @look I have found that complicated Twitter searches can result in 404s. This ...
look authored
45 }
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
46
7667459 @look I have found that complicated Twitter searches can result in 404s. This ...
look authored
47 if res.code == '404'
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
48 raise TwitterSearch::SearchServerError,
49 "Twitter responded with a 404 for your query."
7667459 @look I have found that complicated Twitter searches can result in 404s. This ...
look authored
50 end
e41321d @mjankowski account for possible 403 response
mjankowski authored
51
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
52 json = res.body
e41321d @mjankowski account for possible 403 response
mjankowski authored
53 parsed_json = JSON.parse(json)
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
54
480284e @mjankowski pass the error along to the user
mjankowski authored
55 if parsed_json['error']
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
56 raise TwitterSearch::SearchServerError,
57 "Twitter responded with an error body: #{parsed_json['error']}"
e41321d @mjankowski account for possible 403 response
mjankowski authored
58 end
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
59
e41321d @mjankowski account for possible 403 response
mjankowski authored
60 Tweets.new parsed_json
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
61 end
997c8ee @croaky refactoring test suite
croaky authored
62
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
63 def trends(opts = {})
997c8ee @croaky refactoring test suite
croaky authored
64 url = URI.parse(TWITTER_TRENDS_API_URL)
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
65 if opts['exclude_hashtags']
66 url.query = sanitize_query_hash({ :exclude_hashtags => opts['exclude_hashtags'] })
67 end
68
69 req = Net::HTTP::Get.new(url.path)
70 http = Net::HTTP.new(url.host, url.port)
71 http.read_timeout = timeout
997c8ee @croaky refactoring test suite
croaky authored
72
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
73 json = http.start { |http|
74 http.get("#{url.path}?#{url.query}", headers)
75 }.body
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
76
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
77 Trends.new JSON.parse(json)
78 end
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
79
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
80 def sanitize_query(opts)
81 if opts.is_a? String
82 "q=#{CGI.escape(opts)}"
83 elsif opts.is_a? Hash
84 "#{sanitize_query_hash(opts)}"
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
85 end
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
86 end
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
87
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
88 def sanitize_query_hash(query_hash)
89 query_hash.collect { |key, value|
90 "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
91 }.join('&')
92 end
997c8ee @croaky refactoring test suite
croaky authored
93
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
94 def ensure_no_location_operators(query_string)
95 if query_string.include?("near%3A") ||
96 query_string.include?("within%3A")
97 raise TwitterSearch::SearchOperatorError,
98 "near: and within: are available from the Twitter Search web interface, but not the API. The API requires the geocode parameter. See dancroak/twitter-search README."
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
99 end
c409842 removing YAML in favor of Fakeweb.registering. subclassing array for Twe...
Daniel Croak authored
100 end
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
101
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
102 end
103 end
Something went wrong with that request. Please try again.