x 0.10.0 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +41 -28
- data/README.md +6 -2
- data/lib/x/authenticator.rb +8 -0
- data/lib/x/bearer_token_authenticator.rb +5 -3
- data/lib/x/cgi.rb +15 -0
- data/lib/x/client.rb +40 -40
- data/lib/x/connection.rb +39 -76
- data/lib/x/errors/bad_gateway.rb +5 -0
- data/lib/x/errors/{not_found_error.rb → bad_request.rb} +1 -1
- data/lib/x/errors/connection_exception.rb +5 -0
- data/lib/x/errors/error.rb +1 -6
- data/lib/x/errors/{forbidden_error.rb → forbidden.rb} +1 -1
- data/lib/x/errors/gateway_timeout.rb +5 -0
- data/lib/x/errors/{bad_request_error.rb → gone.rb} +1 -1
- data/lib/x/errors/network_error.rb +1 -1
- data/lib/x/errors/not_acceptable.rb +5 -0
- data/lib/x/errors/not_found.rb +5 -0
- data/lib/x/errors/payload_too_large.rb +5 -0
- data/lib/x/errors/service_unavailable.rb +5 -0
- data/lib/x/errors/too_many_redirects.rb +5 -0
- data/lib/x/errors/too_many_requests.rb +29 -0
- data/lib/x/errors/unauthorized.rb +5 -0
- data/lib/x/errors/unprocessable_entity.rb +5 -0
- data/lib/x/{media_upload.rb → media_uploader.rb} +12 -15
- data/lib/x/oauth_authenticator.rb +10 -15
- data/lib/x/redirect_handler.rb +26 -22
- data/lib/x/request_builder.rb +22 -35
- data/lib/x/response_parser.rb +92 -0
- data/lib/x/version.rb +1 -1
- data/sig/x.rbs +101 -87
- metadata +21 -13
- data/lib/x/errors/authentication_error.rb +0 -5
- data/lib/x/errors/payload_too_large_error.rb +0 -5
- data/lib/x/errors/service_unavailable_error.rb +0 -5
- data/lib/x/errors/too_many_redirects_error.rb +0 -5
- data/lib/x/errors/too_many_requests_error.rb +0 -29
- data/lib/x/response_handler.rb +0 -63
data/sig/x.rbs
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
module X
|
2
2
|
VERSION: Gem::Version
|
3
3
|
|
4
|
+
class Authenticator
|
5
|
+
def header: (Net::HTTPRequest? request) -> Hash[String, String]
|
6
|
+
end
|
7
|
+
|
4
8
|
class BearerTokenAuthenticator
|
5
9
|
attr_accessor bearer_token: String
|
6
|
-
def initialize: (String
|
7
|
-
def header: (Net::HTTPRequest
|
10
|
+
def initialize: (bearer_token: String) -> void
|
11
|
+
def header: (Net::HTTPRequest? request) -> Hash[String, String]
|
8
12
|
end
|
9
13
|
|
10
|
-
class
|
14
|
+
class OAuthAuthenticator
|
11
15
|
OAUTH_VERSION: String
|
12
16
|
OAUTH_SIGNATURE_METHOD: String
|
13
17
|
OAUTH_SIGNATURE_ALGORITHM: String
|
@@ -16,8 +20,8 @@ module X
|
|
16
20
|
attr_accessor api_key_secret: String
|
17
21
|
attr_accessor access_token: String
|
18
22
|
attr_accessor access_token_secret: String
|
19
|
-
def initialize: (
|
20
|
-
def header: (Net::HTTPRequest request) -> String
|
23
|
+
def initialize: (api_key: String, api_key_secret: String, access_token: String, access_token_secret: String) -> void
|
24
|
+
def header: (Net::HTTPRequest request) -> Hash[String, String]
|
21
25
|
|
22
26
|
private
|
23
27
|
def parse_request: (Net::HTTPRequest request) -> [String, String, Hash[String, String]]
|
@@ -33,189 +37,194 @@ module X
|
|
33
37
|
def escape: (String value) -> String
|
34
38
|
end
|
35
39
|
|
36
|
-
class
|
37
|
-
|
40
|
+
class ClientError < Error
|
41
|
+
end
|
38
42
|
|
39
|
-
|
40
|
-
|
43
|
+
class BadGateway < ClientError
|
44
|
+
end
|
41
45
|
|
42
|
-
|
43
|
-
def json_response?: (Net::HTTPResponse response) -> bool
|
46
|
+
class BadRequest < ClientError
|
44
47
|
end
|
45
48
|
|
46
|
-
class
|
49
|
+
class ConnectionException < ClientError
|
47
50
|
end
|
48
51
|
|
49
|
-
class
|
52
|
+
class Error < StandardError
|
53
|
+
def initialize: (String msg, ?Net::HTTPResponse? response) -> void
|
54
|
+
end
|
55
|
+
|
56
|
+
class Forbidden < ClientError
|
50
57
|
end
|
51
58
|
|
52
|
-
class
|
59
|
+
class GatewayTimeout < ClientError
|
53
60
|
end
|
54
61
|
|
55
|
-
class
|
62
|
+
class Gone < ClientError
|
56
63
|
end
|
57
64
|
|
58
65
|
class InternalServerError < ServerError
|
59
66
|
end
|
60
67
|
|
61
|
-
class
|
68
|
+
class NetworkError < Error
|
62
69
|
end
|
63
70
|
|
64
|
-
class
|
71
|
+
class NotAcceptable < ClientError
|
65
72
|
end
|
66
73
|
|
67
|
-
class
|
74
|
+
class NotFound < ClientError
|
75
|
+
end
|
76
|
+
|
77
|
+
class PayloadTooLarge < ClientError
|
78
|
+
end
|
79
|
+
|
80
|
+
class ServerError < Error
|
68
81
|
end
|
69
82
|
|
70
|
-
class
|
83
|
+
class ServiceUnavailable < ServerError
|
84
|
+
end
|
85
|
+
|
86
|
+
class TooManyRedirects < ClientError
|
87
|
+
end
|
88
|
+
|
89
|
+
class TooManyRequests < ClientError
|
71
90
|
@response: Net::HTTPResponse
|
72
91
|
|
73
|
-
def initialize: (String msg,
|
92
|
+
def initialize: (String msg, Net::HTTPResponse response) -> void
|
74
93
|
def limit: -> Integer
|
75
94
|
def remaining: -> Integer
|
76
95
|
def reset_at: -> Time
|
77
96
|
def reset_in: -> Integer?
|
78
97
|
end
|
79
98
|
|
80
|
-
class
|
99
|
+
class Unauthorized < ClientError
|
81
100
|
end
|
82
101
|
|
83
|
-
class
|
84
|
-
end
|
85
|
-
|
86
|
-
class NetworkError < Error
|
102
|
+
class UnprocessableEntity < ClientError
|
87
103
|
end
|
88
104
|
|
89
105
|
class Connection
|
90
|
-
DEFAULT_BASE_URL: String
|
91
106
|
DEFAULT_HOST: String
|
92
107
|
DEFAULT_PORT: Integer
|
93
108
|
DEFAULT_OPEN_TIMEOUT: Integer
|
94
109
|
DEFAULT_READ_TIMEOUT: Integer
|
95
110
|
DEFAULT_WRITE_TIMEOUT: Integer
|
111
|
+
DEFAULT_DEBUG_OUTPUT: IO
|
96
112
|
NETWORK_ERRORS: Array[(singleton(Errno::ECONNREFUSED) | singleton(Errno::ECONNRESET) | singleton(Net::OpenTimeout) | singleton(Net::ReadTimeout) | singleton(OpenSSL::SSL::SSLError))]
|
97
113
|
|
98
114
|
extend Forwardable
|
99
|
-
@http_client: Net::HTTP
|
100
115
|
|
101
|
-
|
116
|
+
attr_accessor open_timeout : Float | Integer
|
117
|
+
attr_accessor read_timeout : Float | Integer
|
118
|
+
attr_accessor write_timeout : Float | Integer
|
119
|
+
attr_accessor debug_output : IO
|
120
|
+
|
102
121
|
attr_reader proxy_uri: URI::Generic?
|
103
|
-
attr_reader
|
104
|
-
attr_reader
|
105
|
-
attr_reader
|
106
|
-
|
107
|
-
|
108
|
-
def
|
109
|
-
def
|
110
|
-
def
|
122
|
+
attr_reader proxy_host : String?
|
123
|
+
attr_reader proxy_port : Integer?
|
124
|
+
attr_reader proxy_user : String?
|
125
|
+
attr_reader proxy_pass : String?
|
126
|
+
|
127
|
+
def initialize: (?open_timeout: Float | Integer, ?read_timeout: Float | Integer, ?write_timeout: Float | Integer, ?proxy_url: URI::Generic? | String?, ?debug_output: IO) -> void
|
128
|
+
def proxy_url=: (URI::Generic | String proxy_url) -> void
|
129
|
+
def perform: (request: Net::HTTPRequest) -> Net::HTTPResponse
|
111
130
|
|
112
131
|
private
|
113
|
-
def
|
114
|
-
def
|
115
|
-
def update_http_client_settings: -> untyped
|
116
|
-
def build_http_client: (host: String, port: Integer) -> (Net::HTTP)
|
117
|
-
def conditionally_apply_http_client_settings: { -> untyped } -> untyped
|
132
|
+
def build_http_client: (?String host, ?Integer port) -> Net::HTTP
|
133
|
+
def configure_http_client: (Net::HTTP http_client) -> Net::HTTP
|
118
134
|
end
|
119
135
|
|
120
136
|
class RequestBuilder
|
121
|
-
HTTP_METHODS: Hash[
|
122
|
-
|
123
|
-
|
124
|
-
AUTHORIZATION_HEADER: String
|
125
|
-
CONTENT_TYPE_HEADER: String
|
126
|
-
USER_AGENT_HEADER: String
|
127
|
-
|
128
|
-
attr_accessor content_type: String
|
129
|
-
attr_accessor user_agent: String
|
137
|
+
HTTP_METHODS: Hash[Symbol, (singleton(Net::HTTP::Get) | singleton(Net::HTTP::Post) | singleton(Net::HTTP::Put) | singleton(Net::HTTP::Delete))]
|
138
|
+
DEFAULT_HEADERS: Hash[String, String]
|
139
|
+
|
130
140
|
def initialize: (?content_type: String, ?user_agent: String) -> void
|
131
|
-
def build: (
|
132
|
-
def configuration: -> Hash[Symbol, untyped]
|
141
|
+
def build: (authenticator: Authenticator, http_method: Symbol, uri: URI::Generic, ?body: String?, ?headers: Hash[String, String]) -> (Net::HTTPRequest)
|
133
142
|
|
134
143
|
private
|
135
|
-
def create_request: (Symbol
|
136
|
-
def
|
137
|
-
def
|
138
|
-
def add_user_agent: (Net::HTTPRequest request) -> void
|
144
|
+
def create_request: (http_method: Symbol, uri: URI::Generic, body: String?) -> (Net::HTTPRequest)
|
145
|
+
def add_authentication: (request: Net::HTTPRequest, authenticator: Authenticator) -> void
|
146
|
+
def add_headers: (request: Net::HTTPRequest, headers: Hash[String, String]) -> void
|
139
147
|
def escape_query_params: (URI::Generic uri) -> URI::Generic
|
140
148
|
end
|
141
149
|
|
142
150
|
class RedirectHandler
|
143
151
|
DEFAULT_MAX_REDIRECTS: Integer
|
144
152
|
|
145
|
-
attr_reader authenticator:
|
153
|
+
attr_reader authenticator: Authenticator
|
146
154
|
attr_reader connection: Connection
|
147
155
|
attr_reader request_builder: RequestBuilder
|
148
156
|
attr_reader max_redirects: Integer
|
149
|
-
def initialize: (
|
150
|
-
def
|
157
|
+
def initialize: (authenticator: Authenticator, connection: Connection, request_builder: RequestBuilder, ?max_redirects: Integer) -> void
|
158
|
+
def handle: (response: Net::HTTPResponse, request: Net::HTTPRequest, base_url: String, ?redirect_count: Integer) -> Net::HTTPResponse
|
151
159
|
|
152
160
|
private
|
153
|
-
def build_new_uri: (Net::HTTPResponse response,
|
154
|
-
def build_request: (Net::HTTPRequest
|
161
|
+
def build_new_uri: (Net::HTTPResponse response, String base_url) -> URI::Generic
|
162
|
+
def build_request: (Net::HTTPRequest request, URI::Generic new_uri, Integer response_code) -> Net::HTTPRequest
|
155
163
|
def send_new_request: (URI::Generic new_uri, Net::HTTPRequest new_request) -> Net::HTTPResponse
|
156
164
|
end
|
157
165
|
|
158
|
-
class
|
166
|
+
class ResponseParser
|
159
167
|
DEFAULT_ARRAY_CLASS: Class
|
160
168
|
DEFAULT_OBJECT_CLASS: Class
|
161
|
-
ERROR_CLASSES: Hash[Integer, singleton(
|
169
|
+
ERROR_CLASSES: Hash[Integer, singleton(Unauthorized) | singleton(BadRequest) | singleton(Forbidden) | singleton(InternalServerError) | singleton(NotFound) | singleton(PayloadTooLarge) | singleton(ServiceUnavailable) | singleton(TooManyRequests)]
|
162
170
|
JSON_CONTENT_TYPE_REGEXP: Regexp
|
163
171
|
|
164
172
|
attr_accessor array_class: Class
|
165
173
|
attr_accessor object_class: Class
|
166
174
|
def initialize: (?array_class: Class, ?object_class: Class) -> void
|
167
|
-
def
|
168
|
-
def configuration: -> Hash[Symbol, Class]
|
175
|
+
def parse: (response: Net::HTTPResponse) -> untyped
|
169
176
|
|
170
177
|
private
|
171
178
|
def success?: (Net::HTTPResponse response) -> bool
|
179
|
+
def error: (Net::HTTPResponse response) -> (Unauthorized | BadRequest | Forbidden | InternalServerError | NotFound | PayloadTooLarge | ServiceUnavailable | TooManyRequests)
|
180
|
+
def error_class: (Net::HTTPResponse response) -> (singleton(Unauthorized) | singleton(BadRequest) | singleton(Forbidden) | singleton(InternalServerError) | singleton(NotFound) | singleton(PayloadTooLarge) | singleton(ServiceUnavailable) | singleton(TooManyRequests))
|
181
|
+
def error_message: (Net::HTTPResponse response) -> String
|
182
|
+
def message_from_json_response: (Net::HTTPResponse response) -> String
|
172
183
|
def json?: (Net::HTTPResponse response) -> bool
|
173
184
|
end
|
174
185
|
|
175
186
|
class Client
|
187
|
+
DEFAULT_BASE_URL: String
|
188
|
+
|
176
189
|
extend Forwardable
|
177
|
-
@authenticator:
|
190
|
+
@authenticator: Authenticator
|
178
191
|
@connection: Connection
|
179
192
|
@request_builder: RequestBuilder
|
180
193
|
@redirect_handler: RedirectHandler
|
181
|
-
@
|
194
|
+
@response_parser: ResponseParser
|
182
195
|
|
183
196
|
attr_accessor access_token: String
|
184
197
|
attr_accessor access_token_secret: String
|
185
198
|
attr_accessor api_key: String
|
186
199
|
attr_accessor api_key_secret: String
|
187
200
|
attr_accessor bearer_token: String
|
188
|
-
attr_accessor
|
201
|
+
attr_accessor base_url: String
|
189
202
|
attr_accessor open_timeout: Float | Integer
|
190
203
|
attr_accessor read_timeout: Float | Integer
|
191
204
|
attr_accessor write_timeout: Float | Integer
|
192
205
|
attr_accessor proxy_url: String
|
193
|
-
attr_accessor
|
194
|
-
attr_accessor user_agent: String
|
195
|
-
attr_accessor debug_output: IO?
|
206
|
+
attr_accessor debug_output: IO
|
196
207
|
attr_accessor array_class: Class
|
197
208
|
attr_accessor object_class: Class
|
198
209
|
attr_accessor max_redirects: Integer
|
199
|
-
attr_accessor authenticator:
|
210
|
+
attr_accessor authenticator: Authenticator
|
200
211
|
attr_accessor connection: Connection
|
201
212
|
attr_accessor request_builder: RequestBuilder
|
202
213
|
attr_accessor redirect_handler: RedirectHandler
|
203
|
-
attr_accessor
|
204
|
-
alias base_url base_uri
|
205
|
-
|
214
|
+
attr_accessor response_parser: ResponseParser
|
206
215
|
|
207
|
-
def initialize: (?bearer_token: String?, ?api_key: String?, ?api_key_secret: String?, ?access_token: String?, ?access_token_secret: String?, ?base_url:
|
208
|
-
def get: (String endpoint) -> untyped
|
209
|
-
def post: (String endpoint, ?String? body) -> untyped
|
210
|
-
def put: (String endpoint, ?String? body) -> untyped
|
211
|
-
def delete: (String endpoint) -> untyped
|
216
|
+
def initialize: (?bearer_token: String?, ?api_key: String?, ?api_key_secret: String?, ?access_token: String?, ?access_token_secret: String?, ?base_url: String, ?open_timeout: Float | Integer, ?read_timeout: Float | Integer, ?write_timeout: Float | Integer, ?proxy_url: URI::Generic? | String?, ?debug_output: IO, ?array_class: Class, ?object_class: Class, ?max_redirects: Integer) -> void
|
217
|
+
def get: (String endpoint, ?headers: Hash[String, String]) -> untyped
|
218
|
+
def post: (String endpoint, ?String? body, ?headers: Hash[String, String]) -> untyped
|
219
|
+
def put: (String endpoint, ?String? body, ?headers: Hash[String, String]) -> untyped
|
220
|
+
def delete: (String endpoint, ?headers: Hash[String, String]) -> untyped
|
212
221
|
|
213
222
|
private
|
214
|
-
def initialize_authenticator: (String? bearer_token, String? api_key, String? api_key_secret, String? access_token, String? access_token_secret) -> (BearerTokenAuthenticator |
|
215
|
-
def
|
223
|
+
def initialize_authenticator: (String? bearer_token, String? api_key, String? api_key_secret, String? access_token, String? access_token_secret) -> (BearerTokenAuthenticator | OAuthAuthenticator | Authenticator)
|
224
|
+
def execute_request: (Symbol http_method, String endpoint, headers: Hash[String, String], ?body: String?) -> untyped
|
216
225
|
end
|
217
226
|
|
218
|
-
module
|
227
|
+
module MediaUploader
|
219
228
|
MAX_RETRIES: Integer
|
220
229
|
BYTES_PER_MB: Integer
|
221
230
|
MEDIA_CATEGORIES: Array[String]
|
@@ -235,10 +244,10 @@ module X
|
|
235
244
|
SUBRIP_MIME_TYPE: String
|
236
245
|
WEBP_MIME_TYPE: String
|
237
246
|
MIME_TYPE_MAP: Hash[String, String]
|
238
|
-
extend
|
247
|
+
extend MediaUploader
|
239
248
|
|
240
|
-
def
|
241
|
-
def
|
249
|
+
def upload: (client: Client, file_path: String, media_category: String, ?media_type: String, ?boundary: String) -> untyped
|
250
|
+
def chunked_upload: (client: Client, file_path: String, media_category: String, ?media_type: String, ?boundary: String, ?chunk_size_mb: Integer) -> untyped
|
242
251
|
def await_processing: (client: Client, media: untyped) -> untyped
|
243
252
|
|
244
253
|
private
|
@@ -247,11 +256,16 @@ module X
|
|
247
256
|
def init: (Client upload_client, String file_path, String media_type, String media_category) -> untyped
|
248
257
|
def split: (String file_path, Integer chunk_size) -> Array[String]
|
249
258
|
def append: (Client upload_client, Array[String] chunk_paths, untyped media, String media_type, ?String boundary) -> Array[String]
|
250
|
-
def upload_chunk: (Client upload_client, String query, String chunk_path, String media_type, String
|
259
|
+
def upload_chunk: (Client upload_client, String query, String chunk_path, String media_type, ?Hash[String, String] headers) -> Integer?
|
251
260
|
def cleanup_chunk: (String chunk_path) -> Integer?
|
252
261
|
def finalize: (Client upload_client, untyped media) -> untyped
|
253
262
|
def construct_upload_body: (String file_path, String media_type, ?String boundary) -> String
|
254
263
|
end
|
264
|
+
|
265
|
+
class CGI
|
266
|
+
def self.escape: (String value) -> String
|
267
|
+
def self.escape_params: (Hash[String, String] | Array[[String, String]] params) -> String
|
268
|
+
end
|
255
269
|
end
|
256
270
|
|
257
271
|
class Dir
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: x
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Berlin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -23,27 +23,35 @@ files:
|
|
23
23
|
- bin/console
|
24
24
|
- bin/setup
|
25
25
|
- lib/x.rb
|
26
|
+
- lib/x/authenticator.rb
|
26
27
|
- lib/x/bearer_token_authenticator.rb
|
28
|
+
- lib/x/cgi.rb
|
27
29
|
- lib/x/client.rb
|
28
30
|
- lib/x/connection.rb
|
29
|
-
- lib/x/errors/
|
30
|
-
- lib/x/errors/
|
31
|
+
- lib/x/errors/bad_gateway.rb
|
32
|
+
- lib/x/errors/bad_request.rb
|
31
33
|
- lib/x/errors/client_error.rb
|
34
|
+
- lib/x/errors/connection_exception.rb
|
32
35
|
- lib/x/errors/error.rb
|
33
|
-
- lib/x/errors/
|
36
|
+
- lib/x/errors/forbidden.rb
|
37
|
+
- lib/x/errors/gateway_timeout.rb
|
38
|
+
- lib/x/errors/gone.rb
|
34
39
|
- lib/x/errors/internal_server_error.rb
|
35
40
|
- lib/x/errors/network_error.rb
|
36
|
-
- lib/x/errors/
|
37
|
-
- lib/x/errors/
|
41
|
+
- lib/x/errors/not_acceptable.rb
|
42
|
+
- lib/x/errors/not_found.rb
|
43
|
+
- lib/x/errors/payload_too_large.rb
|
38
44
|
- lib/x/errors/server_error.rb
|
39
|
-
- lib/x/errors/
|
40
|
-
- lib/x/errors/
|
41
|
-
- lib/x/errors/
|
42
|
-
- lib/x/
|
45
|
+
- lib/x/errors/service_unavailable.rb
|
46
|
+
- lib/x/errors/too_many_redirects.rb
|
47
|
+
- lib/x/errors/too_many_requests.rb
|
48
|
+
- lib/x/errors/unauthorized.rb
|
49
|
+
- lib/x/errors/unprocessable_entity.rb
|
50
|
+
- lib/x/media_uploader.rb
|
43
51
|
- lib/x/oauth_authenticator.rb
|
44
52
|
- lib/x/redirect_handler.rb
|
45
53
|
- lib/x/request_builder.rb
|
46
|
-
- lib/x/
|
54
|
+
- lib/x/response_parser.rb
|
47
55
|
- lib/x/version.rb
|
48
56
|
- sig/x.rbs
|
49
57
|
homepage: https://sferik.github.io/x-ruby
|
@@ -72,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
80
|
- !ruby/object:Gem::Version
|
73
81
|
version: '0'
|
74
82
|
requirements: []
|
75
|
-
rubygems_version: 3.4.
|
83
|
+
rubygems_version: 3.4.21
|
76
84
|
signing_key:
|
77
85
|
specification_version: 4
|
78
86
|
summary: A Ruby interface to the X API.
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require_relative "client_error"
|
2
|
-
|
3
|
-
module X
|
4
|
-
# Rate limit error
|
5
|
-
class TooManyRequestsError < ClientError
|
6
|
-
def initialize(msg, response:)
|
7
|
-
@response = response
|
8
|
-
super
|
9
|
-
end
|
10
|
-
|
11
|
-
def limit
|
12
|
-
@response.fetch("x-rate-limit-limit", 0).to_i
|
13
|
-
end
|
14
|
-
|
15
|
-
def remaining
|
16
|
-
@response.fetch("x-rate-limit-remaining", 0).to_i
|
17
|
-
end
|
18
|
-
|
19
|
-
def reset_at
|
20
|
-
Time.at(@response.fetch("x-rate-limit-reset", 0).to_i).utc
|
21
|
-
end
|
22
|
-
|
23
|
-
def reset_in
|
24
|
-
[(reset_at - Time.now).ceil, 0].max if reset_at
|
25
|
-
end
|
26
|
-
|
27
|
-
alias_method :retry_after, :reset_in
|
28
|
-
end
|
29
|
-
end
|
data/lib/x/response_handler.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
require "json"
|
2
|
-
require "net/http"
|
3
|
-
require_relative "errors/authentication_error"
|
4
|
-
require_relative "errors/bad_request_error"
|
5
|
-
require_relative "errors/forbidden_error"
|
6
|
-
require_relative "errors/not_found_error"
|
7
|
-
require_relative "errors/payload_too_large_error"
|
8
|
-
require_relative "errors/internal_server_error"
|
9
|
-
require_relative "errors/service_unavailable_error"
|
10
|
-
require_relative "errors/too_many_requests_error"
|
11
|
-
|
12
|
-
module X
|
13
|
-
# Process HTTP responses
|
14
|
-
class ResponseHandler
|
15
|
-
DEFAULT_ARRAY_CLASS = Array
|
16
|
-
DEFAULT_OBJECT_CLASS = Hash
|
17
|
-
ERROR_CLASSES = {
|
18
|
-
400 => BadRequestError,
|
19
|
-
401 => AuthenticationError,
|
20
|
-
403 => ForbiddenError,
|
21
|
-
404 => NotFoundError,
|
22
|
-
413 => PayloadTooLargeError,
|
23
|
-
429 => TooManyRequestsError,
|
24
|
-
500 => InternalServerError,
|
25
|
-
503 => ServiceUnavailableError
|
26
|
-
}.freeze
|
27
|
-
JSON_CONTENT_TYPE_REGEXP = %r{application/(problem\+|)json}
|
28
|
-
|
29
|
-
attr_accessor :array_class, :object_class
|
30
|
-
|
31
|
-
def initialize(array_class: DEFAULT_ARRAY_CLASS, object_class: DEFAULT_OBJECT_CLASS)
|
32
|
-
@array_class = array_class
|
33
|
-
@object_class = object_class
|
34
|
-
end
|
35
|
-
|
36
|
-
def handle(response)
|
37
|
-
if success?(response)
|
38
|
-
JSON.parse(response.body, array_class: array_class, object_class: object_class) if json?(response)
|
39
|
-
else
|
40
|
-
error_class = ERROR_CLASSES[response.code.to_i] || Error
|
41
|
-
error_message = "#{response.code} #{response.message}"
|
42
|
-
raise error_class.new(error_message, response: response)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def configuration
|
47
|
-
{
|
48
|
-
array_class: array_class,
|
49
|
-
object_class: object_class
|
50
|
-
}
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def success?(response)
|
56
|
-
response.is_a?(Net::HTTPSuccess)
|
57
|
-
end
|
58
|
-
|
59
|
-
def json?(response)
|
60
|
-
response.body && JSON_CONTENT_TYPE_REGEXP.match?(response["content-type"])
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|