twitter_labs_api 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a581a41b57bc11fb7f01964a03c13c9e7923ee0813250ad582bc853591cc0275
4
- data.tar.gz: 226af47431ababbb840a112b27c4fe31ee485c039739c7f0251b69e63ed6fcfd
3
+ metadata.gz: 471a4a8a8b2f248f11d0337240451d5b5bc2fdff5601370335f0ab080038cb0a
4
+ data.tar.gz: 25f2097f036e2ba1b9431f5e4a01db6a062ea7507f05b648c85b9d3514c5d083
5
5
  SHA512:
6
- metadata.gz: dba4c0c8c92b3a220ebf50cf94c8257a4d1c36bf5c672fabea8df14afa8afe1bfab3c5dfb24878bc0b49bf3b26e617f6ca1d83717a0f261f41d2b014d022463d
7
- data.tar.gz: ef93fc47a585d98df6ef64e98a5a6f108caed74b3680bea093532ea52a4520809b1a26286176f5f864df8f99291e00fbe03079b1795bb701159a2bad91fa89cf
6
+ metadata.gz: df432778d2baef5046f1c0d36ddd1b96f17e58414063ce3e76f15fdb9d7f78abc82327c598bc2a0e67fc3e205a430c5e5f851e1afb16691176a5722e506fe226
7
+ data.tar.gz: bcad0b98629f789f004974fdc505929b7a7f42af362490a8d9bd5e3ade4389ae770e25cf1550cd46af308f3336b2c4f3018a71580e41e9f92e48d42d1aea5fca
data/Gemfile CHANGED
@@ -4,3 +4,4 @@ gemspec
4
4
 
5
5
  gem 'activesupport'
6
6
  gem 'httplog'
7
+ gem 'pry'
@@ -1,31 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twitter_labs_api (0.3.1)
4
+ twitter_labs_api (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activesupport (6.0.0)
9
+ activesupport (6.0.3.2)
10
10
  concurrent-ruby (~> 1.0, >= 1.0.2)
11
11
  i18n (>= 0.7, < 2)
12
12
  minitest (~> 5.1)
13
13
  tzinfo (~> 1.1)
14
- zeitwerk (~> 2.1, >= 2.1.8)
14
+ zeitwerk (~> 2.2, >= 2.2.2)
15
+ coderay (1.1.3)
15
16
  concurrent-ruby (1.1.6)
16
17
  httplog (1.3.2)
17
18
  rack (>= 1.0)
18
19
  rainbow (>= 2.0.0)
19
- i18n (1.6.0)
20
+ i18n (1.8.5)
20
21
  concurrent-ruby (~> 1.0)
21
- minitest (5.14.0)
22
- rack (2.2.2)
22
+ method_source (1.0.0)
23
+ minitest (5.14.1)
24
+ pry (0.13.1)
25
+ coderay (~> 1.1)
26
+ method_source (~> 1.0)
27
+ rack (2.2.3)
23
28
  rainbow (3.0.0)
24
29
  rake (13.0.1)
25
30
  thread_safe (0.3.6)
26
- tzinfo (1.2.6)
31
+ tzinfo (1.2.7)
27
32
  thread_safe (~> 0.1)
28
- zeitwerk (2.1.9)
33
+ zeitwerk (2.4.0)
29
34
 
30
35
  PLATFORMS
31
36
  ruby
@@ -34,8 +39,9 @@ DEPENDENCIES
34
39
  activesupport
35
40
  bundler (~> 2.0)
36
41
  httplog
42
+ pry
37
43
  rake (~> 13.0)
38
44
  twitter_labs_api!
39
45
 
40
46
  BUNDLED WITH
41
- 2.0.2
47
+ 2.1.4
data/README.md CHANGED
@@ -40,6 +40,21 @@ api.get_tweet(id: '1235508591232090112', tweet_fields: my_fields)
40
40
  >> {"author_id"=>"229708614", "created_at"=>"2020-03-05T10:12:57.000Z", "id"=>"1235508591232090112", "text"=>"Hot take: coronavirus will not boost remote work in the long run because spur-of-the-moment work-from-home for in-person companies is likely to be a shitshow."}
41
41
  ```
42
42
 
43
+ #### API Errors
44
+
45
+ Sometimes the API will respond with an error, for example `429 Too Many Requests`. The gem will throw an error with the `Net::HTTP` response as an attribute for proper exception-handling by the consuming app:
46
+
47
+ ```ruby
48
+ def my_twitter_request
49
+ api.get_tweet(id: '1235508591232090112', tweet_fields: my_fields)
50
+
51
+ rescue TwitterLabsAPI::APIError => e
52
+ puts e.msg # 429 Too Many Requests
53
+ puts e.response # <Net::HTTPTooManyRequests 429 Too Many Requests readbody=true>
54
+ # do something with the Net::HTTP response...
55
+ end
56
+ ```
57
+
43
58
  ### Status
44
59
  Currently, the following endpoints are implemented:
45
60
 
@@ -10,7 +10,17 @@ DEFAULT_USER_FIELDS = %w[name username].freeze
10
10
  class TwitterLabsAPI
11
11
  attr_accessor :bearer_token, :debug, :api_response, :parsed_response
12
12
 
13
- class TwitterLabsAPIError < StandardError; end
13
+ class APIError < StandardError
14
+ DEFAULT_MESSAGE = 'Twitter Labs API error, check the response attribute'.freeze
15
+
16
+ attr_reader :response
17
+
18
+ def initialize(msg = DEFAULT_MESSAGE, response = nil)
19
+ @response = response
20
+
21
+ super(msg)
22
+ end
23
+ end
14
24
 
15
25
  def initialize(bearer_token:, debug: false)
16
26
  @bearer_token = bearer_token
@@ -77,7 +87,7 @@ class TwitterLabsAPI
77
87
  http.request(request)
78
88
  end
79
89
 
80
- raise(TwitterLabsAPIError, "#{api_response.code} #{api_response.msg}") unless api_response.is_a?(Net::HTTPSuccess)
90
+ raise_http_error unless api_response.is_a?(Net::HTTPSuccess)
81
91
 
82
92
  self.parsed_response = JSON.parse(api_response.body)
83
93
 
@@ -98,9 +108,13 @@ class TwitterLabsAPI
98
108
  parsed_response['data'].map(&:with_indifferent_access)
99
109
  end
100
110
 
111
+ def raise_http_error
112
+ raise(APIError.new("#{api_response.code} #{api_response.msg}", api_response))
113
+ end
114
+
101
115
  def handle_api_error
102
116
  error = parsed_response['errors'].first
103
117
 
104
- raise TwitterLabsAPIError, "#{error['title']}: #{error['detail']} #{error['type']}"
118
+ raise APIError.new("#{error['title']}: #{error['detail']} #{error['type']}", api_response)
105
119
  end
106
120
  end
@@ -1,3 +1,3 @@
1
1
  class TwitterLabsAPI
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_labs_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tomholford
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2020-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler