Class: TwitterSearch::Client
- Inherits:
-
Object
- Object
- TwitterSearch::Client
- Defined in:
- lib/twitter_search.rb
Constant Summary collapse
- TWITTER_SEARCH_API_URL =
'http://search.twitter.com/search.json'
- TWITTER_TRENDS_API_URL =
'http://search.twitter.com/trends/current.json'
- DEFAULT_TIMEOUT =
5
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #ensure_no_location_operators(query_string) ⇒ Object
- #headers ⇒ Object
-
#initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
- #query(opts = {}) ⇒ Object
- #sanitize_query(opts) ⇒ Object
- #sanitize_query_hash(query_hash) ⇒ Object
- #trends(opts = {}) ⇒ Object
Constructor Details
#initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
23 24 25 26 |
# File 'lib/twitter_search.rb', line 23 def initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT) @agent = agent @timeout = timeout end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
20 21 22 |
# File 'lib/twitter_search.rb', line 20 def agent @agent end |
#timeout ⇒ Object
Returns the value of attribute timeout.
21 22 23 |
# File 'lib/twitter_search.rb', line 21 def timeout @timeout end |
Instance Method Details
#ensure_no_location_operators(query_string) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/twitter_search.rb', line 94 def ensure_no_location_operators(query_string) if query_string.include?("near%3A") || query_string.include?("within%3A") raise TwitterSearch::SearchOperatorError, "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." end end |
#headers ⇒ Object
28 29 30 31 |
# File 'lib/twitter_search.rb', line 28 def headers { "Content-Type" => 'application/json', "User-Agent" => @agent } end |
#query(opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/twitter_search.rb', line 33 def query(opts = {}) url = URI.parse(TWITTER_SEARCH_API_URL) url.query = sanitize_query(opts) ensure_no_location_operators(url.query) req = Net::HTTP::Get.new(url.path) http = Net::HTTP.new(url.host, url.port) http.read_timeout = timeout res = http.start { |http| http.get("#{url.path}?#{url.query}", headers) } if res.code == '404' raise TwitterSearch::SearchServerError, "Twitter responded with a 404 for your query." end json = res.body parsed_json = JSON.parse(json) if parsed_json['error'] raise TwitterSearch::SearchServerError, "Twitter responded with an error body: #{parsed_json['error']}" end Tweets.new parsed_json end |
#sanitize_query(opts) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/twitter_search.rb', line 80 def sanitize_query(opts) if opts.is_a? String "q=#{CGI.escape(opts)}" elsif opts.is_a? Hash "#{sanitize_query_hash(opts)}" end end |
#sanitize_query_hash(query_hash) ⇒ Object
88 89 90 91 92 |
# File 'lib/twitter_search.rb', line 88 def sanitize_query_hash(query_hash) query_hash.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.join('&') end |
#trends(opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/twitter_search.rb', line 63 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 |