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.
30 31 32 33 |
# File 'lib/twitter_search.rb', line 30 def initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT) @agent = agent @timeout = timeout end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
27 28 29 |
# File 'lib/twitter_search.rb', line 27 def agent @agent end |
#timeout ⇒ Object
Returns the value of attribute timeout.
28 29 30 |
# File 'lib/twitter_search.rb', line 28 def timeout @timeout end |
Instance Method Details
#ensure_no_location_operators(query_string) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/twitter_search.rb', line 105 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
35 36 37 38 |
# File 'lib/twitter_search.rb', line 35 def headers { "Content-Type" => 'application/json', "User-Agent" => @agent } end |
#query(opts = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/twitter_search.rb', line 40 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 if res.code == '420' raise TwitterSearch::RateLimitError.new(res['retry-after']) 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
91 92 93 94 95 96 97 |
# File 'lib/twitter_search.rb', line 91 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
99 100 101 102 103 |
# File 'lib/twitter_search.rb', line 99 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/twitter_search.rb', line 74 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 |