From 22af0c1b0c7d30c68559b7b09d24d4a7b604caf4 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Sat, 21 Jul 2012 07:59:21 -0700 Subject: [PATCH] Revert "Remove client modules" This reverts commit 396bb15fe8a273e01370e6a22efbf1e7f6a7805e. --- README.md | 4 +- lib/twitter/api/account.rb | 190 ++ lib/twitter/api/activity.rb | 52 + lib/twitter/api/blocks.rb | 115 + lib/twitter/api/direct_messages.rb | 169 + lib/twitter/api/friendships.rb | 359 +++ lib/twitter/api/geo.rb | 121 + lib/twitter/api/help.rb | 45 + lib/twitter/api/legal.rb | 38 + lib/twitter/api/lists.rb | 680 ++++ lib/twitter/api/notifications.rb | 59 + lib/twitter/api/report_spam.rb | 36 + lib/twitter/api/saved_searches.rb | 114 + lib/twitter/api/search.rb | 63 + lib/twitter/api/statuses.rb | 603 ++++ lib/twitter/api/trends.rb | 101 + lib/twitter/api/users.rb | 282 ++ lib/twitter/api/utils.rb | 27 + lib/twitter/client.rb | 2828 +---------------- lib/twitter/configurable.rb | 4 +- .../accounts_spec.rb => api/account_spec.rb} | 2 +- spec/twitter/{client => api}/activity_spec.rb | 2 +- .../block_spec.rb => api/blocks_spec.rb} | 2 +- .../{client => api}/direct_messages_spec.rb | 2 +- .../friendships_spec.rb} | 2 +- .../geo_spec.rb} | 2 +- spec/twitter/{client => api}/help_spec.rb | 2 +- spec/twitter/{client => api}/legal_spec.rb | 2 +- spec/twitter/{client => api}/lists_spec.rb | 2 +- .../notifications_spec.rb} | 2 +- .../report_spam_spec.rb} | 2 +- .../{client => api}/saved_searches_spec.rb | 2 +- spec/twitter/{client => api}/search_spec.rb | 2 +- .../tweets_spec.rb => api/statuses_spec.rb} | 279 +- .../trends_spec.rb} | 44 +- spec/twitter/{client => api}/users_spec.rb | 58 +- spec/twitter/client/favorites_spec.rb | 82 - spec/twitter/client/suggested_users_spec.rb | 65 - spec/twitter/client/timelines_spec.rb | 213 -- spec/twitter/client/trends_spec.rb | 51 - spec/twitter/client_spec.rb | 18 +- spec/twitter_spec.rb | 2 +- 42 files changed, 3496 insertions(+), 3232 deletions(-) create mode 100644 lib/twitter/api/account.rb create mode 100644 lib/twitter/api/activity.rb create mode 100644 lib/twitter/api/blocks.rb create mode 100644 lib/twitter/api/direct_messages.rb create mode 100644 lib/twitter/api/friendships.rb create mode 100644 lib/twitter/api/geo.rb create mode 100644 lib/twitter/api/help.rb create mode 100644 lib/twitter/api/legal.rb create mode 100644 lib/twitter/api/lists.rb create mode 100644 lib/twitter/api/notifications.rb create mode 100644 lib/twitter/api/report_spam.rb create mode 100644 lib/twitter/api/saved_searches.rb create mode 100644 lib/twitter/api/search.rb create mode 100644 lib/twitter/api/statuses.rb create mode 100644 lib/twitter/api/trends.rb create mode 100644 lib/twitter/api/users.rb create mode 100644 lib/twitter/api/utils.rb rename spec/twitter/{client/accounts_spec.rb => api/account_spec.rb} (99%) rename spec/twitter/{client => api}/activity_spec.rb (97%) rename spec/twitter/{client/block_spec.rb => api/blocks_spec.rb} (99%) rename spec/twitter/{client => api}/direct_messages_spec.rb (99%) rename spec/twitter/{client/friends_and_followers_spec.rb => api/friendships_spec.rb} (99%) rename spec/twitter/{client/places_and_geo_spec.rb => api/geo_spec.rb} (99%) rename spec/twitter/{client => api}/help_spec.rb (97%) rename spec/twitter/{client => api}/legal_spec.rb (96%) rename spec/twitter/{client => api}/lists_spec.rb (99%) rename spec/twitter/{client/notification_spec.rb => api/notifications_spec.rb} (97%) rename spec/twitter/{client/spam_reporting_spec.rb => api/report_spam_spec.rb} (95%) rename spec/twitter/{client => api}/saved_searches_spec.rb (98%) rename spec/twitter/{client => api}/search_spec.rb (98%) rename spec/twitter/{client/tweets_spec.rb => api/statuses_spec.rb} (51%) rename spec/twitter/{client/local_trends_spec.rb => api/trends_spec.rb} (51%) rename spec/twitter/{client => api}/users_spec.rb (87%) delete mode 100644 spec/twitter/client/favorites_spec.rb delete mode 100644 spec/twitter/client/suggested_users_spec.rb delete mode 100644 spec/twitter/client/timelines_spec.rb delete mode 100644 spec/twitter/client/trends_spec.rb diff --git a/README.md b/README.md index 12b7f07da..b1de77aa0 100644 --- a/README.md +++ b/README.md @@ -161,8 +161,8 @@ vulnerabilities are discovered. Here are some fun facts about the 3.0 release: -* The entire library is implemented in just 2,273 lines of code -* With over 5,000 lines of specs, the spec-to-code ratio is well over 2:1 +* The entire library is implemented in just 2,500 lines of code +* With over 5,000 lines of specs, the spec-to-code ratio is over 2:1 * The spec suite contains 665 examples and runs in under 2 seconds on a MacBook * This project has 100% C0 code coverage (the tests execute every line of source code at least once) diff --git a/lib/twitter/api/account.rb b/lib/twitter/api/account.rb new file mode 100644 index 000000000..d4573c9e2 --- /dev/null +++ b/lib/twitter/api/account.rb @@ -0,0 +1,190 @@ +require 'twitter/rate_limit_status' +require 'twitter/settings' +require 'twitter/user' + +module Twitter + module API + module Account + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :rate_limit_status => false, + :verify_credentials => true, + :current_user => true, + :end_session => false, + :update_delivery_device => false, + :update_profile => false, + :update_profile_background_image => false, + :update_profile_colors => false, + :update_profile_image => false, + :settings => true, + ) + end + + # Returns the remaining number of API requests available to the requesting user + # + # @see https://dev.twitter.com/docs/api/1/get/account/rate_limit_status + # @rate_limited No + # @authentication_required No + # @return [Twitter::RateLimitStatus] + # @param options [Hash] A customizable set of options. + # @example Return the remaining number of API requests available to the requesting user + # Twitter.rate_limit_status + def rate_limit_status(options={}) + response = get("/1/account/rate_limit_status.json", options) + Twitter::RateLimitStatus.from_response(response) + end + + # Returns the requesting user if authentication was successful, otherwise raises {Twitter::Error::Unauthorized} + # + # @see https://dev.twitter.com/docs/api/1/get/account/verify_credentials + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param options [Hash] A customizable set of options. + # @option options [Boolean, String, Integer] :skip_status Do not include user's statuses when set to true, 't' or 1. + # @example Return the requesting user if authentication was successful + # Twitter.verify_credentials + def verify_credentials(options={}) + response = get("/1/account/verify_credentials.json", options) + Twitter::User.from_response(response) + end + alias current_user verify_credentials + + # Ends the session of the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/account/end_session + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Hash] + # @param options [Hash] A customizable set of options. + # @example End the session of the authenticating user + # Twitter.end_session + def end_session(options={}) + post("/1/account/end_session.json", options)[:body] + end + + # Sets which device Twitter delivers updates to for the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/account/update_delivery_device + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param device [String] Must be one of: 'sms', 'none'. + # @param options [Hash] A customizable set of options. + # @example Turn SMS updates on for the authenticating user + # Twitter.update_delivery_device('sms') + def update_delivery_device(device, options={}) + response = post("/1/account/update_delivery_device.json", options.merge(:device => device)) + Twitter::User.from_response(response) + end + + # Sets values that users are able to set under the "Account" tab of their settings page + # + # @see https://dev.twitter.com/docs/api/1/post/account/update_profile + # @note Only the options specified will be updated. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param options [Hash] A customizable set of options. + # @option options [String] :name Full name associated with the profile. Maximum of 20 characters. + # @option options [String] :url URL associated with the profile. Will be prepended with "http://" if not present. Maximum of 100 characters. + # @option options [String] :location The city or country describing where the user of the account is located. The contents are not normalized or geocoded in any way. Maximum of 30 characters. + # @option options [String] :description A description of the user owning the account. Maximum of 160 characters. + # @example Set authenticating user's name to Erik Michaels-Ober + # Twitter.update_profile(:name => "Erik Michaels-Ober") + def update_profile(options={}) + response = post("/1/account/update_profile.json", options) + Twitter::User.from_response(response) + end + + # Updates the authenticating user's profile background image + # + # @see https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param image [File, Hash] The background image for the profile. Must be a valid GIF, JPG, or PNG image of less than 800 kilobytes in size. Images with width larger than 2048 pixels will be scaled down. + # @param options [Hash] A customizable set of options. + # @option options [Boolean] :tile Whether or not to tile the background image. If set to true the background image will be displayed tiled. The image will not be tiled otherwise. + # @example Update the authenticating user's profile background image + # Twitter.update_profile_background_image(File.new("we_concept_bg2.png")) + # Twitter.update_profile_background_image(:io => StringIO.new(pic), :type => 'jpg') + def update_profile_background_image(image, options={}) + response = post("/1/account/update_profile_background_image.json", options.merge(:image => image)) + Twitter::User.from_response(response) + end + + # Sets one or more hex values that control the color scheme of the authenticating user's profile + # + # @see https://dev.twitter.com/docs/api/1/post/account/update_profile_colors + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param options [Hash] A customizable set of options. + # @option options [String] :profile_background_color Profile background color. + # @option options [String] :profile_text_color Profile text color. + # @option options [String] :profile_link_color Profile link color. + # @option options [String] :profile_sidebar_fill_color Profile sidebar's background color. + # @option options [String] :profile_sidebar_border_color Profile sidebar's border color. + # @example Set authenticating user's profile background to black + # Twitter.update_profile_colors(:profile_background_color => '000000') + def update_profile_colors(options={}) + response = post("/1/account/update_profile_colors.json", options) + Twitter::User.from_response(response) + end + + # Updates the authenticating user's profile image + # + # @see https://dev.twitter.com/docs/api/1/post/account/update_profile_image + # @note This method asynchronously processes the uploaded file before updating the user's profile image URL. You can either update your local cache the next time you request the user's information, or, at least 5 seconds after uploading the image, ask for the updated URL using {Twitter::User#profile_image_url}. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::User] The authenticated user. + # @param image [File, Hash] The avatar image for the profile. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down. Animated GIFs will be converted to a static GIF of the first frame, removing the animation. + # @param options [Hash] A customizable set of options. + # @example Update the authenticating user's profile image + # Twitter.update_profile_image(File.new("me.jpeg")) + # Twitter.update_profile_image(:io => StringIO.new(pic), :type => 'jpg') + def update_profile_image(image, options={}) + response = post("/1/account/update_profile_image.json", options.merge(:image => image)) + Twitter::User.from_response(response) + end + + # Updates the authenticating user's settings. + # Or, if no options supplied, returns settings (including current trend, geo and sleep time information) for the authenticating user. + # + # @see https://dev.twitter.com/docs/api/1/post/account/settings + # @see https://dev.twitter.com/docs/api/1/get/account/settings + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Settings] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :trend_location_woeid The Yahoo! Where On Earth ID to use as the user's default trend location. Global information is available by using 1 as the WOEID. The woeid must be one of the locations returned by {https://dev.twitter.com/docs/api/1/get/trends/available GET trends/available}. + # @option options [Boolean, String, Integer] :sleep_time_enabled When set to true, 't' or 1, will enable sleep time for the user. Sleep time is the time when push or SMS notifications should not be sent to the user. + # @option options [Integer] :start_sleep_time The hour that sleep time should begin if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting. + # @option options [Integer] :end_sleep_time The hour that sleep time should end if it is enabled. The value for this parameter should be provided in {http://en.wikipedia.org/wiki/ISO_8601 ISO8601} format (i.e. 00-23). The time is considered to be in the same timezone as the user's time_zone setting. + # @option options [String] :time_zone The timezone dates and times should be displayed in for the user. The timezone must be one of the {http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html Rails TimeZone} names. + # @option options [String] :lang The language which Twitter should render in for this user. The language must be specified by the appropriate two letter ISO 639-1 representation. Currently supported languages are provided by {https://dev.twitter.com/docs/api/1/get/help/languages GET help/languages}. + # @example Return the settings for the authenticating user. + # Twitter.settings + def settings(options={}) + response = if options.size.zero? + get("/1/account/settings.json", options) + else + post("/1/account/settings.json", options) + end + Twitter::Settings.from_response(response) + end + + end + end +end diff --git a/lib/twitter/api/activity.rb b/lib/twitter/api/activity.rb new file mode 100644 index 000000000..7cb8ac355 --- /dev/null +++ b/lib/twitter/api/activity.rb @@ -0,0 +1,52 @@ +require 'twitter/api/utils' +require 'twitter/action_factory' + +module Twitter + module API + module Activity + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :activity_about_me => true, + :activity_by_friends => true, + ) + end + + # Returns activity about me + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] An array of actions + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @example Return activity about me + # Twitter.activity_about_me + def activity_about_me(options={}) + response = get("/i/activity/about_me.json", options) + collection_from_array(response[:body], Twitter::ActionFactory) + end + + # Returns activity by friends + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid./ + # @return [Array] An array of actions + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @example Return activity by friends + # Twitter.activity_by_friends + def activity_by_friends(options={}) + response = get("/i/activity/by_friends.json", options) + collection_from_array(response[:body], Twitter::ActionFactory) + end + + end + end +end diff --git a/lib/twitter/api/blocks.rb b/lib/twitter/api/blocks.rb new file mode 100644 index 000000000..217db33ed --- /dev/null +++ b/lib/twitter/api/blocks.rb @@ -0,0 +1,115 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/hash' + +module Twitter + module API + module Blocks + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :blocking => true, + :blocked_ids => true, + :block? => true, + :block => true, + :unblock => false, + ) + end + + # Returns an array of user objects that the authenticating user is blocking + # + # @see https://dev.twitter.com/docs/api/1/get/blocks/blocking + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] User objects that the authenticating user is blocking. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :page Specifies the page of results to retrieve. + # @example Return an array of user objects that the authenticating user is blocking + # Twitter.blocking + def blocking(options={}) + response = get("/1/blocks/blocking.json", options) + collection_from_array(response[:body], Twitter::User) + end + + # Returns an array of numeric user ids the authenticating user is blocking + # + # @see https://dev.twitter.com/docs/api/1/get/blocks/blocking/ids + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] Numeric user ids the authenticating user is blocking. + # @param options [Hash] A customizable set of options. + # @example Return an array of numeric user ids the authenticating user is blocking + # Twitter.blocking_ids + def blocked_ids(options={}) + get("/1/blocks/blocking/ids.json", options)[:body] + end + + # Returns true if the authenticating user is blocking a target user + # + # @see https://dev.twitter.com/docs/api/1/get/blocks/exists + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Boolean] true if the authenticating user is blocking the target user, otherwise false. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @example Check whether the authenticating user is blocking @sferik + # Twitter.block?('sferik') + # Twitter.block?(7505382) # Same as above + def block?(user, options={}) + options.merge_user!(user) + get("/1/blocks/exists.json", options) + true + rescue Twitter::Error::NotFound + false + end + + # Blocks the users specified by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/blocks/create + # @note Destroys a friendship to the blocked user if it exists. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The blocked users. + # @overload block(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Block and unfriend @sferik as the authenticating user + # Twitter.block('sferik') + # Twitter.block(7505382) # Same as above + # @overload block(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def block(*args) + users_from_response(args) do |options| + post("/1/blocks/create.json", options) + end + end + + # Un-blocks the users specified by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/blocks/destroy + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The un-blocked users. + # @overload unblock(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Un-block @sferik as the authenticating user + # Twitter.unblock('sferik') + # Twitter.unblock(7505382) # Same as above + # @overload unblock(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def unblock(*args) + users_from_response(args) do |options| + delete("/1/blocks/destroy.json", options) + end + end + + end + end +end + diff --git a/lib/twitter/api/direct_messages.rb b/lib/twitter/api/direct_messages.rb new file mode 100644 index 000000000..335909c43 --- /dev/null +++ b/lib/twitter/api/direct_messages.rb @@ -0,0 +1,169 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/array' +require 'twitter/core_ext/enumerable' +require 'twitter/core_ext/hash' +require 'twitter/direct_message' + +module Twitter + module API + module DirectMessages + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :direct_messages_received => true, + :direct_messages_sent => true, + :direct_message_destroy => false, + :direct_message_create => false, + :d => false, + :m => false, + :direct_message => true, + :direct_messages => true, + ) + end + + # Returns the 20 most recent direct messages sent to the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages + # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] Direct messages sent to the authenticating user. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Integer] :page Specifies the page of results to retrieve. + # @example Return the 20 most recent direct messages sent to the authenticating user + # Twitter.direct_messages_received + def direct_messages_received(options={}) + response = get("/1/direct_messages.json", options) + collection_from_array(response[:body], Twitter::DirectMessage) + end + + # Returns the 20 most recent direct messages sent by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages/sent + # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] Direct messages sent by the authenticating user. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Integer] :page Specifies the page of results to retrieve. + # @example Return the 20 most recent direct messages sent by the authenticating user + # Twitter.direct_messages_sent + def direct_messages_sent(options={}) + response = get("/1/direct_messages/sent.json", options) + collection_from_array(response[:body], Twitter::DirectMessage) + end + + # Destroys direct messages + # + # @see https://dev.twitter.com/docs/api/1/post/direct_messages/destroy/:id + # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] Deleted direct message. + # @overload direct_message_destroy(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Destroys the direct message with the ID 1825785544 + # Twitter.direct_message_destroy(1825785544) + # @overload direct_message_destroy(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def direct_message_destroy(*args) + options = args.extract_options! + args.flatten.threaded_map do |id| + response = delete("/1/direct_messages/destroy/#{id}.json", options) + Twitter::DirectMessage.from_response(response) + end + end + + # Sends a new direct message to the specified user from the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/direct_messages/new + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::DirectMessage] The sent message. + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param text [String] The text of your direct message, up to 140 characters. + # @param options [Hash] A customizable set of options. + # @example Send a direct message to @sferik from the authenticating user + # Twitter.direct_message_create('sferik', "I'm sending you this message via @gem!") + # Twitter.direct_message_create(7505382, "I'm sending you this message via @gem!") # Same as above + def direct_message_create(user, text, options={}) + options.merge_user!(user) + response = post("/1/direct_messages/new.json", options.merge(:text => text)) + Twitter::DirectMessage.from_response(response) + end + alias d direct_message_create + alias m direct_message_create + + # Returns a direct message + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages/show/%3Aid + # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::DirectMessage] The requested messages. + # @param id [Integer] A Twitter status IDs. + # @param options [Hash] A customizable set of options. + # @example Return the direct message with the id 1825786345 + # Twitter.direct_message(1825786345) + def direct_message(id, options={}) + response = get("/1/direct_messages/show/#{id}.json", options) + Twitter::DirectMessage.from_response(response) + end + + # @note This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The requested messages. + # @overload direct_messages(options={}) + # Returns the 20 most recent direct messages sent to the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Integer] :page Specifies the page of results to retrieve. + # @example Return the 20 most recent direct messages sent to the authenticating user + # Twitter.direct_messages + # @overload direct_messages(*ids) + # Returns direct messages + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages/show/%3Aid + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Return the direct message with the id 1825786345 + # Twitter.direct_messages(1825786345) + # @overload direct_messages(*ids, options) + # Returns direct messages + # + # @see https://dev.twitter.com/docs/api/1/get/direct_messages/show/%3Aid + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def direct_messages(*args) + options = args.extract_options! + if args.empty? + self.direct_messages_received(options) + else + args.flatten.threaded_map do |id| + response = get("/1/direct_messages/show/#{id}.json", options) + Twitter::DirectMessage.from_response(response) + end + end + end + + end + end +end diff --git a/lib/twitter/api/friendships.rb b/lib/twitter/api/friendships.rb new file mode 100644 index 000000000..646318c2f --- /dev/null +++ b/lib/twitter/api/friendships.rb @@ -0,0 +1,359 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/array' +require 'twitter/core_ext/enumerable' +require 'twitter/core_ext/hash' +require 'twitter/cursor' +require 'twitter/relationship' +require 'twitter/user' + +module Twitter + module API + module Friendships + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :follower_ids => true, + :friend_ids => true, + :friendship? => true, + :friendships_incoming => true, + :friendships_outgoing => true, + :friendship => true, + :friendship_show => true, + :relationship => true, + :follow => false, + :friendship_create => false, + :follow! => false, + :friendship_create! => false, + :unfollow => false, + :friendship_destroy => false, + :friendships => true, + :friendship_update => true, + :no_retweet_ids => true, + :accept => false, + :deny => false, + ) + end + + # @see https://dev.twitter.com/docs/api/1/get/followers/ids + # @rate_limited Yes + # @authentication_required No unless requesting it from a protected user + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload follower_ids(options={}) + # Returns an array of numeric IDs for every user following the authenticated user + # + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the authenticated user's followers' IDs + # Twitter.follower_ids + # @overload follower_ids(user, options={}) + # Returns an array of numeric IDs for every user following the specified user + # + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return @sferik's followers' IDs + # Twitter.follower_ids('sferik') + # Twitter.follower_ids(7505382) # Same as above + def follower_ids(*args) + options = {:cursor => -1} + options.merge!(args.extract_options!) + user = args.pop + options.merge_user!(user) + response = get("/1/followers/ids.json", options) + Twitter::Cursor.from_response(response) + end + + # @see https://dev.twitter.com/docs/api/1/get/friends/ids + # @rate_limited Yes + # @authentication_required No unless requesting it from a protected user + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload friend_ids(options={}) + # Returns an array of numeric IDs for every user the authenticated user is following + # + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. This is recommended for users who are following many users. Provide a value of -1 to begin paging. Provide values as returned in the response body's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the authenticated user's friends' IDs + # Twitter.friend_ids + # @overload friend_ids(user, options={}) + # Returns an array of numeric IDs for every user the specified user is following + # + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return @sferik's friends' IDs + # Twitter.friend_ids('sferik') + # Twitter.friend_ids(7505382) # Same as above + def friend_ids(*args) + options = {:cursor => -1} + options.merge!(args.extract_options!) + user = args.pop + options.merge_user!(user) + response = get("/1/friends/ids.json", options) + Twitter::Cursor.from_response(response) + end + + # Test for the existence of friendship between two users + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/exists + # @note Consider using {Twitter::API::Friendships#friendship} instead of this method. + # @rate_limited Yes + # @authentication_required No unless user_a or user_b is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Boolean] true if user_a follows user_b, otherwise false. + # @param user_a [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the subject user. + # @param user_b [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the user to test for following. + # @param options [Hash] A customizable set of options. + # @example Return true if @sferik follows @pengwynn + # Twitter.friendship?('sferik', 'pengwynn') + # Twitter.friendship?('sferik', 14100886) # Same as above + # Twitter.friendship?(7505382, 14100886) # Same as above + def friendship?(user_a, user_b, options={}) + options.merge_user!(user_a, nil, "a") + options.merge_user!(user_b, nil, "b") + get("/1/friendships/exists.json", options)[:body] + end + + # Returns an array of numeric IDs for every user who has a pending request to follow the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/incoming + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return an array of numeric IDs for every user who has a pending request to follow the authenticating user + # Twitter.friendships_incoming + def friendships_incoming(options={}) + options = {:cursor => -1}.merge(options) + response = get("/1/friendships/incoming.json", options) + Twitter::Cursor.from_response(response) + end + + # Returns an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/outgoing + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return an array of numeric IDs for every protected user for whom the authenticating user has a pending follow request + # Twitter.friendships_outgoing + def friendships_outgoing(options={}) + options = {:cursor => -1}.merge(options) + response = get("/1/friendships/outgoing.json", options) + Twitter::Cursor.from_response(response) + end + + # Returns detailed information about the relationship between two users + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/show + # @rate_limited Yes + # @authentication_required No + # @return [Twitter::Relationship] + # @param source [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the source user. + # @param target [Integer, String, Twitter::User] The Twitter user ID, screen name, or object of the target user. + # @param options [Hash] A customizable set of options. + # @example Return the relationship between @sferik and @pengwynn + # Twitter.friendship('sferik', 'pengwynn') + # Twitter.friendship('sferik', 14100886) # Same as above + # Twitter.friendship(7505382, 14100886) # Same as above + def friendship(source, target, options={}) + options.merge_user!(source, "source") + options[:source_id] = options.delete(:source_user_id) unless options[:source_user_id].nil? + options.merge_user!(target, "target") + options[:target_id] = options.delete(:target_user_id) unless options[:target_user_id].nil? + response = get("/1/friendships/show.json", options) + Twitter::Relationship.from_response(response) + end + alias friendship_show friendship + alias relationship friendship + + # Allows the authenticating user to follow the specified users, unless they are already followed + # + # @see https://dev.twitter.com/docs/api/1/post/friendships/create + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The followed users. + # @overload(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Follow @sferik + # Twitter.follow('sferik') + # @overload(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @option options [Boolean] :follow (false) Enable notifications for the target user. + def follow(*args) + options = args.extract_options! + # Twitter always turns on notifications if the "follow" option is present, even if it's set to false + # so only send follow if it's true + options.merge!(:follow => true) if options.delete(:follow) + friend_ids = Thread.new do + self.friend_ids.ids + end + user_ids = Thread.new do + self.users(args).map(&:id) + end + follow!(user_ids.value - friend_ids.value, options) + end + alias friendship_create follow + + # Allows the authenticating user to follow the specified users + # + # @see https://dev.twitter.com/docs/api/1/post/friendships/create + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The followed users. + # @overload follow!(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Follow @sferik + # Twitter.follow!('sferik') + # @overload follow!(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @option options [Boolean] :follow (false) Enable notifications for the target user. + def follow!(*args) + options = args.extract_options! + # Twitter always turns on notifications if the "follow" option is present, even if it's set to false + # so only send follow if it's true + options.merge!(:follow => true) if options.delete(:follow) + args.flatten.threaded_map do |user| + begin + response = post("/1/friendships/create.json", options.merge_user(user)) + Twitter::User.from_response(response) + rescue Twitter::Error::Forbidden + # This error will be raised if the user doesn't have permission to + # follow list_member, for whatever reason. + end + end.compact + end + alias friendship_create! follow! + + # Allows the authenticating user to unfollow the specified users + # + # @see https://dev.twitter.com/docs/api/1/post/friendships/destroy + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The unfollowed users. + # @overload unfollow(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Unfollow @sferik + # Twitter.unfollow('sferik') + # @overload unfollow(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def unfollow(*args) + users_from_response(args) do |options| + delete("/1/friendships/destroy.json", options) + end + end + alias friendship_destroy unfollow + + # Returns the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided. Values for connections can be: following, following_requested, followed_by, none. + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/lookup + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The requested users. + # @overload friendships(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Return extended information for @sferik and @pengwynn + # Twitter.friendships('sferik', 'pengwynn') + # Twitter.friendships('sferik', 14100886) # Same as above + # Twitter.friendships(7505382, 14100886) # Same as above + # @overload friendships(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def friendships(*args) + options = args.extract_options! + options.merge_users!(Array(args)) + response = get("/1/friendships/lookup.json", options) + collection_from_array(response[:body], Twitter::User) + end + + # Allows one to enable or disable retweets and device notifications from the specified user. + # + # @see https://dev.twitter.com/docs/api/1/post/friendships/update + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Relationship] + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Boolean] :device Enable/disable device notifications from the target user. + # @option options [Boolean] :retweets Enable/disable retweets from the target user. + # @example Enable rewteets and devise notifications for @sferik + # Twitter.friendship_update('sferik', :device => true, :retweets => true) + def friendship_update(user, options={}) + options.merge_user!(user) + response = post("/1/friendships/update.json", options) + Twitter::Relationship.from_response(response) + end + + # Returns an array of user_ids that the currently authenticated user does not want to see retweets from. + # + # @see https://dev.twitter.com/docs/api/1/get/friendships/no_retweet_ids + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param options [Hash] A customizable set of options. + # @option options [Boolean] :stringify_ids Many programming environments will not consume our ids due to their size. Provide this option to have ids returned as strings instead. Read more about Twitter IDs, JSON and Snowflake. + # @example Enable rewteets and devise notifications for @sferik + # Twitter.no_retweet_ids + def no_retweet_ids(options={}) + get("/1/friendships/no_retweet_ids.json", options)[:body] + end + + # Allows the authenticating user to accept the specified users' follow requests + # + # @note Undocumented + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The accepted users. + # @overload accept(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Accept @sferik's follow request + # Twitter.accept('sferik') + # @overload accept(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def accept(*args) + users_from_response(args) do |options| + post("/1/friendships/accept.json", options) + end + end + + # Allows the authenticating user to deny the specified users' follow requests + # + # @note Undocumented + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The denied users. + # @overload deny(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Deny @sferik's follow request + # Twitter.deny('sferik') + # @overload deny(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def deny(*args) + users_from_response(args) do |options| + post("/1/friendships/deny.json", options) + end + end + + end + end +end diff --git a/lib/twitter/api/geo.rb b/lib/twitter/api/geo.rb new file mode 100644 index 000000000..0df906834 --- /dev/null +++ b/lib/twitter/api/geo.rb @@ -0,0 +1,121 @@ +require 'twitter/api/utils' +require 'twitter/place' + +module Twitter + module API + module Geo + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :places_nearby => true, + :geo_search => true, + :places_similar => true, + :reverse_geocode => true, + :place => true, + :place_create => true, + ) + end + + # Search for places that can be attached to a {Twitter::API::Statuses#update} + # + # @see https://dev.twitter.com/docs/api/1/get/geo/search + # @rate_limited Yes + # @authentication_required No + # @param options [Hash] A customizable set of options. + # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option. + # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option. + # @option options [String] :query Free-form text to match against while executing a geo-based query, best suited for finding nearby locations by name. + # @option options [String] :ip An IP address. Used when attempting to fix geolocation based off of the user's IP address. + # @option options [String] :granularity ('neighborhood') This is the minimal granularity of place types to return and must be one of: 'poi', 'neighborhood', 'city', 'admin' or 'country'. + # @option options [String] :accuracy ('0m') A hint on the "region" in which to search. If a number, then this is a radius in meters, but it can also take a string that is suffixed with ft to specify feet. If coming from a device, in practice, this value is whatever accuracy the device has measuring its location (whether it be coming from a GPS, WiFi triangulation, etc.). + # @option options [Integer] :max_results A hint as to the number of results to return. This does not guarantee that the number of results returned will equal max_results, but instead informs how many "nearby" results to return. Ideally, only pass in the number of places you intend to display to the user here. + # @option options [String] :contained_within This is the place_id which you would like to restrict the search results to. Setting this value means only places within the given place_id will be found. + # @option options [String] :"attribute:street_address" This option searches for places which have this given street address. There are other well-known and application-specific attributes available. Custom attributes are also permitted. + # @return [Array] + # @example Return an array of places near the IP address 74.125.19.104 + # Twitter.places_nearby(:ip => "74.125.19.104") + def places_nearby(options={}) + response = get("/1/geo/search.json", options) + collection_from_array(response[:body][:result][:places], Twitter::Place) + end + alias geo_search places_nearby + + # Locates places near the given coordinates which are similar in name + # + # @see https://dev.twitter.com/docs/api/1/get/geo/similar_places + # @note Conceptually, you would use this method to get a list of known places to choose from first. Then, if the desired place doesn't exist, make a request to {Twitter::API::Geo#place} to create a new one. The token contained in the response is the token necessary to create a new place. + # @rate_limited Yes + # @authentication_required No + # @param options [Hash] A customizable set of options. + # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option. + # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option. + # @option options [String] :name The name a place is known as. + # @option options [String] :contained_within This is the place_id which you would like to restrict the search results to. Setting this value means only places within the given place_id will be found. + # @option options [String] :"attribute:street_address" This option searches for places which have this given street address. There are other well-known and application-specific attributes available. Custom attributes are also permitted. + # @return [Array] + # @example Return an array of places similar to Twitter HQ + # Twitter.places_similar(:lat => "37.7821120598956", :long => "-122.400612831116", :name => "Twitter HQ") + def places_similar(options={}) + response = get("/1/geo/similar_places.json", options) + collection_from_array(response[:body][:result][:places], Twitter::Place) + end + + # Searches for up to 20 places that can be used as a place_id + # + # @see https://dev.twitter.com/docs/api/1/get/geo/reverse_geocode + # @note This request is an informative call and will deliver generalized results about geography. + # @rate_limited Yes + # @authentication_required No + # @param options [Hash] A customizable set of options. + # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option. + # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option. + # @option options [String] :accuracy ('0m') A hint on the "region" in which to search. If a number, then this is a radius in meters, but it can also take a string that is suffixed with ft to specify feet. If coming from a device, in practice, this value is whatever accuracy the device has measuring its location (whether it be coming from a GPS, WiFi triangulation, etc.). + # @option options [String] :granularity ('neighborhood') This is the minimal granularity of place types to return and must be one of: 'poi', 'neighborhood', 'city', 'admin' or 'country'. + # @option options [Integer] :max_results A hint as to the number of results to return. This does not guarantee that the number of results returned will equal max_results, but instead informs how many "nearby" results to return. Ideally, only pass in the number of places you intend to display to the user here. + # @return [Array] + # @example Return an array of places within the specified region + # Twitter.reverse_geocode(:lat => "37.7821120598956", :long => "-122.400612831116") + def reverse_geocode(options={}) + response = get("/1/geo/reverse_geocode.json", options) + collection_from_array(response[:body][:result][:places], Twitter::Place) + end + + # Returns all the information about a known place + # + # @see https://dev.twitter.com/docs/api/1/get/geo/id/:place_id + # @rate_limited Yes + # @authentication_required No + # @param place_id [String] A place in the world. These IDs can be retrieved from {Twitter::API::Geo#reverse_geocode}. + # @param options [Hash] A customizable set of options. + # @return [Twitter::Place] The requested place. + # @example Return all the information about Twitter HQ + # Twitter.place("247f43d441defc03") + def place(place_id, options={}) + response = get("/1/geo/id/#{place_id}.json", options) + Twitter::Place.from_response(response) + end + + # Creates a new place at the given latitude and longitude + # + # @see https://dev.twitter.com/docs/api/1/post/geo/place + # @rate_limited Yes + # @authentication_required No + # @param options [Hash] A customizable set of options. + # @option options [String] :name The name a place is known as. + # @option options [String] :contained_within This is the place_id which you would like to restrict the search results to. Setting this value means only places within the given place_id will be found. + # @option options [String] :token The token found in the response from {Twitter::API::Geo#places_similar}. + # @option options [Float] :lat The latitude to search around. This option will be ignored unless it is inside the range -90.0 to +90.0 (North is positive) inclusive. It will also be ignored if there isn't a corresponding :long option. + # @option options [Float] :long The longitude to search around. The valid range for longitude is -180.0 to +180.0 (East is positive) inclusive. This option will be ignored if outside that range, if it is not a number, if geo_enabled is disabled, or if there not a corresponding :lat option. + # @option options [String] :"attribute:street_address" This option searches for places which have this given street address. There are other well-known and application-specific attributes available. Custom attributes are also permitted. + # @return [Twitter::Place] The created place. + # @example Create a new place + # Twitter.place_create(:name => "@sferik's Apartment", :token => "22ff5b1f7159032cf69218c4d8bb78bc", :contained_within => "41bcb736f84a799e", :lat => "37.783699", :long => "-122.393581") + def place_create(options={}) + response = post("/1/geo/place.json", options) + Twitter::Place.from_response(response) + end + + end + end +end diff --git a/lib/twitter/api/help.rb b/lib/twitter/api/help.rb new file mode 100644 index 000000000..f8ae381af --- /dev/null +++ b/lib/twitter/api/help.rb @@ -0,0 +1,45 @@ +require 'twitter/api/utils' +require 'twitter/configuration' +require 'twitter/language' + +module Twitter + module API + module Help + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :configuration => true, + :languages => true, + ) + end + + # Returns the current configuration used by Twitter + # + # @see https://dev.twitter.com/docs/api/1/get/help/configuration + # @rate_limited Yes + # @authentication_required No + # @return [Twitter::Configuration] Twitter's configuration. + # @example Return the current configuration used by Twitter + # Twitter.configuration + def configuration(options={}) + response = get("/1/help/configuration.json", options) + Twitter::Configuration.from_response(response) + end + + # Returns the list of languages supported by Twitter + # + # @see https://dev.twitter.com/docs/api/1/get/help/languages + # @rate_limited Yes + # @authentication_required No + # @return [Array] + # @example Return the list of languages Twitter supports + # Twitter.languages + def languages(options={}) + response = get("/1/help/languages.json", options) + collection_from_array(response[:body], Twitter::Language) + end + + end + end +end diff --git a/lib/twitter/api/legal.rb b/lib/twitter/api/legal.rb new file mode 100644 index 000000000..a449e5fe1 --- /dev/null +++ b/lib/twitter/api/legal.rb @@ -0,0 +1,38 @@ +module Twitter + module API + module Legal + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :privacy => true, + :tos => true, + ) + end + + # Returns {https://twitter.com/privacy Twitter's Privacy Policy} + # + # @see https://dev.twitter.com/docs/api/1/get/legal/privacy + # @rate_limited Yes + # @authentication_required No + # @return [String] + # @example Return {https://twitter.com/privacy Twitter's Privacy Policy} + # Twitter.privacy + def privacy(options={}) + get("/1/legal/privacy.json", options)[:body][:privacy] + end + + # Returns {https://twitter.com/tos Twitter's Terms of Service} + # + # @see https://dev.twitter.com/docs/api/1/get/legal/tos + # @rate_limited Yes + # @authentication_required No + # @return [String] + # @example Return {https://twitter.com/tos Twitter's Terms of Service} + # Twitter.tos + def tos(options={}) + get("/1/legal/tos.json", options)[:body][:tos] + end + + end + end +end diff --git a/lib/twitter/api/lists.rb b/lib/twitter/api/lists.rb new file mode 100644 index 000000000..b8c3c959b --- /dev/null +++ b/lib/twitter/api/lists.rb @@ -0,0 +1,680 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/array' +require 'twitter/core_ext/enumerable' +require 'twitter/core_ext/hash' +require 'twitter/cursor' +require 'twitter/list' + +module Twitter + module API + module Lists + include Twitter::API::Utils + MAX_USERS_PER_REQUEST = 100 + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :lists_subscribed_to => true, + :list_timeline => true, + :list_remove_member => false, + :memberships => true, + :list_subscribers => true, + :subscriptions => true, + :list_subscribe => false, + :list_subscriber? => true, + :list_unsubscribe => false, + :list_add_members => false, + :list_remove_members => false, + :list_member? => true, + :list_members => true, + :list_add_member => false, + :list_destroy => false, + :list_update => false, + :list_create => false, + :lists => true, + :list => true, + ) + end + + # Returns all lists the authenticating or specified user subscribes to, including their own + # + # @see https://dev.twitter.com/docs/api/1/get/lists/all + # @rate_limited Yes + # @authentication_required Supported + # @return [Array] + # @overload lists_subscribed_to(options={}) + # @param options [Hash] A customizable set of options. + # @example Return all lists the authenticating user subscribes to + # Twitter.lists_subscribed_to + # @overload lists_subscribed_to(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @example Return all lists the specified user subscribes to + # Twitter.lists_subscribed_to('sferik') + # Twitter.lists_subscribed_to(8863586) + def lists_subscribed_to(*args) + options = args.extract_options! + if user = args.pop + options.merge_user!(user) + end + response = get("/1/lists/all.json", options) + collection_from_array(response[:body], Twitter::List) + end + + # Show tweet timeline for members of the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/statuses + # @rate_limited Yes + # @authentication_required No + # @return [Array] + # @overload list_timeline(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :per_page The number of results to retrieve. + # @example Show tweet timeline for members of the authenticated user's "presidents" list + # Twitter.list_timeline('presidents') + # Twitter.list_timeline(8863586) + # @overload list_timeline(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :per_page The number of results to retrieve. + # @example Show tweet timeline for members of @sferik's "presidents" list + # Twitter.list_timeline('sferik', 'presidents') + # Twitter.list_timeline('sferik', 8863586) + # Twitter.list_timeline(7505382, 'presidents') + # Twitter.list_timeline(7505382, 8863586) + def list_timeline(*args) + options = args.extract_options! + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = get("/1/lists/statuses.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Removes the specified member from the list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/members/destroy + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The list. + # @overload list_remove_member(list, user_to_remove, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_remove [Integer, String] The user id or screen name of the list member to remove. + # @param options [Hash] A customizable set of options. + # @example Remove @BarackObama from the authenticated user's "presidents" list + # Twitter.list_remove_member('presidents', 813286) + # Twitter.list_remove_member('presidents', 'BarackObama') + # Twitter.list_remove_member(8863586, 'BarackObama') + # @overload list_remove_member(user, list, user_to_remove, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_remove [Integer, String] The user id or screen name of the list member to remove. + # @param options [Hash] A customizable set of options. + # @example Remove @BarackObama from @sferik's "presidents" list + # Twitter.list_remove_member('sferik', 'presidents', 813286) + # Twitter.list_remove_member('sferik', 'presidents', 'BarackObama') + # Twitter.list_remove_member('sferik', 8863586, 'BarackObama') + # Twitter.list_remove_member(7505382, 'presidents', 813286) + def list_remove_member(*args) + options = args.extract_options! + user_to_remove = args.pop + options.merge_user!(user_to_remove) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = post("/1/lists/members/destroy.json", options) + Twitter::List.from_response(response) + end + + # List the lists the specified user has been added to + # + # @see https://dev.twitter.com/docs/api/1/get/lists/memberships + # @rate_limited Yes + # @authentication_required Supported + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload memberships(options={}) + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List the lists the authenticated user has been added to + # Twitter.memberships + # @overload memberships(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List the lists that @sferik has been added to + # Twitter.memberships('sferik') + # Twitter.memberships(7505382) + def memberships(*args) + options = {:cursor => -1}.merge(args.extract_options!) + if user = args.pop + options.merge_user!(user) + end + response = get("/1/lists/memberships.json", options) + Twitter::Cursor.from_response(response, 'lists', Twitter::List) + end + + # Returns the subscribers of the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/subscribers + # @rate_limited Yes + # @authentication_required Supported + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] The subscribers of the specified list. + # @overload list_subscribers(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the subscribers of the authenticated user's "presidents" list + # Twitter.list_subscribers('presidents') + # Twitter.list_subscribers(8863586) + # @overload list_subscribers(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the subscribers of @sferik's "presidents" list + # Twitter.list_subscribers('sferik', 'presidents') + # Twitter.list_subscribers('sferik', 8863586) + # Twitter.list_subscribers(7505382, 'presidents') + def list_subscribers(*args) + options = {:cursor => -1}.merge(args.extract_options!) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = get("/1/lists/subscribers.json", options) + Twitter::Cursor.from_response(response, 'users', Twitter::User) + end + + # List the lists the specified user follows + # + # @see https://dev.twitter.com/docs/api/1/get/lists/subscriptions + # @rate_limited Yes + # @authentication_required Supported + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload subscriptions(options={}) + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List the lists the authenticated user follows + # Twitter.subscriptions + # @overload subscriptions(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List the lists that @sferik follows + # Twitter.subscriptions('sferik') + # Twitter.subscriptions(7505382) + def subscriptions(*args) + options = {:cursor => -1}.merge(args.extract_options!) + if user = args.pop + options.merge_user!(user) + end + response = get("/1/lists/subscriptions.json", options) + Twitter::Cursor.from_response(response, 'lists', Twitter::List) + end + + # Make the authenticated user follow the specified list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/subscribers/create + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The specified list. + # @overload list_subscribe(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Subscribe to the authenticated user's "presidents" list + # Twitter.list_subscribe('presidents') + # Twitter.list_subscribe(8863586) + # @overload list_subscribe(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Subscribe to @sferik's "presidents" list + # Twitter.list_subscribe('sferik', 'presidents') + # Twitter.list_subscribe('sferik', 8863586) + # Twitter.list_subscribe(7505382, 'presidents') + def list_subscribe(*args) + list_from_response(args) do |options| + post("/1/lists/subscribers/create.json", options) + end + end + + # Check if a user is a subscriber of the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/subscribers/show + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Boolean] true if user is a subscriber of the specified list, otherwise false. + # @overload list_subscriber?(list, user_to_check, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @example Check if @BarackObama is a subscriber of the authenticated user's "presidents" list + # Twitter.list_subscriber?('presidents', 813286) + # Twitter.list_subscriber?(8863586, 813286) + # Twitter.list_subscriber?('presidents', 'BarackObama') + # @overload list_subscriber?(user, list, user_to_check, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_check [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @example Check if @BarackObama is a subscriber of @sferik's "presidents" list + # Twitter.list_subscriber?('sferik', 'presidents', 813286) + # Twitter.list_subscriber?('sferik', 8863586, 813286) + # Twitter.list_subscriber?(7505382, 'presidents', 813286) + # Twitter.list_subscriber?('sferik', 'presidents', 'BarackObama') + # @return [Boolean] true if user is a subscriber of the specified list, otherwise false. + def list_subscriber?(*args) + options = args.extract_options! + user_to_check = args.pop + options.merge_user!(user_to_check) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + get("/1/lists/subscribers/show.json", options) + true + rescue Twitter::Error::NotFound, Twitter::Error::Forbidden + false + end + + # Unsubscribes the authenticated user form the specified list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/subscribers/destroy + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The specified list. + # @overload list_unsubscribe(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Unsubscribe from the authenticated user's "presidents" list + # Twitter.list_unsubscribe('presidents') + # Twitter.list_unsubscribe(8863586) + # @overload list_unsubscribe(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Unsubscribe from @sferik's "presidents" list + # Twitter.list_unsubscribe('sferik', 'presidents') + # Twitter.list_unsubscribe('sferik', 8863586) + # Twitter.list_unsubscribe(7505382, 'presidents') + def list_unsubscribe(*args) + list_from_response(args) do |options| + post("/1/lists/subscribers/destroy.json", options) + end + end + + # Adds specified members to a list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/members/create_all + # @note Lists are limited to having 500 members, and you are limited to adding up to 100 members to a list at a time with this method. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The list. + # @overload list_add_members(list, users, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @example Add @BarackObama and @pengwynn to the authenticated user's "presidents" list + # Twitter.list_add_members('presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_add_members('presidents', [813286, 18755393]) + # Twitter.list_add_members(8863586, ['BarackObama', 'pengwynn']) + # Twitter.list_add_members(8863586, [813286, 18755393]) + # @overload list_add_members(user, list, users, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @example Add @BarackObama and @pengwynn to @sferik's "presidents" list + # Twitter.list_add_members('sferik', 'presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_add_members('sferik', 'presidents', [813286, 18755393]) + # Twitter.list_add_members(7505382, 'presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_add_members(7505382, 'presidents', [813286, 18755393]) + # Twitter.list_add_members(7505382, 8863586, ['BarackObama', 'pengwynn']) + # Twitter.list_add_members(7505382, 8863586, [813286, 18755393]) + def list_add_members(*args) + options = args.extract_options! + members = args.pop + options.merge_list!(args.pop) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = members.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users| + post("/1/lists/members/create_all.json", options.merge_users(users)) + end.last + Twitter::List.from_response(response) + end + + # Removes specified members from the list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/members/destroy_all + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The list. + # @overload list_remove_members(list, users, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @example Remove @BarackObama and @pengwynn from the authenticated user's "presidents" list + # Twitter.list_remove_members('presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_remove_members('presidents', [813286, 18755393]) + # Twitter.list_remove_members(8863586, ['BarackObama', 'pengwynn']) + # Twitter.list_remove_members(8863586, [813286, 18755393]) + # @overload list_remove_members(user, list, users, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + # @example Remove @BarackObama and @pengwynn from @sferik's "presidents" list + # Twitter.list_remove_members('sferik', 'presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_remove_members('sferik', 'presidents', [813286, 18755393]) + # Twitter.list_remove_members(7505382, 'presidents', ['BarackObama', 'pengwynn']) + # Twitter.list_remove_members(7505382, 'presidents', [813286, 18755393]) + # Twitter.list_remove_members(7505382, 8863586, ['BarackObama', 'pengwynn']) + # Twitter.list_remove_members(7505382, 8863586, [813286, 18755393]) + def list_remove_members(*args) + options = args.extract_options! + members = args.pop + options.merge_list!(args.pop) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = members.flatten.each_slice(MAX_USERS_PER_REQUEST).threaded_map do |users| + post("/1/lists/members/destroy_all.json", options.merge_users(users)) + end.last + Twitter::List.from_response(response) + end + + # Check if a user is a member of the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/members/show + # @authentication_required Yes + # @rate_limited Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Boolean] true if user is a member of the specified list, otherwise false. + # @overload list_member?(list, user_to_check, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_check [Integer, String] The user ID or screen name of the list member. + # @param options [Hash] A customizable set of options. + # @example Check if @BarackObama is a member of the authenticated user's "presidents" list + # Twitter.list_member?('presidents', 813286) + # Twitter.list_member?(8863586, 'BarackObama') + # @overload list_member?(user, list, user_to_check, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_check [Integer, String] The user ID or screen name of the list member. + # @param options [Hash] A customizable set of options. + # @example Check if @BarackObama is a member of @sferik's "presidents" list + # Twitter.list_member?('sferik', 'presidents', 813286) + # Twitter.list_member?('sferik', 8863586, 'BarackObama') + # Twitter.list_member?(7505382, 'presidents', 813286) + def list_member?(*args) + options = args.extract_options! + user_to_check = args.pop + options.merge_user!(user_to_check) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + get("/1/lists/members/show.json", options) + true + rescue Twitter::Error::NotFound, Twitter::Error::Forbidden + false + end + + # Returns the members of the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/members + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload list_members(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the members of the authenticated user's "presidents" list + # Twitter.list_members('presidents') + # Twitter.list_members(8863586) + # @overload list_members(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example Return the members of @sferik's "presidents" list + # Twitter.list_members('sferik', 'presidents') + # Twitter.list_members('sferik', 8863586) + # Twitter.list_members(7505382, 'presidents') + # Twitter.list_members(7505382, 8863586) + def list_members(*args) + options = {:cursor => -1}.merge(args.extract_options!) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = get("/1/lists/members.json", options) + Twitter::Cursor.from_response(response, 'users', Twitter::User) + end + + # Add a member to a list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/members/create + # @note Lists are limited to having 500 members. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The list. + # @overload list_add_member(list, user_to_add, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_add [Integer, String] The user id or screen name to add to the list. + # @param options [Hash] A customizable set of options. + # @example Add @BarackObama to the authenticated user's "presidents" list + # Twitter.list_add_member('presidents', 813286) + # Twitter.list_add_member(8863586, 813286) + # @overload list_add_member(user, list, user_to_add, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param user_to_add [Integer, String] The user id or screen name to add to the list. + # @param options [Hash] A customizable set of options. + # @example Add @BarackObama to @sferik's "presidents" list + # Twitter.list_add_member('sferik', 'presidents', 813286) + # Twitter.list_add_member('sferik', 8863586, 813286) + # Twitter.list_add_member(7505382, 'presidents', 813286) + # Twitter.list_add_member(7505382, 8863586, 813286) + def list_add_member(*args) + options = args.extract_options! + user_to_add = args.pop + options.merge_user!(user_to_add) + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = post("/1/lists/members/create.json", options) + Twitter::List.from_response(response) + end + + # Deletes the specified list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/destroy + # @note Must be owned by the authenticated user. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The deleted list. + # @overload list_destroy(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Delete the authenticated user's "presidents" list + # Twitter.list_destroy('presidents') + # Twitter.list_destroy(8863586) + # @overload list_destroy(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Delete @sferik's "presidents" list + # Twitter.list_destroy('sferik', 'presidents') + # Twitter.list_destroy('sferik', 8863586) + # Twitter.list_destroy(7505382, 'presidents') + # Twitter.list_destroy(7505382, 8863586) + def list_destroy(*args) + list_from_response(args) do |options| + delete("/1/lists/destroy.json", options) + end + end + + # Updates the specified list + # + # @see https://dev.twitter.com/docs/api/1/post/lists/update + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The created list. + # @overload list_update(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'. + # @option options [String] :description The description to give the list. + # @example Update the authenticated user's "presidents" list to have the description "Presidents of the United States of America" + # Twitter.list_update('presidents', :description => "Presidents of the United States of America") + # Twitter.list_update(8863586, :description => "Presidents of the United States of America") + # @overload list_update(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'. + # @option options [String] :description The description to give the list. + # @example Update the @sferik's "presidents" list to have the description "Presidents of the United States of America" + # Twitter.list_update('sferik', 'presidents', :description => "Presidents of the United States of America") + # Twitter.list_update(7505382, 'presidents', :description => "Presidents of the United States of America") + # Twitter.list_update('sferik', 8863586, :description => "Presidents of the United States of America") + # Twitter.list_update(7505382, 8863586, :description => "Presidents of the United States of America") + def list_update(*args) + list_from_response(args) do |options| + post("/1/lists/update.json", options) + end + end + + # Creates a new list for the authenticated user + # + # @see https://dev.twitter.com/docs/api/1/post/lists/create + # @note Accounts are limited to 20 lists. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The created list. + # @param name [String] The name for the list. + # @param options [Hash] A customizable set of options. + # @option options [String] :mode ('public') Whether your list is public or private. Values can be 'public' or 'private'. + # @option options [String] :description The description to give the list. + # @example Create a list named 'presidents' + # Twitter.list_create('presidents') + def list_create(name, options={}) + response = post("/1/lists/create.json", options.merge(:name => name)) + Twitter::List.from_response(response) + end + + # List the lists of the specified user + # + # @see https://dev.twitter.com/docs/api/1/get/lists + # @note Private lists will be included if the authenticated user is the same as the user whose lists are being returned. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Cursor] + # @overload lists(options={}) + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List the authenticated user's lists + # Twitter.lists + # @overload lists(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :cursor (-1) Breaks the results into pages. Provide values as returned in the response objects's next_cursor and previous_cursor attributes to page back and forth in the list. + # @example List @sferik's lists + # Twitter.lists('sferik') + # Twitter.lists(7505382) + def lists(*args) + options = {:cursor => -1}.merge(args.extract_options!) + user = args.pop + options.merge_user!(user) if user + response = get("/1/lists.json", options) + Twitter::Cursor.from_response(response, 'lists', Twitter::List) + end + + # Show the specified list + # + # @see https://dev.twitter.com/docs/api/1/get/lists/show + # @note Private lists will only be shown if the authenticated user owns the specified list. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::List] The specified list. + # @overload list(list, options={}) + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Show the authenticated user's "presidents" list + # Twitter.list('presidents') + # Twitter.list(8863586) + # @overload list(user, list, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param list [Integer, String, Twitter::List] A Twitter list ID, slug, or object. + # @param options [Hash] A customizable set of options. + # @example Show @sferik's "presidents" list + # Twitter.list('sferik', 'presidents') + # Twitter.list('sferik', 8863586) + # Twitter.list(7505382, 'presidents') + # Twitter.list(7505382, 8863586) + def list(*args) + list_from_response(args) do |options| + get("/1/lists/show.json", options) + end + end + + private + + def list_from_response(args, &block) + options = args.extract_options! + list = args.pop + options.merge_list!(list) + unless options[:owner_id] || options[:owner_screen_name] + owner = args.pop || self.verify_credentials.screen_name + options.merge_owner!(owner) + end + response = yield(options) + Twitter::List.from_response(response) + end + + end + end +end diff --git a/lib/twitter/api/notifications.rb b/lib/twitter/api/notifications.rb new file mode 100644 index 000000000..82d6df8ff --- /dev/null +++ b/lib/twitter/api/notifications.rb @@ -0,0 +1,59 @@ +require 'twitter/api/utils' + +module Twitter + module API + module Notifications + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :enable_notifications => false, + :disable_notifications => false, + ) + end + + # Enables device notifications for updates from the specified users to the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/notifications/follow + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The specified users. + # @overload enable_notifications(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Enable device notifications for updates from @sferik + # Twitter.enable_notifications('sferik') + # Twitter.enable_notifications(7505382) # Same as above + # @overload enable_notifications(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def enable_notifications(*args) + users_from_response(args) do |options| + post("/1/notifications/follow.json", options) + end + end + + # Disables notifications for updates from the specified users to the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/notifications/leave + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The specified users. + # @overload disable_notifications(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Disable device notifications for updates from @sferik + # Twitter.disable_notifications('sferik') + # Twitter.disable_notifications(7505382) # Same as above + # @overload disable_notifications(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def disable_notifications(*args) + users_from_response(args) do |options| + post("/1/notifications/leave.json", options) + end + end + + end + end +end diff --git a/lib/twitter/api/report_spam.rb b/lib/twitter/api/report_spam.rb new file mode 100644 index 000000000..1d7511ef7 --- /dev/null +++ b/lib/twitter/api/report_spam.rb @@ -0,0 +1,36 @@ +require 'twitter/api/utils' + +module Twitter + module API + module ReportSpam + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :report_spam => true, + ) + end + + # The users specified are blocked by the authenticated user and reported as spammers + # + # @see https://dev.twitter.com/docs/api/1/post/report_spam + # @rate_limited Yes + # @authentication_required No + # @return [Array] The reported users. + # @overload report_spam(*users) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @example Report @spam for spam + # Twitter.report_spam("spam") + # Twitter.report_spam(14589771) # Same as above + # @overload report_spam(*users, options) + # @param users [Array, Set] An array of Twitter user IDs, screen names, or objects. + # @param options [Hash] A customizable set of options. + def report_spam(*args) + users_from_response(args) do |options| + post("/1/report_spam.json", options) + end + end + + end + end +end diff --git a/lib/twitter/api/saved_searches.rb b/lib/twitter/api/saved_searches.rb new file mode 100644 index 000000000..6f8ee9ba7 --- /dev/null +++ b/lib/twitter/api/saved_searches.rb @@ -0,0 +1,114 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/array' +require 'twitter/core_ext/enumerable' +require 'twitter/saved_search' + +module Twitter + module API + module SavedSearches + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :saved_searches => true, + :saved_search => true, + :saved_search_create => false, + :saved_search_destroy => false, + ) + end + + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The saved searches. + # @overload saved_search(options={}) + # Returns the authenticated user's saved search queries + # + # @see https://dev.twitter.com/docs/api/1/get/saved_searches + # @param options [Hash] A customizable set of options. + # @example Return the authenticated user's saved search queries + # Twitter.saved_searches + # @overload saved_search(*ids) + # Retrieve the data for saved searches owned by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/saved_searches/show/:id + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Retrieve the data for a saved search owned by the authenticating user with the ID 16129012 + # Twitter.saved_search(16129012) + # @overload saved_search(*ids, options) + # Retrieve the data for saved searches owned by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/saved_searches/show/:id + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def saved_searches(*args) + options = args.extract_options! + if args.empty? + response = get("/1/saved_searches.json", options) + collection_from_array(response[:body], Twitter::SavedSearch) + else + args.flatten.threaded_map do |id| + response = get("/1/saved_searches/show/#{id}.json", options) + Twitter::SavedSearch.from_response(response) + end + end + end + + # Retrieve the data for saved searches owned by the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/saved_searches/show/:id + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::SavedSearch] The saved searches. + # @param id [Integer] A Twitter status IDs. + # @param options [Hash] A customizable set of options. + # @example Retrieve the data for a saved search owned by the authenticating user with the ID 16129012 + # Twitter.saved_search(16129012) + def saved_search(id, options={}) + response = get("/1/saved_searches/show/#{id}.json", options) + Twitter::SavedSearch.from_response(response) + end + + # Creates a saved search for the authenticated user + # + # @see https://dev.twitter.com/docs/api/1/post/saved_searches/create + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::SavedSearch] The created saved search. + # @param query [String] The query of the search the user would like to save. + # @param options [Hash] A customizable set of options. + # @example Create a saved search for the authenticated user with the query "twitter" + # Twitter.saved_search_create("twitter") + def saved_search_create(query, options={}) + response = post("/1/saved_searches/create.json", options.merge(:query => query)) + Twitter::SavedSearch.from_response(response) + end + + # Destroys saved searches for the authenticated user + # + # @see https://dev.twitter.com/docs/api/1/post/saved_searches/destroy/:id + # @note The search specified by ID must be owned by the authenticating user. + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The deleted saved searches. + # @overload saved_search_destroy(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Destroys a saved search for the authenticated user with the ID 16129012 + # Twitter.saved_search_destroy(16129012) + # @overload saved_search_destroy(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def saved_search_destroy(*args) + options = args.extract_options! + args.flatten.threaded_map do |id| + response = delete("/1/saved_searches/destroy/#{id}.json", options) + Twitter::SavedSearch.from_response(response) + end + end + + end + end +end diff --git a/lib/twitter/api/search.rb b/lib/twitter/api/search.rb new file mode 100644 index 000000000..d93413fc4 --- /dev/null +++ b/lib/twitter/api/search.rb @@ -0,0 +1,63 @@ +require 'twitter/api/utils' +require 'twitter/search_results' + +module Twitter + module API + module Search + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :search => true, + :phoenix_search => true, + ) + 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. + # @rate_limited Yes + # @authentication_required 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. Options are "mixed", "recent", and "popular". 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 [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Boolean, String, Integer] :with_twitter_user_id When set to either true, t or 1, the from_user_id and to_user_id values in the response will map to "official" user IDs which will match those returned by the REST API. + # @return [Twitter::SearchResults] Return tweets that match a specified query with search metadata + # @example Returns tweets related to twitter + # Twitter.search('twitter') + def search(q, options={}) + response = get("/search.json", options.merge(:q => q), :endpoint => @search_endpoint) + Twitter::SearchResults.from_response(response) + end + + # Returns recent statuses related to a query with images and videos embedded + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required No + # @param q [String] A search term. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @return [Array] An array of statuses that contain videos + # @example Return recent statuses related to twitter with images and videos embedded + # Twitter.phoenix_search('twitter') + def phoenix_search(q, options={}) + response = get("/phoenix_search.phoenix", options.merge(:q => q)) + collection_from_array(response[:body][:statuses], Twitter::Status) + end + + end + end +end diff --git a/lib/twitter/api/statuses.rb b/lib/twitter/api/statuses.rb new file mode 100644 index 000000000..b92fc3580 --- /dev/null +++ b/lib/twitter/api/statuses.rb @@ -0,0 +1,603 @@ +require 'twitter/api/utils' +require 'twitter/core_ext/array' +require 'twitter/core_ext/enumerable' +require 'twitter/core_ext/hash' +require 'twitter/status' + +module Twitter + module API + module Statuses + include Twitter::API::Utils + + def self.included(klass) + klass.class_variable_get(:@@rate_limited).merge!( + :favorites => true, + :favorite => false, + :fav => false, + :fave => false, + :favorite_create => false, + :unfavorite => false, + :favorite_destroy => false, + :home_timeline => true, + :mentions => true, + :retweeted_by => true, + :retweeted_to => true, + :retweets_of_me => true, + :user_timeline => true, + :media_timeline => true, + :network_timeline => true, + :retweeters_of => true, + :retweets => true, + :status => true, + :statuses => true, + :status_activity => true, + :statuses_activity => true, + :oembed => true, + :oembeds => true, + :status_destroy => false, + :retweet => true, + :update => false, + :update_with_media => false, + ) + end + + + # @see https://dev.twitter.com/docs/api/1/get/favorites + # @rate_limited Yes + # @authentication_required No + # @return [Array] favorite statuses. + # @overload favorites(options={}) + # Returns the 20 most recent favorite statuses for the authenticating user + # + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @example Return the 20 most recent favorite statuses for the authenticating user + # Twitter.favorites + # @overload favorites(user, options={}) + # Returns the 20 most recent favorite statuses for the specified user + # + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @example Return the 20 most recent favorite statuses for @sferik + # Twitter.favorites('sferik') + def favorites(*args) + options = args.extract_options! + response = if user = args.pop + get("/1/favorites/#{user}.json", options) + else + get("/1/favorites.json", options) + end + collection_from_array(response[:body], Twitter::Status) + end + + # Favorites the specified statuses as the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/favorites/create/:id + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The favorited statuses. + # @overload favorite(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Favorite the status with the ID 25938088801 + # Twitter.favorite(25938088801) + # @overload favorite(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def favorite(*args) + statuses_from_response(args) do |id, options| + post("/1/favorites/create/#{id}.json", options) + end + end + alias fav favorite + alias fave favorite + alias favorite_create favorite + + # Un-favorites the specified statuses as the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/post/favorites/destroy/:id + # @rate_limited No + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The un-favorited statuses. + # @overload unfavorite(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Un-favorite the status with the ID 25938088801 + # Twitter.unfavorite(25938088801) + # @overload unfavorite(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def unfavorite(*args) + statuses_from_response(args) do |id, options| + delete("/1/favorites/destroy/#{id}.json", options) + end + end + alias favorite_destroy unfavorite + + # Returns the 20 most recent statuses, including retweets if they exist, posted by the authenticating user and the users they follow + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/home_timeline + # @note This method can only return up to 800 statuses, including retweets. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies. + # @example Return the 20 most recent statuses, including retweets if they exist, posted by the authenticating user and the users they follow + # Twitter.home_timeline + def home_timeline(options={}) + response = get("/1/statuses/home_timeline.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent mentions (statuses containing @username) for the authenticating user + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/mentions + # @note This method can only return up to 800 statuses. + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent mentions (statuses containing @username) for the authenticating user + # Twitter.mentions + def mentions(options={}) + response = get("/1/statuses/mentions.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent retweets posted by the specified user + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweeted_by_me + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweeted_by_user + # @rate_limited Yes + # @authentication_required Supported + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @overload retweeted_by(options={}) + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent retweets posted by the authenticating user + # Twitter.retweeted_by('sferik') + # @overload retweeted_by(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent retweets posted by the authenticating user + # Twitter.retweeted_by + def retweeted_by(*args) + options = args.extract_options! + response = if user = args.pop + options.merge_user!(user) + get("/1/statuses/retweeted_by_user.json", options) + else + get("/1/statuses/retweeted_by_me.json", options) + end + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent retweets posted by users the specified user follows + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweeted_to_me + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweeted_to_user + # @rate_limited Yes + # @authentication_required Supported + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @overload retweeted_to(options={}) + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent retweets posted by users followed by the authenticating user + # Twitter.retweeted_to + # @overload retweeted_to(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent retweets posted by users followed by the authenticating user + # Twitter.retweeted_to('sferik') + def retweeted_to(*args) + options = args.extract_options! + response = if user = args.pop + options.merge_user!(user) + get("/1/statuses/retweeted_to_user.json", options) + else + get("/1/statuses/retweeted_to_me.json", options) + end + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent tweets of the authenticated user that have been retweeted by others + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweets_of_me + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the 20 most recent tweets of the authenticated user that have been retweeted by others + # Twitter.retweets_of_me + def retweets_of_me(options={}) + response = get("/1/statuses/retweets_of_me.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent statuses posted by the specified user + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/user_timeline + # @note This method can only return up to 3200 statuses. + # @rate_limited Yes + # @authentication_required No, unless the user whose timeline you're trying to view is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @overload user_timeline(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies. + # @example Return the 20 most recent statuses posted by @sferik + # Twitter.user_timeline('sferik') + def user_timeline(*args) + options = args.extract_options! + if user = args.pop + options.merge_user!(user) + end + response = get("/1/statuses/user_timeline.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent images posted by the specified user + # + # @see https://support.twitter.com/articles/20169409 + # @note This method can only return up to the 100 most recent images. + # @note Images will not be returned from tweets posted before January 1, 2010. + # @rate_limited Yes + # @authentication_required No, unless the user whose timeline you're trying to view is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @overload media_timeline(user, options={}) + # @param user [Integer, String, Twitter::User] A Twitter user ID, screen name, or object. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean] :filter Include possibly sensitive media when set to false, 'f' or 0. + # @example Return the 20 most recent statuses posted by @sferik + # Twitter.media_timeline('sferik') + def media_timeline(*args) + options = args.extract_options! + if user = args.pop + options.merge_user!(user) + end + response = get("/1/statuses/media_timeline.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns the 20 most recent statuses from the authenticating user's network + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param options [Hash] A customizable set of options. + # @option options [Integer] :since_id Returns results with an ID greater than (that is, more recent than) the specified ID. + # @option options [Integer] :max_id Returns results with an ID less than (that is, older than) or equal to the specified ID. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 200. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @option options [Boolean, String, Integer] :exclude_replies This parameter will prevent replies from appearing in the returned timeline. Using exclude_replies with the count parameter will mean you will receive up-to count tweets - this is because the count parameter retrieves that many tweets before filtering out retweets and replies. + # @example Return the 20 most recent statuses from the authenticating user's network + # Twitter.network_timeline + def network_timeline(options={}) + response = get("/i/statuses/network_timeline.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Show up to 100 users who retweeted the status + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/:id/retweeted_by + # @see https://dev.twitter.com/docs/api/1/get/statuses/:id/retweeted_by/ids + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param id [Integer] The numerical ID of the desired status. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Integer] :page Specifies the page of results to retrieve. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @option options [Boolean] :ids_only ('false') Only return user ids instead of full user objects. + # @example Show up to 100 users who retweeted the status with the ID 28561922516 + # Twitter.retweeters_of(28561922516) + def retweeters_of(id, options={}) + if ids_only = !!options.delete(:ids_only) + get("/1/statuses/#{id}/retweeted_by/ids.json", options)[:body] + else + response = get("/1/statuses/#{id}/retweeted_by.json", options) + collection_from_array(response[:body], Twitter::User) + end + end + + # Returns up to 100 of the first retweets of a given tweet + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/retweets/:id + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] + # @param id [Integer] The numerical ID of the desired status. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :count Specifies the number of records to retrieve. Must be less than or equal to 100. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return up to 100 of the first retweets of the status with the ID 28561922516 + # Twitter.retweets(28561922516) + def retweets(id, options={}) + response = get("/1/statuses/retweets/#{id}.json", options) + collection_from_array(response[:body], Twitter::Status) + end + + # Returns a status + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/show/:id + # @rate_limited Yes + # @authentication_required No, unless the author of the status is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Status] The requested status. + # @param id [Integer] A Twitter status ID. + # @param options [Hash] A customizable set of options. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + # @example Return the status with the ID 25938088801 + # Twitter.status(25938088801) + def status(id, options={}) + response = get("/1/statuses/show/#{id}.json", options) + Twitter::Status.from_response(response) + end + + # Returns statuses + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/show/:id + # @rate_limited Yes + # @authentication_required No, unless the author of the status is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The requested statuses. + # @overload statuses(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Return the status with the ID 25938088801 + # Twitter.statuses(25938088801) + # @overload statuses(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + # @option options [Boolean, String, Integer] :trim_user Each tweet returned in a timeline will include a user object with only the author's numerical ID when set to true, 't' or 1. + def statuses(*args) + statuses_from_response(args) do |id, options| + get("/1/statuses/show/#{id}.json", options) + end + end + + # Returns activity summary for a status + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::Status] The requested status. + # @param id [Integer] A Twitter status ID. + # @param options [Hash] A customizable set of options. + # @example Return activity summary for the status with the ID 25938088801 + # Twitter.status_activity(25938088801) + def status_activity(id, options={}) + response = get("/i/statuses/#{id}/activity/summary.json", options) + response[:body].merge!(:id => id) if response[:body] + Twitter::Status.from_response(response) + end + + # Returns activity summary for statuses + # + # @note Undocumented + # @rate_limited Yes + # @authentication_required Yes + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Array] The requested statuses. + # @overload statuses_activity(*ids) + # @param ids [Array, Set] An array of Twitter status IDs. + # @example Return activity summary for the status with the ID 25938088801 + # Twitter.statuses_activity(25938088801) + # @overload statuses_activity(*ids, options) + # @param ids [Array, Set] An array of Twitter status IDs. + # @param options [Hash] A customizable set of options. + def statuses_activity(*args) + options = args.extract_options! + args.flatten.threaded_map do |id| + status_activity(id, options) + end + end + + # Returns oEmbed for status + # + # @see https://dev.twitter.com/docs/api/1/get/statuses/oembed + # @rate_limited Yes + # @authentication_required No, unless the author of the status is protected + # @raise [Twitter::Error::Unauthorized] Error raised when supplied user credentials are not valid. + # @return [Twitter::OEmbed] OEmbed for the requested status. + # @param id [Integer, String] A Twitter status ID. + # @param options [Hash] A customizable set of options. + # @option options [Integer] :maxwidth The maximum width in pixels that the embed should be rendered at. This value is constrained to be between 250 and 550 pixels. + # @option options [Boolean, String, Integer] :hide_media Specifies whether the embedded Tweet should automatically expand images which were uploaded via {https://dev.twitter.com/docs/api/1/post/statuses/update_with_media POST statuses/update_with_media}. When set to either true, t or 1 images will not be expanded. Defaults to false. + # @option options [Boolean, String, Integer] :hide_thread Specifies whether the embedded Tweet should automatically show the original message in the case that the embedded Tweet is a reply. When set to either true, t or 1 the original Tweet will not be shown. Defaults to false. + # @option options [Boolean, String, Integer] :omit_script Specifies whether the embedded Tweet HTML should include a `