Skip to content

Commit

Permalink
Replace Twitter::Search with Twitter::Client#search
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Oct 29, 2011
1 parent e14227b commit 591cbf1
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 968 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ question mark, for example:

Twitter.user("sferik").protected?

The `Twitter::Search` class has been replaced by the `Twitter::Client#search`
method. This unifies the library's interfaces and will make the code easier to
maintain over time. As a result, you can no longer build queries by chaining
methods (ARel-style) but the new syntax is more consistent and concise.

Version 2 also includes some advanced Tweet-parsing methods, for example:

# Fetch the Tweet at https://twitter.com/twitter/statuses/76360760606986241
Expand Down Expand Up @@ -148,14 +153,13 @@ Return the text of the Tweet at https://twitter.com/sferik/statuses/27558893223
Twitter.status(27558893223).text
Find the 3 most recent marriage proposals to [@justinbieber][justinbieber]

search = Twitter::Search.new
search.containing("marry me").to("justinbieber").result_type("recent").per_page(3).map do |status|
Twitter.search("to:justinbieber marry me", :rpp => 3, :result_type => "recent").map do |status|
"#{status.from_user}: #{status.text}"
end
Enough about Justin Bieber. Let's find a Japanese-language Tweet tagged #ruby.

search = Twitter::Search.new
search.hashtag("ruby").language("ja").no_retweets.per_page(1).fetch.first.text
Let's find a Japanese-language Tweet tagged #ruby (no retweets)

Twitter.search("#ruby -rt", :lang => "ja", :rpp => 1).first.text

Certain methods require authentication. To get your Twitter OAuth credentials,
register an app at http://dev.twitter.com/apps
Expand Down
26 changes: 0 additions & 26 deletions lib/twitter/api.rb

This file was deleted.

26 changes: 21 additions & 5 deletions lib/twitter/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'twitter/api'
require 'twitter/authenticatable'
require 'twitter/config'
require 'twitter/configuration'
require 'twitter/connection'
require 'twitter/cursor'
require 'twitter/direct_message'
require 'twitter/language'
Expand All @@ -11,8 +13,8 @@
require 'twitter/polygon'
require 'twitter/rate_limit_status'
require 'twitter/relationship'
require 'twitter/request'
require 'twitter/saved_search'
require 'twitter/search'
require 'twitter/settings'
require 'twitter/size'
require 'twitter/status'
Expand All @@ -25,8 +27,7 @@ module Twitter
#
# @note All methods have been separated into modules and follow the same grouping used in {http://dev.twitter.com/doc the Twitter API Documentation}.
# @see http://dev.twitter.com/pages/every_developer
class Client < API

class Client
# Require client method modules after initializing the Client class in
# order to avoid a superclass mismatch error, allowing those modules to be
# Client-namespaced.
Expand All @@ -52,7 +53,9 @@ class Client < API
require 'twitter/client/urls'
require 'twitter/client/users'

alias :api_endpoint :endpoint
include Twitter::Authenticatable
include Twitter::Connection
include Twitter::Request

include Twitter::Client::Accounts
include Twitter::Client::Activity
Expand All @@ -76,5 +79,18 @@ class Client < API
include Twitter::Client::Urls
include Twitter::Client::Users

attr_accessor *Config::VALID_OPTIONS_KEYS

# Initializes a new API object
#
# @param attrs [Hash]
# @return [Twitter::Client]
def initialize(attrs={})
attrs = Twitter.options.merge(attrs)
Config::VALID_OPTIONS_KEYS.each do |key|
instance_variable_set("@#{key}".to_sym, attrs[key])
end
end

end
end
34 changes: 32 additions & 2 deletions lib/twitter/client/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ def videos(q, options={})
end
end

# Returns tweets that match a specified query.
#
# @see https://dev.twitter.com/docs/api/1/get/search
# @see https://dev.twitter.com/docs/using-search
# @see https://dev.twitter.com/docs/history-rest-search-api
# @note As of April 1st 2010, the Search API provides an option to retrieve "popular tweets" in addition to real-time search results. In an upcoming release, this will become the default and clients that don't want to receive popular tweets in their search results will have to explicitly opt-out. See the result_type parameter below for more information.
# @note By default, the user ids in the Search API are different from those in the REST API. To return user IDs which map to the values returned by the REST API, specify the with_twitter_user_id=true query parameter.
# @rate_limited Yes
# @requires_authentication No
# @param q [String] A search term.
# @param options [Hash] A customizable set of options.
# @option options [String] :geocode Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly.
# @option options [String] :lang Restricts tweets to the given language, given by an ISO 639-1 code.
# @option options [String] :locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases.
# @option options [Integer] :page The page number (starting at 1) to return, up to a max of roughly 1500 results (based on rpp * page).
# @option options [String] :result_type Specifies what type of search results you would prefer to receive. The current default is "mixed."
# @option options [Integer] :rpp The number of tweets to return per page, up to a max of 100.
# @option options [String] :until Optional. Returns tweets generated before the given date. Date should be formatted as YYYY-MM-DD.
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available.
# @option options [Boolean, String, Integer] :include_entities Include {http://dev.twitter.com/pages/tweet_entities Tweet Entities} when set to true, 't' or 1.
# @option options [Boolean, String, Integer] :with_twitter_user_id When set to either true, t or 1, the from_user_id, from_user_id_str, to_user_id, and to_user_id_str values in the response will map to "official" user IDs which will match those returned by the REST API.
# @return [Array<Twitter::Status>] Return tweets that match a specified query
# @example Returns tweets related to twitter
# Twitter.search('twitter')
def search(q, options={})
get("/search.json", options.merge(:q => q), :endpoint => search_endpoint)['results'].map do |status|
Twitter::Status.new(status)
end
end

# Returns recent statuses related to a query with images and videos embedded
#
# @note Undocumented
Expand All @@ -53,8 +83,8 @@ def videos(q, options={})
# @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
# @return [Array<Twitter::Status>] An array of statuses that contain videos
# @example Return recent statuses related to twitter with images and videos embedded
# Twitter.search('twitter')
def search(q, options={})
# Twitter.phoenix_search('twitter')
def phoenix_search(q, options={})
get("/phoenix_search.phoenix", options.merge(:q => q), :phoenix => true)['statuses'].map do |status|
Twitter::Status.new(status)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module Config
# The value sent in the 'User-Agent' header if none is set
DEFAULT_USER_AGENT = "Twitter Ruby Gem #{Twitter::Version}"

# An array of valid keys in the options hash when configuring a {Twitter::API}
# An array of valid keys in the options hash when configuring a {Twitter::Client}
VALID_OPTIONS_KEYS = [
:adapter,
:connection_options,
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def connection(options={})
},
:proxy => proxy,
:ssl => {:verify => false},
:url => options.fetch(:endpoint, api_endpoint),
:url => options.fetch(:endpoint, endpoint),
})
Faraday.new(merged_options) do |builder|
builder.use Twitter::Request::Phoenix if options[:phoenix]
Expand Down
Loading

0 comments on commit 591cbf1

Please sign in to comment.