Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 89 lines (70 sloc) 2.41 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
12
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
13 class Client
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
14 TWITTER_SEARCH_API_URL = 'http://search.twitter.com/search.json'
15 TWITTER_TRENDS_API_URL = 'http://search.twitter.com/trends/current.json'
997c8ee @croaky refactoring test suite
croaky authored
16 DEFAULT_TIMEOUT = 5
17
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
18 attr_accessor :agent
6e3dfd7 @dburkes parameterize timeout
dburkes authored
19 attr_accessor :timeout
997c8ee @croaky refactoring test suite
croaky authored
20
21 def initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT)
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
22 @agent = agent
6e3dfd7 @dburkes parameterize timeout
dburkes authored
23 @timeout = timeout
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
24 end
997c8ee @croaky refactoring test suite
croaky authored
25
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
26 def headers
27 { "Content-Type" => 'application/json',
28 "User-Agent" => @agent }
29 end
997c8ee @croaky refactoring test suite
croaky authored
30
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
31 def query(opts = {})
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
32 url = URI.parse(TWITTER_SEARCH_API_URL)
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
33 url.query = sanitize_query(opts)
a8d9a15 @HotFusionMan Added methods to check for and retrieve the next page of search results.
HotFusionMan authored
34
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
35 ensure_no_location_operators(url.query)
36
d1d6ccb @croaky bump the gemspec: 0.5.2
croaky authored
37 req = Net::HTTP::Get.new(url.path)
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
38 http = Net::HTTP.new(url.host, url.port)
39 http.read_timeout = timeout
997c8ee @croaky refactoring test suite
croaky authored
40
ac48909 Send User Agent to Twitter when doing the HTTP request
Alejandro Crosa authored
41 json = http.start { |http|
42 http.get("#{url.path}?#{url.query}", headers)
43 }.body
44 Tweets.new JSON.parse(json)
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
45 end
997c8ee @croaky refactoring test suite
croaky authored
46
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
47 def trends(opts = {})
997c8ee @croaky refactoring test suite
croaky authored
48 url = URI.parse(TWITTER_TRENDS_API_URL)
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
49 if opts['exclude_hashtags']
50 url.query = sanitize_query_hash({ :exclude_hashtags => opts['exclude_hashtags'] })
51 end
52
53 req = Net::HTTP::Get.new(url.path)
54 http = Net::HTTP.new(url.host, url.port)
55 http.read_timeout = timeout
997c8ee @croaky refactoring test suite
croaky authored
56
eb2d1b1 @croaky pulling in Matt Sanford's trends addition
croaky authored
57 json = http.start { |http|
58 http.get("#{url.path}?#{url.query}", headers)
59 }.body
60 Trends.new JSON.parse(json)
61 end
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
62
63 private
64
65 def sanitize_query(opts)
66 if opts.is_a? String
997c8ee @croaky refactoring test suite
croaky authored
67 "q=#{CGI.escape(opts)}"
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
68 elsif opts.is_a? Hash
69 "#{sanitize_query_hash(opts)}"
70 end
71 end
72
73 def sanitize_query_hash(query_hash)
997c8ee @croaky refactoring test suite
croaky authored
74 query_hash.collect { |key, value|
75 "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
d1d6ccb @croaky bump the gemspec: 0.5.2
croaky authored
76 }.join('&')
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
77 end
997c8ee @croaky refactoring test suite
croaky authored
78
4c040be @croaky raise a SearchOperatorError when near: or within: are in the query
croaky authored
79 def ensure_no_location_operators(query_string)
80 if query_string.include?("near%3A") ||
81 query_string.include?("within%3A")
82 raise TwitterSearch::SearchOperatorError,
83 "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."
84 end
85 end
86
520eb1c @croaky converting ruby-summize gem to newly named twitter-search
croaky authored
87 end
88 end
Something went wrong with that request. Please try again.