ifttt-twitter 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/.yardopts +8 -0
- data/Gemfile +7 -0
- data/HISTORY.md +520 -0
- data/LICENSE.md +20 -0
- data/README.md +251 -0
- data/Rakefile +13 -0
- data/lib/twitter.rb +24 -0
- data/lib/twitter/action.rb +9 -0
- data/lib/twitter/action_factory.rb +28 -0
- data/lib/twitter/authenticatable.rb +24 -0
- data/lib/twitter/base.rb +40 -0
- data/lib/twitter/client.rb +126 -0
- data/lib/twitter/client/accounts.rb +180 -0
- data/lib/twitter/client/activity.rb +48 -0
- data/lib/twitter/client/block.rb +104 -0
- data/lib/twitter/client/direct_messages.rb +110 -0
- data/lib/twitter/client/favorites.rb +82 -0
- data/lib/twitter/client/friends_and_followers.rb +293 -0
- data/lib/twitter/client/help.rb +38 -0
- data/lib/twitter/client/legal.rb +32 -0
- data/lib/twitter/client/lists.rb +643 -0
- data/lib/twitter/client/local_trends.rb +46 -0
- data/lib/twitter/client/notification.rb +49 -0
- data/lib/twitter/client/places_and_geo.rb +113 -0
- data/lib/twitter/client/saved_searches.rb +73 -0
- data/lib/twitter/client/search.rb +94 -0
- data/lib/twitter/client/spam_reporting.rb +30 -0
- data/lib/twitter/client/suggested_users.rb +60 -0
- data/lib/twitter/client/timelines.rb +265 -0
- data/lib/twitter/client/trends.rb +55 -0
- data/lib/twitter/client/tweets.rb +191 -0
- data/lib/twitter/client/urls.rb +26 -0
- data/lib/twitter/client/users.rb +203 -0
- data/lib/twitter/config.rb +106 -0
- data/lib/twitter/configuration.rb +20 -0
- data/lib/twitter/connection.rb +43 -0
- data/lib/twitter/core_ext/hash.rb +81 -0
- data/lib/twitter/creatable.rb +14 -0
- data/lib/twitter/cursor.rb +44 -0
- data/lib/twitter/direct_message.rb +27 -0
- data/lib/twitter/error.rb +37 -0
- data/lib/twitter/error/bad_gateway.rb +7 -0
- data/lib/twitter/error/bad_request.rb +7 -0
- data/lib/twitter/error/client_error.rb +7 -0
- data/lib/twitter/error/enhance_your_calm.rb +13 -0
- data/lib/twitter/error/forbidden.rb +7 -0
- data/lib/twitter/error/internal_server_error.rb +7 -0
- data/lib/twitter/error/not_acceptable.rb +7 -0
- data/lib/twitter/error/not_found.rb +7 -0
- data/lib/twitter/error/server_error.rb +7 -0
- data/lib/twitter/error/service_unavailable.rb +7 -0
- data/lib/twitter/error/unauthorized.rb +7 -0
- data/lib/twitter/favorite.rb +28 -0
- data/lib/twitter/follow.rb +27 -0
- data/lib/twitter/geo_factory.rb +22 -0
- data/lib/twitter/language.rb +7 -0
- data/lib/twitter/list.rb +22 -0
- data/lib/twitter/list_member_added.rb +36 -0
- data/lib/twitter/media_factory.rb +21 -0
- data/lib/twitter/mention.rb +43 -0
- data/lib/twitter/metadata.rb +7 -0
- data/lib/twitter/oembed.rb +8 -0
- data/lib/twitter/photo.rb +24 -0
- data/lib/twitter/place.rb +36 -0
- data/lib/twitter/point.rb +27 -0
- data/lib/twitter/polygon.rb +14 -0
- data/lib/twitter/rate_limit_status.rb +15 -0
- data/lib/twitter/relationship.rb +18 -0
- data/lib/twitter/reply.rb +36 -0
- data/lib/twitter/request.rb +38 -0
- data/lib/twitter/request/gateway.rb +20 -0
- data/lib/twitter/request/multipart_with_file.rb +37 -0
- data/lib/twitter/request/oauth.rb +26 -0
- data/lib/twitter/request/phoenix.rb +22 -0
- data/lib/twitter/response/parse_json.rb +28 -0
- data/lib/twitter/response/raise_client_error.rb +49 -0
- data/lib/twitter/response/raise_server_error.rb +23 -0
- data/lib/twitter/retweet.rb +36 -0
- data/lib/twitter/saved_search.rb +16 -0
- data/lib/twitter/settings.rb +17 -0
- data/lib/twitter/size.rb +16 -0
- data/lib/twitter/status.rb +80 -0
- data/lib/twitter/suggestion.rb +22 -0
- data/lib/twitter/trend.rb +14 -0
- data/lib/twitter/user.rb +65 -0
- data/lib/twitter/version.rb +30 -0
- data/spec/faraday/request_spec.rb +25 -0
- data/spec/faraday/response_spec.rb +68 -0
- data/spec/fixtures/about_me.json +1 -0
- data/spec/fixtures/all.json +1 -0
- data/spec/fixtures/bad_gateway.json +1 -0
- data/spec/fixtures/bad_request.json +1 -0
- data/spec/fixtures/by_friends.json +1 -0
- data/spec/fixtures/category.json +1 -0
- data/spec/fixtures/configuration.json +1 -0
- data/spec/fixtures/contributees.json +1 -0
- data/spec/fixtures/contributors.json +1 -0
- data/spec/fixtures/direct_message.json +1 -0
- data/spec/fixtures/direct_messages.json +1 -0
- data/spec/fixtures/end_session.json +1 -0
- data/spec/fixtures/enhance_your_calm.text +11 -0
- data/spec/fixtures/false.json +1 -0
- data/spec/fixtures/favorites.json +1 -0
- data/spec/fixtures/forbidden.json +1 -0
- data/spec/fixtures/friendships.json +1 -0
- data/spec/fixtures/id_list.json +1 -0
- data/spec/fixtures/ids.json +1 -0
- data/spec/fixtures/image_facets.json +1 -0
- data/spec/fixtures/internal_server_error.json +1 -0
- data/spec/fixtures/languages.json +1 -0
- data/spec/fixtures/list.json +1 -0
- data/spec/fixtures/lists.json +1 -0
- data/spec/fixtures/locations.json +1 -0
- data/spec/fixtures/matching_trends.json +1 -0
- data/spec/fixtures/me.jpeg +0 -0
- data/spec/fixtures/media_timeline.json +1 -0
- data/spec/fixtures/members.json +1 -0
- data/spec/fixtures/n605431196_2079896_558_normal.jpg +0 -0
- data/spec/fixtures/no_user_matches.json +1 -0
- data/spec/fixtures/not_acceptable.json +1 -0
- data/spec/fixtures/not_found.json +1 -0
- data/spec/fixtures/oembed.json +1 -0
- data/spec/fixtures/pengwynn.json +1 -0
- data/spec/fixtures/phoenix_search.phoenix +1 -0
- data/spec/fixtures/place.json +1 -0
- data/spec/fixtures/places.json +1 -0
- data/spec/fixtures/privacy.json +1 -0
- data/spec/fixtures/profile_image.text +24 -0
- data/spec/fixtures/rate_limit_status.json +1 -0
- data/spec/fixtures/recommendations.json +1 -0
- data/spec/fixtures/relationship.json +1 -0
- data/spec/fixtures/resolve.json +1 -0
- data/spec/fixtures/retweet.json +1 -0
- data/spec/fixtures/retweeters_of.json +1 -0
- data/spec/fixtures/retweets.json +1 -0
- data/spec/fixtures/saved_search.json +1 -0
- data/spec/fixtures/saved_searches.json +1 -0
- data/spec/fixtures/search.json +1 -0
- data/spec/fixtures/service_unavailable.json +1 -0
- data/spec/fixtures/settings.json +1 -0
- data/spec/fixtures/sferik.json +1 -0
- data/spec/fixtures/status.json +1 -0
- data/spec/fixtures/status_with_media.json +104 -0
- data/spec/fixtures/statuses.json +1 -0
- data/spec/fixtures/suggestions.json +1 -0
- data/spec/fixtures/tos.json +1 -0
- data/spec/fixtures/totals.json +1 -0
- data/spec/fixtures/trends.json +1 -0
- data/spec/fixtures/trends_current.json +1 -0
- data/spec/fixtures/trends_daily.json +1 -0
- data/spec/fixtures/trends_weekly.json +1 -0
- data/spec/fixtures/true.json +1 -0
- data/spec/fixtures/unauthorized.json +1 -0
- data/spec/fixtures/user_search.json +1 -0
- data/spec/fixtures/user_timeline.json +1 -0
- data/spec/fixtures/users.json +1 -0
- data/spec/fixtures/users_list.json +1 -0
- data/spec/fixtures/video_facets.json +1 -0
- data/spec/fixtures/we_concept_bg2.png +0 -0
- data/spec/helper.rb +49 -0
- data/spec/twitter/action_factory_spec.rb +37 -0
- data/spec/twitter/action_spec.rb +16 -0
- data/spec/twitter/base_spec.rb +29 -0
- data/spec/twitter/client/accounts_spec.rb +181 -0
- data/spec/twitter/client/activity_spec.rb +43 -0
- data/spec/twitter/client/block_spec.rb +106 -0
- data/spec/twitter/client/direct_messages_spec.rb +98 -0
- data/spec/twitter/client/favorites_spec.rb +80 -0
- data/spec/twitter/client/friends_and_followers_spec.rb +478 -0
- data/spec/twitter/client/help_spec.rb +44 -0
- data/spec/twitter/client/legal_spec.rb +41 -0
- data/spec/twitter/client/lists_spec.rb +1104 -0
- data/spec/twitter/client/local_trends_spec.rb +58 -0
- data/spec/twitter/client/notification_spec.rb +47 -0
- data/spec/twitter/client/places_and_geo_spec.rb +100 -0
- data/spec/twitter/client/saved_searches_spec.rb +80 -0
- data/spec/twitter/client/search_spec.rb +92 -0
- data/spec/twitter/client/spam_reporting_spec.rb +28 -0
- data/spec/twitter/client/suggested_users_spec.rb +65 -0
- data/spec/twitter/client/timelines_spec.rb +231 -0
- data/spec/twitter/client/trends_spec.rb +51 -0
- data/spec/twitter/client/tweets_spec.rb +189 -0
- data/spec/twitter/client/urls_spec.rb +31 -0
- data/spec/twitter/client/users_spec.rb +323 -0
- data/spec/twitter/client_spec.rb +94 -0
- data/spec/twitter/configuration_spec.rb +17 -0
- data/spec/twitter/cursor_spec.rb +43 -0
- data/spec/twitter/direct_message_spec.rb +56 -0
- data/spec/twitter/favorite_spec.rb +29 -0
- data/spec/twitter/follow_spec.rb +29 -0
- data/spec/twitter/geo_factory_spec.rb +21 -0
- data/spec/twitter/list_spec.rb +34 -0
- data/spec/twitter/media_factory_spec.rb +17 -0
- data/spec/twitter/mention_spec.rb +52 -0
- data/spec/twitter/oembed_spec.rb +145 -0
- data/spec/twitter/photo_spec.rb +35 -0
- data/spec/twitter/place_spec.rb +75 -0
- data/spec/twitter/point_spec.rb +32 -0
- data/spec/twitter/polygon_spec.rb +18 -0
- data/spec/twitter/rate_limit_status_spec.rb +16 -0
- data/spec/twitter/relationship_spec.rb +27 -0
- data/spec/twitter/reply_spec.rb +41 -0
- data/spec/twitter/retweet_spec.rb +41 -0
- data/spec/twitter/saved_search_spec.rb +34 -0
- data/spec/twitter/settings_spec.rb +16 -0
- data/spec/twitter/size_spec.rb +18 -0
- data/spec/twitter/status_spec.rb +148 -0
- data/spec/twitter/suggestion_spec.rb +20 -0
- data/spec/twitter/trend_spec.rb +20 -0
- data/spec/twitter/user_spec.rb +50 -0
- data/spec/twitter_spec.rb +91 -0
- data/twitter.gemspec +42 -0
- metadata +564 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'active_support/core_ext/enumerable'
|
2
|
+
require 'twitter/base'
|
3
|
+
require 'twitter/size'
|
4
|
+
|
5
|
+
module Twitter
|
6
|
+
class Photo < Twitter::Base
|
7
|
+
lazy_attr_reader :display_url, :expanded_url, :id, :indices, :media_url,
|
8
|
+
:media_url_https, :url
|
9
|
+
|
10
|
+
# @param other [Twiter::Photo]
|
11
|
+
# @return [Boolean]
|
12
|
+
def ==(other)
|
13
|
+
super || (other.class == self.class && other.id == self.id)
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Array<Twitter::Size>]
|
17
|
+
def sizes
|
18
|
+
@sizes ||= Array(@attrs['sizes']).each_with_object({}) do |(key, value), object|
|
19
|
+
object[key] = Twitter::Size.new(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'twitter/base'
|
2
|
+
require 'twitter/geo_factory'
|
3
|
+
|
4
|
+
module Twitter
|
5
|
+
class Place < Twitter::Base
|
6
|
+
lazy_attr_reader :attributes, :country, :full_name, :id, :name, :url,
|
7
|
+
:woeid
|
8
|
+
|
9
|
+
# @param other [Twiter::Place]
|
10
|
+
# @return [Boolean]
|
11
|
+
def ==(other)
|
12
|
+
super || (other.class == self.class && other.id == self.id)
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Twitter::Point, Twitter::Polygon]
|
16
|
+
def bounding_box
|
17
|
+
@bounding_box ||= Twitter::GeoFactory.new(@attrs['bounding_box']) unless @attrs['bounding_box'].nil?
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [String]
|
21
|
+
def country_code
|
22
|
+
@country_code ||= @attrs['country_code'] || @attrs['countryCode']
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [Integer]
|
26
|
+
def parent_id
|
27
|
+
@parent_id ||= @attrs['parentid']
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [String]
|
31
|
+
def place_type
|
32
|
+
@place_type ||= @attrs['place_type'] || @attrs['placeType'] && @attrs['placeType']['name']
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'twitter/base'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
class Point < Twitter::Base
|
5
|
+
lazy_attr_reader :coordinates
|
6
|
+
|
7
|
+
# @param other [Twiter::Point]
|
8
|
+
# @return [Boolean]
|
9
|
+
def ==(other)
|
10
|
+
super || (other.class == self.class && other.coordinates == self.coordinates)
|
11
|
+
end
|
12
|
+
|
13
|
+
# @return [Integer]
|
14
|
+
def latitude
|
15
|
+
coordinates[0]
|
16
|
+
end
|
17
|
+
alias :lat :latitude
|
18
|
+
|
19
|
+
# @return [Integer]
|
20
|
+
def longitude
|
21
|
+
coordinates[1]
|
22
|
+
end
|
23
|
+
alias :long :longitude
|
24
|
+
alias :lng :longitude
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'twitter/base'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
class Polygon < Twitter::Base
|
5
|
+
lazy_attr_reader :coordinates
|
6
|
+
|
7
|
+
# @param other [Twiter::Polygon]
|
8
|
+
# @return [Boolean]
|
9
|
+
def ==(other)
|
10
|
+
super || (other.class == self.class && other.coordinates == self.coordinates)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'twitter/base'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
class RateLimitStatus < Twitter::Base
|
5
|
+
lazy_attr_reader :hourly_limit, :remaining_hits, :reset_time_in_seconds
|
6
|
+
|
7
|
+
# Time when the authenticating user's rate limit will be reset
|
8
|
+
#
|
9
|
+
# @return [Time]
|
10
|
+
def reset_time
|
11
|
+
@reset_time ||= Time.parse(@attrs['reset_time']) unless @attrs['reset_time'].nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'twitter/base'
|
2
|
+
require 'twitter/user'
|
3
|
+
|
4
|
+
module Twitter
|
5
|
+
class Relationship < Twitter::Base
|
6
|
+
|
7
|
+
# @return [Twitter::User]
|
8
|
+
def source
|
9
|
+
@source ||= Twitter::User.new(@attrs['source']) unless @attrs['source'].nil?
|
10
|
+
end
|
11
|
+
|
12
|
+
# @return [Twitter::User]
|
13
|
+
def target
|
14
|
+
@target ||= Twitter::User.new(@attrs['target']) unless @attrs['target'].nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'twitter/action'
|
2
|
+
require 'twitter/status'
|
3
|
+
require 'twitter/user'
|
4
|
+
|
5
|
+
module Twitter
|
6
|
+
class Reply < Twitter::Action
|
7
|
+
|
8
|
+
# A collection of users who replies to a user
|
9
|
+
#
|
10
|
+
# @return [Array<Twitter::User>]
|
11
|
+
def sources
|
12
|
+
@sources = Array(@attrs['sources']).map do |user|
|
13
|
+
Twitter::User.new(user)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# A collection of statuses that reply to a user
|
18
|
+
#
|
19
|
+
# @return [Array<Twitter::Status>]
|
20
|
+
def target_objects
|
21
|
+
@target_objects = Array(@attrs['target_objects']).map do |status|
|
22
|
+
Twitter::Status.new(status)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# A collection that contains the replied-to status
|
27
|
+
#
|
28
|
+
# @return [Array<Twitter::Status>]
|
29
|
+
def targets
|
30
|
+
@targets = Array(@attrs['targets']).map do |status|
|
31
|
+
Twitter::Status.new(status)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Twitter
|
2
|
+
# Defines HTTP request methods
|
3
|
+
module Request
|
4
|
+
# Perform an HTTP DELETE request
|
5
|
+
def delete(path, params={}, options={})
|
6
|
+
request(:delete, path, params, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Perform an HTTP GET request
|
10
|
+
def get(path, params={}, options={})
|
11
|
+
request(:get, path, params, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Perform an HTTP POST request
|
15
|
+
def post(path, params={}, options={})
|
16
|
+
request(:post, path, params, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
# Perform an HTTP request
|
22
|
+
def request(method, path, params, options)
|
23
|
+
response = connection(options).run_request(method, nil, nil, nil) do |request|
|
24
|
+
request.options[:phoenix] = true if options[:phoenix]
|
25
|
+
request.options[:raw] = true if options[:raw]
|
26
|
+
case method.to_sym
|
27
|
+
when :delete, :get
|
28
|
+
request.url(path, params)
|
29
|
+
when :post
|
30
|
+
request.path = path
|
31
|
+
request.body = params unless params.empty?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
options[:raw] ? response : response.body
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
module Request
|
5
|
+
class Gateway < Faraday::Middleware
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
url = env[:url].dup
|
9
|
+
url.host = @gateway
|
10
|
+
env[:url] = url
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(app, gateway)
|
15
|
+
@app, @gateway = app, gateway
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
module Request
|
5
|
+
class MultipartWithFile < Faraday::Middleware
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
if env[:body].is_a?(Hash)
|
9
|
+
env[:body].each do |key, value|
|
10
|
+
if value.is_a?(File)
|
11
|
+
env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
|
12
|
+
elsif value.is_a?(Hash) && (value['io'].is_a?(IO) || value['io'].is_a?(StringIO))
|
13
|
+
env[:body][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def mime_type(path)
|
23
|
+
case path
|
24
|
+
when /\.jpe?g/i
|
25
|
+
'image/jpeg'
|
26
|
+
when /\.gif$/i
|
27
|
+
'image/gif'
|
28
|
+
when /\.png$/i
|
29
|
+
'image/png'
|
30
|
+
else
|
31
|
+
'application/octet-stream'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'simple_oauth'
|
3
|
+
|
4
|
+
module Twitter
|
5
|
+
module Request
|
6
|
+
class TwitterOAuth < Faraday::Middleware
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
params = env[:body] || {}
|
10
|
+
signature_params = params
|
11
|
+
params.each do |key, value|
|
12
|
+
signature_params = {} if value.respond_to?(:content_type)
|
13
|
+
end
|
14
|
+
header = SimpleOAuth::Header.new(env[:method], env[:url], signature_params, @options)
|
15
|
+
env[:request_headers]['Authorization'] = header.to_s
|
16
|
+
|
17
|
+
@app.call(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(app, options)
|
21
|
+
@app, @options = app, options
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Twitter
|
4
|
+
module Request
|
5
|
+
class Phoenix < Faraday::Middleware
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
# Not sure what what the X-Phx (Phoenix?) header is for but it's
|
9
|
+
# required to access certain undocumented resources
|
10
|
+
# e.g. GET urls/resolve
|
11
|
+
env[:request_headers]['X-Phx'] = 'true' if env[:request][:phoenix]
|
12
|
+
|
13
|
+
@app.call(env)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(app)
|
17
|
+
@app = app
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module Twitter
|
5
|
+
module Response
|
6
|
+
class ParseJson < Faraday::Response::Middleware
|
7
|
+
|
8
|
+
def parse(body)
|
9
|
+
case body
|
10
|
+
when ''
|
11
|
+
nil
|
12
|
+
when 'true'
|
13
|
+
true
|
14
|
+
when 'false'
|
15
|
+
false
|
16
|
+
else
|
17
|
+
::MultiJson.decode(body)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_complete(env)
|
22
|
+
if respond_to? :parse
|
23
|
+
env[:body] = parse(env[:body]) unless env[:request][:raw] or [204,304].index env[:status]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'twitter/error/bad_request'
|
3
|
+
require 'twitter/error/enhance_your_calm'
|
4
|
+
require 'twitter/error/forbidden'
|
5
|
+
require 'twitter/error/not_acceptable'
|
6
|
+
require 'twitter/error/not_found'
|
7
|
+
require 'twitter/error/unauthorized'
|
8
|
+
|
9
|
+
module Twitter
|
10
|
+
module Response
|
11
|
+
class RaiseClientError < Faraday::Response::Middleware
|
12
|
+
|
13
|
+
def on_complete(env)
|
14
|
+
case env[:status].to_i
|
15
|
+
when 400
|
16
|
+
raise Twitter::Error::BadRequest.new(error_body(env[:body]), env[:response_headers])
|
17
|
+
when 401
|
18
|
+
raise Twitter::Error::Unauthorized.new(error_body(env[:body]), env[:response_headers])
|
19
|
+
when 403
|
20
|
+
raise Twitter::Error::Forbidden.new(error_body(env[:body]), env[:response_headers])
|
21
|
+
when 404
|
22
|
+
raise Twitter::Error::NotFound.new(error_body(env[:body]), env[:response_headers])
|
23
|
+
when 406
|
24
|
+
raise Twitter::Error::NotAcceptable.new(error_body(env[:body]), env[:response_headers])
|
25
|
+
when 420
|
26
|
+
raise Twitter::Error::EnhanceYourCalm.new(error_body(env[:body]), env[:response_headers])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def error_body(body)
|
33
|
+
if body.nil?
|
34
|
+
''
|
35
|
+
elsif body['error']
|
36
|
+
body['error']
|
37
|
+
elsif body['errors']
|
38
|
+
first = Array(body['errors']).first
|
39
|
+
if first.kind_of?(Hash)
|
40
|
+
first['message'].chomp
|
41
|
+
else
|
42
|
+
first.chomp
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'twitter/error/bad_gateway'
|
3
|
+
require 'twitter/error/internal_server_error'
|
4
|
+
require 'twitter/error/service_unavailable'
|
5
|
+
|
6
|
+
module Twitter
|
7
|
+
module Response
|
8
|
+
class RaiseServerError < Faraday::Response::Middleware
|
9
|
+
|
10
|
+
def on_complete(env)
|
11
|
+
case env[:status].to_i
|
12
|
+
when 500
|
13
|
+
raise Twitter::Error::InternalServerError.new("Something is technically wrong.", env[:response_headers])
|
14
|
+
when 502
|
15
|
+
raise Twitter::Error::BadGateway.new("Twitter is down or being upgraded.", env[:response_headers])
|
16
|
+
when 503
|
17
|
+
raise Twitter::Error::ServiceUnavailable.new("(__-){ Twitter is over capacity.", env[:response_headers])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|