Class: TwitterSearch::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_search.rb

Constant Summary collapse

TWITTER_SEARCH_API_URL =
'http://search.twitter.com/search.json'
'http://search.twitter.com/trends/current.json'
DEFAULT_TIMEOUT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.



33
34
35
36
# File 'lib/twitter_search.rb', line 33

def initialize(agent = 'twitter-search', timeout = DEFAULT_TIMEOUT)
  @agent   = agent
  @timeout = timeout
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



30
31
32
# File 'lib/twitter_search.rb', line 30

def agent
  @agent
end

#timeoutObject

Returns the value of attribute timeout.



31
32
33
# File 'lib/twitter_search.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#ensure_no_location_operators(query_string) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/twitter_search.rb', line 108

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

#headersObject



38
39
40
41
# File 'lib/twitter_search.rb', line 38

def headers
  { "Content-Type" => 'application/json',
    "User-Agent"   => @agent }
end

#query(opts = {}) ⇒ Object



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
73
74
75
# File 'lib/twitter_search.rb', line 43

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

  unless res['retry-after'].nil? || res['retry-after'].empty?
    raise TwitterSearch::RateLimited.new("You have been rate limited", res['retry-after'].to_i)
  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



94
95
96
97
98
99
100
# File 'lib/twitter_search.rb', line 94

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



102
103
104
105
106
# File 'lib/twitter_search.rb', line 102

def sanitize_query_hash(query_hash)
  query_hash.collect { |key, value|
    "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
  }.join('&')
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/twitter_search.rb', line 77

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