ifttt-twitter 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +9 -0
  5. data/.yardopts +8 -0
  6. data/Gemfile +7 -0
  7. data/HISTORY.md +520 -0
  8. data/LICENSE.md +20 -0
  9. data/README.md +251 -0
  10. data/Rakefile +13 -0
  11. data/lib/twitter.rb +24 -0
  12. data/lib/twitter/action.rb +9 -0
  13. data/lib/twitter/action_factory.rb +28 -0
  14. data/lib/twitter/authenticatable.rb +24 -0
  15. data/lib/twitter/base.rb +40 -0
  16. data/lib/twitter/client.rb +126 -0
  17. data/lib/twitter/client/accounts.rb +180 -0
  18. data/lib/twitter/client/activity.rb +48 -0
  19. data/lib/twitter/client/block.rb +104 -0
  20. data/lib/twitter/client/direct_messages.rb +110 -0
  21. data/lib/twitter/client/favorites.rb +82 -0
  22. data/lib/twitter/client/friends_and_followers.rb +293 -0
  23. data/lib/twitter/client/help.rb +38 -0
  24. data/lib/twitter/client/legal.rb +32 -0
  25. data/lib/twitter/client/lists.rb +643 -0
  26. data/lib/twitter/client/local_trends.rb +46 -0
  27. data/lib/twitter/client/notification.rb +49 -0
  28. data/lib/twitter/client/places_and_geo.rb +113 -0
  29. data/lib/twitter/client/saved_searches.rb +73 -0
  30. data/lib/twitter/client/search.rb +94 -0
  31. data/lib/twitter/client/spam_reporting.rb +30 -0
  32. data/lib/twitter/client/suggested_users.rb +60 -0
  33. data/lib/twitter/client/timelines.rb +265 -0
  34. data/lib/twitter/client/trends.rb +55 -0
  35. data/lib/twitter/client/tweets.rb +191 -0
  36. data/lib/twitter/client/urls.rb +26 -0
  37. data/lib/twitter/client/users.rb +203 -0
  38. data/lib/twitter/config.rb +106 -0
  39. data/lib/twitter/configuration.rb +20 -0
  40. data/lib/twitter/connection.rb +43 -0
  41. data/lib/twitter/core_ext/hash.rb +81 -0
  42. data/lib/twitter/creatable.rb +14 -0
  43. data/lib/twitter/cursor.rb +44 -0
  44. data/lib/twitter/direct_message.rb +27 -0
  45. data/lib/twitter/error.rb +37 -0
  46. data/lib/twitter/error/bad_gateway.rb +7 -0
  47. data/lib/twitter/error/bad_request.rb +7 -0
  48. data/lib/twitter/error/client_error.rb +7 -0
  49. data/lib/twitter/error/enhance_your_calm.rb +13 -0
  50. data/lib/twitter/error/forbidden.rb +7 -0
  51. data/lib/twitter/error/internal_server_error.rb +7 -0
  52. data/lib/twitter/error/not_acceptable.rb +7 -0
  53. data/lib/twitter/error/not_found.rb +7 -0
  54. data/lib/twitter/error/server_error.rb +7 -0
  55. data/lib/twitter/error/service_unavailable.rb +7 -0
  56. data/lib/twitter/error/unauthorized.rb +7 -0
  57. data/lib/twitter/favorite.rb +28 -0
  58. data/lib/twitter/follow.rb +27 -0
  59. data/lib/twitter/geo_factory.rb +22 -0
  60. data/lib/twitter/language.rb +7 -0
  61. data/lib/twitter/list.rb +22 -0
  62. data/lib/twitter/list_member_added.rb +36 -0
  63. data/lib/twitter/media_factory.rb +21 -0
  64. data/lib/twitter/mention.rb +43 -0
  65. data/lib/twitter/metadata.rb +7 -0
  66. data/lib/twitter/oembed.rb +8 -0
  67. data/lib/twitter/photo.rb +24 -0
  68. data/lib/twitter/place.rb +36 -0
  69. data/lib/twitter/point.rb +27 -0
  70. data/lib/twitter/polygon.rb +14 -0
  71. data/lib/twitter/rate_limit_status.rb +15 -0
  72. data/lib/twitter/relationship.rb +18 -0
  73. data/lib/twitter/reply.rb +36 -0
  74. data/lib/twitter/request.rb +38 -0
  75. data/lib/twitter/request/gateway.rb +20 -0
  76. data/lib/twitter/request/multipart_with_file.rb +37 -0
  77. data/lib/twitter/request/oauth.rb +26 -0
  78. data/lib/twitter/request/phoenix.rb +22 -0
  79. data/lib/twitter/response/parse_json.rb +28 -0
  80. data/lib/twitter/response/raise_client_error.rb +49 -0
  81. data/lib/twitter/response/raise_server_error.rb +23 -0
  82. data/lib/twitter/retweet.rb +36 -0
  83. data/lib/twitter/saved_search.rb +16 -0
  84. data/lib/twitter/settings.rb +17 -0
  85. data/lib/twitter/size.rb +16 -0
  86. data/lib/twitter/status.rb +80 -0
  87. data/lib/twitter/suggestion.rb +22 -0
  88. data/lib/twitter/trend.rb +14 -0
  89. data/lib/twitter/user.rb +65 -0
  90. data/lib/twitter/version.rb +30 -0
  91. data/spec/faraday/request_spec.rb +25 -0
  92. data/spec/faraday/response_spec.rb +68 -0
  93. data/spec/fixtures/about_me.json +1 -0
  94. data/spec/fixtures/all.json +1 -0
  95. data/spec/fixtures/bad_gateway.json +1 -0
  96. data/spec/fixtures/bad_request.json +1 -0
  97. data/spec/fixtures/by_friends.json +1 -0
  98. data/spec/fixtures/category.json +1 -0
  99. data/spec/fixtures/configuration.json +1 -0
  100. data/spec/fixtures/contributees.json +1 -0
  101. data/spec/fixtures/contributors.json +1 -0
  102. data/spec/fixtures/direct_message.json +1 -0
  103. data/spec/fixtures/direct_messages.json +1 -0
  104. data/spec/fixtures/end_session.json +1 -0
  105. data/spec/fixtures/enhance_your_calm.text +11 -0
  106. data/spec/fixtures/false.json +1 -0
  107. data/spec/fixtures/favorites.json +1 -0
  108. data/spec/fixtures/forbidden.json +1 -0
  109. data/spec/fixtures/friendships.json +1 -0
  110. data/spec/fixtures/id_list.json +1 -0
  111. data/spec/fixtures/ids.json +1 -0
  112. data/spec/fixtures/image_facets.json +1 -0
  113. data/spec/fixtures/internal_server_error.json +1 -0
  114. data/spec/fixtures/languages.json +1 -0
  115. data/spec/fixtures/list.json +1 -0
  116. data/spec/fixtures/lists.json +1 -0
  117. data/spec/fixtures/locations.json +1 -0
  118. data/spec/fixtures/matching_trends.json +1 -0
  119. data/spec/fixtures/me.jpeg +0 -0
  120. data/spec/fixtures/media_timeline.json +1 -0
  121. data/spec/fixtures/members.json +1 -0
  122. data/spec/fixtures/n605431196_2079896_558_normal.jpg +0 -0
  123. data/spec/fixtures/no_user_matches.json +1 -0
  124. data/spec/fixtures/not_acceptable.json +1 -0
  125. data/spec/fixtures/not_found.json +1 -0
  126. data/spec/fixtures/oembed.json +1 -0
  127. data/spec/fixtures/pengwynn.json +1 -0
  128. data/spec/fixtures/phoenix_search.phoenix +1 -0
  129. data/spec/fixtures/place.json +1 -0
  130. data/spec/fixtures/places.json +1 -0
  131. data/spec/fixtures/privacy.json +1 -0
  132. data/spec/fixtures/profile_image.text +24 -0
  133. data/spec/fixtures/rate_limit_status.json +1 -0
  134. data/spec/fixtures/recommendations.json +1 -0
  135. data/spec/fixtures/relationship.json +1 -0
  136. data/spec/fixtures/resolve.json +1 -0
  137. data/spec/fixtures/retweet.json +1 -0
  138. data/spec/fixtures/retweeters_of.json +1 -0
  139. data/spec/fixtures/retweets.json +1 -0
  140. data/spec/fixtures/saved_search.json +1 -0
  141. data/spec/fixtures/saved_searches.json +1 -0
  142. data/spec/fixtures/search.json +1 -0
  143. data/spec/fixtures/service_unavailable.json +1 -0
  144. data/spec/fixtures/settings.json +1 -0
  145. data/spec/fixtures/sferik.json +1 -0
  146. data/spec/fixtures/status.json +1 -0
  147. data/spec/fixtures/status_with_media.json +104 -0
  148. data/spec/fixtures/statuses.json +1 -0
  149. data/spec/fixtures/suggestions.json +1 -0
  150. data/spec/fixtures/tos.json +1 -0
  151. data/spec/fixtures/totals.json +1 -0
  152. data/spec/fixtures/trends.json +1 -0
  153. data/spec/fixtures/trends_current.json +1 -0
  154. data/spec/fixtures/trends_daily.json +1 -0
  155. data/spec/fixtures/trends_weekly.json +1 -0
  156. data/spec/fixtures/true.json +1 -0
  157. data/spec/fixtures/unauthorized.json +1 -0
  158. data/spec/fixtures/user_search.json +1 -0
  159. data/spec/fixtures/user_timeline.json +1 -0
  160. data/spec/fixtures/users.json +1 -0
  161. data/spec/fixtures/users_list.json +1 -0
  162. data/spec/fixtures/video_facets.json +1 -0
  163. data/spec/fixtures/we_concept_bg2.png +0 -0
  164. data/spec/helper.rb +49 -0
  165. data/spec/twitter/action_factory_spec.rb +37 -0
  166. data/spec/twitter/action_spec.rb +16 -0
  167. data/spec/twitter/base_spec.rb +29 -0
  168. data/spec/twitter/client/accounts_spec.rb +181 -0
  169. data/spec/twitter/client/activity_spec.rb +43 -0
  170. data/spec/twitter/client/block_spec.rb +106 -0
  171. data/spec/twitter/client/direct_messages_spec.rb +98 -0
  172. data/spec/twitter/client/favorites_spec.rb +80 -0
  173. data/spec/twitter/client/friends_and_followers_spec.rb +478 -0
  174. data/spec/twitter/client/help_spec.rb +44 -0
  175. data/spec/twitter/client/legal_spec.rb +41 -0
  176. data/spec/twitter/client/lists_spec.rb +1104 -0
  177. data/spec/twitter/client/local_trends_spec.rb +58 -0
  178. data/spec/twitter/client/notification_spec.rb +47 -0
  179. data/spec/twitter/client/places_and_geo_spec.rb +100 -0
  180. data/spec/twitter/client/saved_searches_spec.rb +80 -0
  181. data/spec/twitter/client/search_spec.rb +92 -0
  182. data/spec/twitter/client/spam_reporting_spec.rb +28 -0
  183. data/spec/twitter/client/suggested_users_spec.rb +65 -0
  184. data/spec/twitter/client/timelines_spec.rb +231 -0
  185. data/spec/twitter/client/trends_spec.rb +51 -0
  186. data/spec/twitter/client/tweets_spec.rb +189 -0
  187. data/spec/twitter/client/urls_spec.rb +31 -0
  188. data/spec/twitter/client/users_spec.rb +323 -0
  189. data/spec/twitter/client_spec.rb +94 -0
  190. data/spec/twitter/configuration_spec.rb +17 -0
  191. data/spec/twitter/cursor_spec.rb +43 -0
  192. data/spec/twitter/direct_message_spec.rb +56 -0
  193. data/spec/twitter/favorite_spec.rb +29 -0
  194. data/spec/twitter/follow_spec.rb +29 -0
  195. data/spec/twitter/geo_factory_spec.rb +21 -0
  196. data/spec/twitter/list_spec.rb +34 -0
  197. data/spec/twitter/media_factory_spec.rb +17 -0
  198. data/spec/twitter/mention_spec.rb +52 -0
  199. data/spec/twitter/oembed_spec.rb +145 -0
  200. data/spec/twitter/photo_spec.rb +35 -0
  201. data/spec/twitter/place_spec.rb +75 -0
  202. data/spec/twitter/point_spec.rb +32 -0
  203. data/spec/twitter/polygon_spec.rb +18 -0
  204. data/spec/twitter/rate_limit_status_spec.rb +16 -0
  205. data/spec/twitter/relationship_spec.rb +27 -0
  206. data/spec/twitter/reply_spec.rb +41 -0
  207. data/spec/twitter/retweet_spec.rb +41 -0
  208. data/spec/twitter/saved_search_spec.rb +34 -0
  209. data/spec/twitter/settings_spec.rb +16 -0
  210. data/spec/twitter/size_spec.rb +18 -0
  211. data/spec/twitter/status_spec.rb +148 -0
  212. data/spec/twitter/suggestion_spec.rb +20 -0
  213. data/spec/twitter/trend_spec.rb +20 -0
  214. data/spec/twitter/user_spec.rb +50 -0
  215. data/spec/twitter_spec.rb +91 -0
  216. data/twitter.gemspec +42 -0
  217. metadata +564 -0
@@ -0,0 +1,7 @@
1
+ require 'twitter/base'
2
+
3
+ module Twitter
4
+ class Metadata < Twitter::Base
5
+ lazy_attr_reader :result_type
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'twitter/base'
2
+
3
+ module Twitter
4
+ class OEmbed < Twitter::Base
5
+ lazy_attr_reader :author_name,:author_url, :cache_age, :height, :html, :id,
6
+ :provider_name, :provider_url, :type, :width, :url, :version
7
+ end
8
+ end
@@ -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