Class: Twitter::Search
- Inherits:
-
Object
- Object
- Twitter::Search
- Includes:
- Enumerable, HTTParty
- Defined in:
- lib/twitter/search.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears all the query filters to make a new search.
- #containing(word, exclude = false) ⇒ Object (also: #contains)
- #each ⇒ Object
- #fetch(force = false) ⇒ Object
- #fetch_next_page ⇒ Object
- #from(user, exclude = false) ⇒ Object
-
#geocode(lat, long, range) ⇒ Object
Ranges like 25km and 50mi work.
-
#hashed(tag, exclude = false) ⇒ Object
adds filtering based on hash tag ie: #twitter.
-
#initialize(q = nil, options = {}) ⇒ Search
constructor
A new instance of Search.
-
#lang(lang) ⇒ Object
lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
- #max(id) ⇒ Object
- #next_page? ⇒ Boolean
-
#page(num) ⇒ Object
Which page of results to fetch.
-
#per_page(num) ⇒ Object
Limits the number of results per page.
-
#phrase(phrase) ⇒ Object
Search for a phrase instead of a group of words.
- #referencing(user, exclude = false) ⇒ Object (also: #references, #ref)
-
#result_type(result_type) ⇒ Object
popular|recent.
-
#since(since_id) ⇒ Object
Only searches tweets since a given id.
-
#since_date(since_date) ⇒ Object
From the advanced search form, not documented in the API Format YYYY-MM-DD.
- #to(user, exclude = false) ⇒ Object
-
#until_date(until_date) ⇒ Object
From the advanced search form, not documented in the API Format YYYY-MM-DD.
- #user_agent ⇒ Object
Constructor Details
#initialize(q = nil, options = {}) ⇒ Search
Returns a new instance of Search.
10 11 12 13 14 15 |
# File 'lib/twitter/search.rb', line 10 def initialize(q=nil, ={}) @options = clear containing(q) if q && q.strip != "" self.class.base_uri([:api_endpoint]) if [:api_endpoint] end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
8 9 10 |
# File 'lib/twitter/search.rb', line 8 def query @query end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
8 9 10 |
# File 'lib/twitter/search.rb', line 8 def result @result end |
Instance Method Details
#clear ⇒ Object
Clears all the query filters to make a new search
117 118 119 120 121 122 |
# File 'lib/twitter/search.rb', line 117 def clear @fetch = nil @query = {} @query[:q] = [] self end |
#containing(word, exclude = false) ⇒ Object Also known as: contains
38 39 40 41 |
# File 'lib/twitter/search.rb', line 38 def containing(word, exclude=false) @query[:q] << "#{exclude ? "-" : ""}#{word}" self end |
#each ⇒ Object
134 135 136 137 138 |
# File 'lib/twitter/search.rb', line 134 def each results = fetch()['results'] return if results.nil? results.each {|r| yield r} end |
#fetch(force = false) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/twitter/search.rb', line 124 def fetch(force=false) if @fetch.nil? || force query = @query.dup query[:q] = query[:q].join(" ") perform_get(query) end @fetch end |
#fetch_next_page ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/twitter/search.rb', line 144 def fetch_next_page if next_page? s = Search.new(nil, :user_agent => user_agent) s.perform_get(fetch()["next_page"][1..-1]) s end end |
#from(user, exclude = false) ⇒ Object
21 22 23 24 |
# File 'lib/twitter/search.rb', line 21 def from(user, exclude=false) @query[:q] << "#{exclude ? "-" : ""}from:#{user}" self end |
#geocode(lat, long, range) ⇒ Object
Ranges like 25km and 50mi work.
106 107 108 109 |
# File 'lib/twitter/search.rb', line 106 def geocode(lat, long, range) @query[:geocode] = [lat, long, range].join(",") self end |
#hashed(tag, exclude = false) ⇒ Object
adds filtering based on hash tag ie: #twitter
45 46 47 48 |
# File 'lib/twitter/search.rb', line 45 def hashed(tag, exclude=false) @query[:q] << "#{exclude ? "-" : ""}\##{tag}" self end |
#lang(lang) ⇒ Object
lang must be ISO 639-1 code ie: en, fr, de, ja, etc.
when I tried en it limited my results a lot and took out several tweets that were english so i’d avoid this unless you really want it
61 62 63 64 |
# File 'lib/twitter/search.rb', line 61 def lang(lang) @query[:lang] = lang self end |
#max(id) ⇒ Object
111 112 113 114 |
# File 'lib/twitter/search.rb', line 111 def max(id) @query[:max_id] = id self end |
#next_page? ⇒ Boolean
140 141 142 |
# File 'lib/twitter/search.rb', line 140 def next_page? !!fetch()["next_page"] end |
#page(num) ⇒ Object
Which page of results to fetch
79 80 81 82 |
# File 'lib/twitter/search.rb', line 79 def page(num) @query[:page] = num self end |
#per_page(num) ⇒ Object
Limits the number of results per page
73 74 75 76 |
# File 'lib/twitter/search.rb', line 73 def per_page(num) @query[:rpp] = num self end |
#phrase(phrase) ⇒ Object
Search for a phrase instead of a group of words
51 52 53 54 |
# File 'lib/twitter/search.rb', line 51 def phrase(phrase) @query[:phrase] = phrase self end |
#referencing(user, exclude = false) ⇒ Object Also known as: references, ref
31 32 33 34 |
# File 'lib/twitter/search.rb', line 31 def referencing(user, exclude=false) @query[:q] << "#{exclude ? "-" : ""}@#{user}" self end |
#result_type(result_type) ⇒ Object
popular|recent
67 68 69 70 |
# File 'lib/twitter/search.rb', line 67 def result_type(result_type) @query[:result_type] = result_type self end |
#since(since_id) ⇒ Object
Only searches tweets since a given id. Recommended to use this when possible.
86 87 88 89 |
# File 'lib/twitter/search.rb', line 86 def since(since_id) @query[:since_id] = since_id self end |
#since_date(since_date) ⇒ Object
From the advanced search form, not documented in the API Format YYYY-MM-DD
93 94 95 96 |
# File 'lib/twitter/search.rb', line 93 def since_date(since_date) @query[:since] = since_date self end |
#to(user, exclude = false) ⇒ Object
26 27 28 29 |
# File 'lib/twitter/search.rb', line 26 def to(user, exclude=false) @query[:q] << "#{exclude ? "-" : ""}to:#{user}" self end |
#until_date(until_date) ⇒ Object
From the advanced search form, not documented in the API Format YYYY-MM-DD
100 101 102 103 |
# File 'lib/twitter/search.rb', line 100 def until_date(until_date) @query[:until] = until_date self end |
#user_agent ⇒ Object
17 18 19 |
# File 'lib/twitter/search.rb', line 17 def user_agent @options[:user_agent] || "Ruby Twitter Gem" end |