x 0.5.1 → 0.7.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 +10 -0
- data/Gemfile +2 -0
- data/README.md +56 -22
- data/Rakefile +9 -1
- data/Steepfile +13 -0
- data/lib/x/authenticator.rb +65 -27
- data/lib/x/client.rb +13 -16
- data/lib/x/connection.rb +11 -5
- data/lib/x/errors/error.rb +1 -1
- data/lib/x/errors/network_error.rb +1 -1
- data/lib/x/errors/too_many_requests_error.rb +4 -4
- data/lib/x/request_builder.rb +4 -8
- data/lib/x/response_handler.rb +2 -8
- data/lib/x/version.rb +1 -1
- data/sig/x.rbs +143 -2
- metadata +6 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4e43c461d1aa0e058f2dfcbb53f85fbcf5ade26e1f4a169a044454d9110f2b9
|
4
|
+
data.tar.gz: 267f92093e37ecc6e82c1fdd6cc2eb8615a58e6e586e4eaa63a4d8b890e1afc6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c68483c77e776f13a7178775148ca16046515dad0ce3d259667b49d0194b89714d70fe0bddf9b14b0cd80514e276ad545fbf4913e9b65a8f106ec1e08443fa7
|
7
|
+
data.tar.gz: 58490badac233a7233331051fc7f2940e6648cd2c5a7de74261b968656b5617d01eeeea3eddfd514a25bac77628b54d78e09085924993700b8a52dc5817fd248
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.7.0] - 2023-09-02
|
4
|
+
|
5
|
+
- Remove OAuth gem (7c29bb1)
|
6
|
+
|
7
|
+
## [0.6.0] - 2023-08-30
|
8
|
+
|
9
|
+
- Add configurable debug output stream for logging (fd2d4b0)
|
10
|
+
- Remove bearer token authentication (efff940)
|
11
|
+
- Define RBS type signatures (d7f63ba)
|
12
|
+
|
3
13
|
## [0.5.1] - 2023-08-16
|
4
14
|
|
5
15
|
- Fix bearer token authentication (1a1ca93)
|
data/Gemfile
CHANGED
@@ -6,11 +6,13 @@ gemspec
|
|
6
6
|
gem "hashie", ">= 5"
|
7
7
|
gem "minitest", ">= 5.19"
|
8
8
|
gem "rake", ">= 13.0.6"
|
9
|
+
gem "rbs", ">= 3.2.1"
|
9
10
|
gem "rubocop", ">= 1.21"
|
10
11
|
gem "rubocop-minitest", ">= 0.31"
|
11
12
|
gem "rubocop-performance", ">= 1.18"
|
12
13
|
gem "rubocop-rake", ">= 0.6"
|
13
14
|
gem "simplecov", ">= 0.22"
|
14
15
|
gem "standard", ">= 1.30.1"
|
16
|
+
gem "steep", ">= 1.5.3"
|
15
17
|
gem "timecop", ">= 0.9.6"
|
16
18
|
gem "webmock", ">= 3.18.1"
|
data/README.md
CHANGED
@@ -1,31 +1,33 @@
|
|
1
1
|
# X
|
2
2
|
|
3
|
-
A Ruby interface to the X API.
|
3
|
+
#### A Ruby interface to the X API.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Install the gem and add to the application's Gemfile
|
7
|
+
Install the gem and add to the application's Gemfile:
|
8
8
|
|
9
|
-
|
9
|
+
bundle add x
|
10
10
|
|
11
|
-
|
11
|
+
Or, if Bundler is not being used to manage dependencies:
|
12
12
|
|
13
|
-
|
13
|
+
gem install x
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
+
First, obtain X credentails from https://developer.x.com.
|
18
|
+
|
17
19
|
```ruby
|
18
|
-
|
20
|
+
x_credentials = {
|
19
21
|
api_key: "INSERT YOUR X API KEY HERE",
|
20
22
|
api_key_secret: "INSERT YOUR X API KEY SECRET HERE",
|
21
|
-
access_token: "INSERT YOUR X
|
22
|
-
access_token_secret: "INSERT YOUR X
|
23
|
+
access_token: "INSERT YOUR X ACCESS TOKEN HERE",
|
24
|
+
access_token_secret: "INSERT YOUR X ACCESS TOKEN SECRET HERE",
|
23
25
|
}
|
24
26
|
|
25
|
-
# Initialize X API client with OAuth credentials
|
26
|
-
x_client = X::Client.new(**
|
27
|
+
# Initialize an X API client with your OAuth credentials
|
28
|
+
x_client = X::Client.new(**x_credentials)
|
27
29
|
|
28
|
-
#
|
30
|
+
# Get data about yourself
|
29
31
|
x_client.get("users/me")
|
30
32
|
# {"data"=>{"id"=>"7505382", "name"=>"Erik Berlin", "username"=>"sferik"}}
|
31
33
|
|
@@ -33,18 +35,18 @@ x_client.get("users/me")
|
|
33
35
|
tweet = x_client.post("tweets", '{"text":"Hello, World! (from @gem)"}')
|
34
36
|
# {"data"=>{"edit_history_tweet_ids"=>["1234567890123456789"], "id"=>"1234567890123456789", "text"=>"Hello, World! (from @gem)"}}
|
35
37
|
|
36
|
-
# Delete
|
38
|
+
# Delete the tweet you just posted
|
37
39
|
x_client.delete("tweets/#{tweet["data"]["id"]}")
|
38
40
|
# {"data"=>{"deleted"=>true}}
|
39
41
|
|
40
42
|
# Initialize an API v1.1 client
|
41
|
-
v1_client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **
|
43
|
+
v1_client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **x_credentials)
|
42
44
|
|
43
45
|
# Request your account settings
|
44
46
|
v1_client.get("account/settings.json")
|
45
47
|
|
46
48
|
# Initialize an X Ads API client
|
47
|
-
ads_client = X::Client.new(base_url: "https://ads-api.twitter.com/12/", **
|
49
|
+
ads_client = X::Client.new(base_url: "https://ads-api.twitter.com/12/", **x_credentials)
|
48
50
|
|
49
51
|
# Request your ad accounts
|
50
52
|
ads_client.get("accounts")
|
@@ -52,29 +54,61 @@ ads_client.get("accounts")
|
|
52
54
|
|
53
55
|
## History and Philosophy
|
54
56
|
|
55
|
-
This library is a rewrite of the [Twitter Ruby library](https://github.com/sferik/twitter). Over 16 years, that library ballooned to over 3,000 lines of code (plus 7,500 lines of tests). At the time of writing, this library is about 300 lines of code (plus 200 test lines) and I’d like to keep it that way. That doesn’t mean new features won’t be added over time, but the benefits of
|
57
|
+
This library is a rewrite of the [Twitter Ruby library](https://github.com/sferik/twitter). Over 16 years of development, that library ballooned to over 3,000 lines of code (plus 7,500 lines of tests). At the time of writing, this library is about 300 lines of code (plus 200 test lines) and I’d like to keep it that way. That doesn’t mean new features won’t be added over time, but the benefits of more code must be weighted against the benefits of less:
|
56
58
|
|
57
59
|
* Less code is easier to maintain.
|
58
60
|
* Less code means fewer bugs.
|
59
61
|
* Less code runs faster.
|
60
62
|
|
61
|
-
In the immortal words of [Ezra Zygmuntowicz](https://github.com/ezmobius) and his [Merb](https://github.com/merb) project (may they both rest in peace):
|
63
|
+
In the immortal words of [Ezra Zygmuntowicz](https://github.com/ezmobius) and his [Merb](https://github.com/merb) project (may they both rest in peace):
|
64
|
+
|
65
|
+
> No code is faster than no code.
|
62
66
|
|
63
|
-
The
|
67
|
+
The fastest code is the code that is never executed because it doesn’t exist. That principle should apply not just to this library itself but to third-party dependencies. At present, this library has no runtime dependencies and I’d like to keep it that way.
|
64
68
|
|
65
|
-
|
69
|
+
The tests for the previous version of this library executed in about 2 seconds. That sounds pretty fast until you see that tests for this library run in 2 hundredths of a second. This means you can automatically run the tests any time you write a file and receive immediate feedback. For such of workflows, 2 seconds feels painfully slow.
|
66
70
|
|
67
|
-
This
|
71
|
+
This code is not littered with comments that are intended to generate documentation. Rather, this code is intended to be simple enough to serve as its own documentation. If you want to understand how something works, don’t read the documentation—it might be wrong—read the code. The code is always right.
|
68
72
|
|
69
73
|
## Development
|
70
74
|
|
71
|
-
|
75
|
+
1. Checkout and repo:
|
76
|
+
|
77
|
+
git checkout git@github.com:sferik/x-ruby.git
|
78
|
+
|
79
|
+
2. Enter the repo’s directory:
|
80
|
+
|
81
|
+
cd x-ruby
|
82
|
+
|
83
|
+
3. Install dependencies via Bundler:
|
84
|
+
|
85
|
+
bin/setup
|
86
|
+
|
87
|
+
4. Run the default Rake task to ensure all tests pass:
|
88
|
+
|
89
|
+
bundle exec rake
|
72
90
|
|
73
|
-
|
91
|
+
5. Create a new branch for your feature or bug fix:
|
92
|
+
|
93
|
+
git checkout -b my-new-branch
|
74
94
|
|
75
95
|
## Contributing
|
76
96
|
|
77
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/x.
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/x-ruby.
|
98
|
+
|
99
|
+
Pull requests will only be accepted if they meet all the following criteria:
|
100
|
+
|
101
|
+
1. Code must conform to [Standard Ruby](https://github.com/standardrb/standard). This can be verified with:
|
102
|
+
|
103
|
+
bundle exec rake standard
|
104
|
+
|
105
|
+
2. For any new code paths, tests must be added to maintain 100% C0 code coverage. This can be verified with:
|
106
|
+
|
107
|
+
bundle exec rake test
|
108
|
+
|
109
|
+
3. For any new classes or methods, RBS type signatures must be added (to sig/x.rbs). This can be verified with:
|
110
|
+
|
111
|
+
bundle exec rake steep
|
78
112
|
|
79
113
|
## License
|
80
114
|
|
data/Rakefile
CHANGED
@@ -12,4 +12,12 @@ require "rubocop/rake_task"
|
|
12
12
|
|
13
13
|
RuboCop::RakeTask.new
|
14
14
|
|
15
|
-
|
15
|
+
require "steep"
|
16
|
+
require "steep/cli"
|
17
|
+
|
18
|
+
desc "Type check with Steep"
|
19
|
+
task :steep do
|
20
|
+
Steep::CLI.new(argv: ["check"], stdout: $stdout, stderr: $stderr, stdin: $stdin).run
|
21
|
+
end
|
22
|
+
|
23
|
+
task default: %i[test rubocop standard steep]
|
data/Steepfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
target :lib do
|
2
|
+
signature "sig"
|
3
|
+
check "lib"
|
4
|
+
library "base64"
|
5
|
+
library "cgi"
|
6
|
+
library "forwardable"
|
7
|
+
library "json"
|
8
|
+
library "net-http"
|
9
|
+
library "openssl"
|
10
|
+
library "securerandom"
|
11
|
+
library "uri"
|
12
|
+
configure_code_diagnostics(Steep::Diagnostic::Ruby.strict)
|
13
|
+
end
|
data/lib/x/authenticator.rb
CHANGED
@@ -1,43 +1,81 @@
|
|
1
|
-
require "
|
2
|
-
require "
|
1
|
+
require "base64"
|
2
|
+
require "cgi"
|
3
|
+
require "json"
|
4
|
+
require "openssl"
|
5
|
+
require "securerandom"
|
6
|
+
require "uri"
|
3
7
|
|
4
8
|
module X
|
5
|
-
# Handles OAuth
|
9
|
+
# Handles OAuth authentication
|
6
10
|
class Authenticator
|
7
|
-
|
11
|
+
attr_accessor :api_key, :api_key_secret, :access_token, :access_token_secret
|
8
12
|
|
9
|
-
|
13
|
+
OAUTH_VERSION = "1.0".freeze
|
14
|
+
OAUTH_SIGNATURE_METHOD = "HMAC-SHA1".freeze
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def_delegator :@consumer, :key=, :api_key=
|
17
|
-
def_delegator :@consumer, :secret, :api_key_secret
|
18
|
-
def_delegator :@consumer, :secret=, :api_key_secret=
|
19
|
-
|
20
|
-
def initialize(bearer_token:, api_key:, api_key_secret:, access_token:, access_token_secret:)
|
21
|
-
if bearer_token
|
22
|
-
@bearer_token = bearer_token
|
23
|
-
else
|
24
|
-
initialize_oauth(api_key, api_key_secret, access_token, access_token_secret)
|
25
|
-
end
|
16
|
+
def initialize(api_key, api_key_secret, access_token, access_token_secret)
|
17
|
+
@api_key = api_key
|
18
|
+
@api_key_secret = api_key_secret
|
19
|
+
@access_token = access_token
|
20
|
+
@access_token_secret = access_token_secret
|
26
21
|
end
|
27
22
|
|
28
23
|
def sign!(request)
|
29
|
-
|
24
|
+
method = request.method
|
25
|
+
uri, query_params = split_uri(request.uri)
|
26
|
+
request.add_field("Authorization", oauth_header(method, uri, query_params))
|
30
27
|
end
|
31
28
|
|
32
29
|
private
|
33
30
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
def split_uri(uri)
|
32
|
+
uri_base = uri.path.to_s
|
33
|
+
query_params = URI.decode_www_form(uri.query.to_s).to_h
|
34
|
+
[uri_base, query_params]
|
35
|
+
end
|
36
|
+
|
37
|
+
def oauth_header(method, uri, query_params)
|
38
|
+
oauth_params = default_oauth_params
|
39
|
+
all_params = query_params.merge(oauth_params)
|
40
|
+
oauth_params["oauth_signature"] = generate_signature(method, uri, all_params)
|
41
|
+
formatted_oauth_header(oauth_params)
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_oauth_params
|
45
|
+
{
|
46
|
+
"oauth_consumer_key" => @api_key,
|
47
|
+
"oauth_nonce" => SecureRandom.hex,
|
48
|
+
"oauth_signature_method" => OAUTH_SIGNATURE_METHOD,
|
49
|
+
"oauth_timestamp" => Time.now.utc.to_i.to_s,
|
50
|
+
"oauth_token" => @access_token,
|
51
|
+
"oauth_version" => OAUTH_VERSION
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate_signature(method, uri, params)
|
56
|
+
Base64.encode64(OpenSSL::HMAC.digest(
|
57
|
+
OpenSSL::Digest.new("sha1"),
|
58
|
+
signing_key,
|
59
|
+
signature_base_string(method, uri, params)
|
60
|
+
)).chomp
|
61
|
+
end
|
62
|
+
|
63
|
+
def signature_base_string(method, uri, params)
|
64
|
+
encoded_params = encode_params(params)
|
65
|
+
"#{method}&#{CGI.escape(uri)}&#{CGI.escape(encoded_params)}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def encode_params(params)
|
69
|
+
# TODO: Replace CGI.escape with CGI.escapeURIComponent when support for Ruby 3.1 is dropped
|
70
|
+
params.sort.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&").gsub("+", "%20")
|
71
|
+
end
|
72
|
+
|
73
|
+
def signing_key
|
74
|
+
"#{CGI.escape(@api_key_secret)}&#{CGI.escape(@access_token_secret)}"
|
75
|
+
end
|
38
76
|
|
39
|
-
|
40
|
-
|
77
|
+
def formatted_oauth_header(params)
|
78
|
+
"OAuth #{params.sort.map { |k, v| "#{k}=\"#{CGI.escape(v.to_s)}\"" }.join(", ")}"
|
41
79
|
end
|
42
80
|
end
|
43
81
|
end
|
data/lib/x/client.rb
CHANGED
@@ -11,25 +11,23 @@ module X
|
|
11
11
|
extend Forwardable
|
12
12
|
include ClientDefaults
|
13
13
|
|
14
|
-
def_delegators :@authenticator, :
|
15
|
-
def_delegators :@authenticator, :
|
16
|
-
def_delegators :@connection, :base_url, :open_timeout, :read_timeout, :write_timeout
|
17
|
-
def_delegators :@connection, :base_url=, :open_timeout=, :read_timeout=, :write_timeout=
|
14
|
+
def_delegators :@authenticator, :api_key, :api_key_secret, :access_token, :access_token_secret
|
15
|
+
def_delegators :@authenticator, :api_key=, :api_key_secret=, :access_token=, :access_token_secret=
|
16
|
+
def_delegators :@connection, :base_url, :open_timeout, :read_timeout, :write_timeout, :debug_output
|
17
|
+
def_delegators :@connection, :base_url=, :open_timeout=, :read_timeout=, :write_timeout=, :debug_output=
|
18
18
|
def_delegators :@request_builder, :content_type, :user_agent
|
19
19
|
def_delegators :@request_builder, :content_type=, :user_agent=
|
20
20
|
def_delegators :@response_handler, :array_class, :object_class
|
21
21
|
def_delegators :@response_handler, :array_class=, :object_class=
|
22
22
|
|
23
|
-
def initialize(
|
23
|
+
def initialize(api_key:, api_key_secret:, access_token:, access_token_secret:,
|
24
24
|
base_url: DEFAULT_BASE_URL, content_type: DEFAULT_CONTENT_TYPE, user_agent: DEFAULT_USER_AGENT,
|
25
25
|
open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, write_timeout: DEFAULT_WRITE_TIMEOUT,
|
26
|
-
array_class: DEFAULT_ARRAY_CLASS, object_class: DEFAULT_OBJECT_CLASS)
|
27
|
-
@authenticator = Authenticator.new(
|
28
|
-
|
29
|
-
@
|
30
|
-
|
31
|
-
@request_builder = RequestBuilder.new(content_type: content_type, user_agent: user_agent)
|
32
|
-
@response_handler = ResponseHandler.new(array_class: array_class, object_class: object_class)
|
26
|
+
debug_output: nil, array_class: DEFAULT_ARRAY_CLASS, object_class: DEFAULT_OBJECT_CLASS)
|
27
|
+
@authenticator = Authenticator.new(api_key, api_key_secret, access_token, access_token_secret)
|
28
|
+
@connection = Connection.new(base_url, open_timeout, read_timeout, write_timeout, debug_output: debug_output)
|
29
|
+
@request_builder = RequestBuilder.new(content_type, user_agent)
|
30
|
+
@response_handler = ResponseHandler.new(array_class, object_class)
|
33
31
|
end
|
34
32
|
|
35
33
|
def get(endpoint)
|
@@ -51,10 +49,9 @@ module X
|
|
51
49
|
private
|
52
50
|
|
53
51
|
def send_request(http_method, endpoint, body = nil)
|
54
|
-
request = @request_builder.build(
|
55
|
-
|
56
|
-
|
57
|
-
@response_handler.handle(response: response)
|
52
|
+
request = @request_builder.build(@authenticator, http_method, base_url, endpoint, body: body)
|
53
|
+
response = @connection.send_request(request)
|
54
|
+
@response_handler.handle(response)
|
58
55
|
end
|
59
56
|
end
|
60
57
|
end
|
data/lib/x/connection.rb
CHANGED
@@ -14,17 +14,19 @@ module X
|
|
14
14
|
|
15
15
|
def_delegators :@http_client, :open_timeout, :read_timeout, :write_timeout
|
16
16
|
def_delegators :@http_client, :open_timeout=, :read_timeout=, :write_timeout=
|
17
|
+
def_delegator :@http_client, :set_debug_output, :debug_output=
|
17
18
|
|
18
|
-
def initialize(
|
19
|
-
self.base_url =
|
20
|
-
@http_client = Net::HTTP.new(
|
21
|
-
@http_client.use_ssl =
|
19
|
+
def initialize(url, open_timeout, read_timeout, write_timeout, debug_output: nil)
|
20
|
+
self.base_url = url
|
21
|
+
@http_client = Net::HTTP.new(base_url.host, base_url.port) if base_url.host
|
22
|
+
@http_client.use_ssl = base_url.scheme == "https"
|
22
23
|
@http_client.open_timeout = open_timeout
|
23
24
|
@http_client.read_timeout = read_timeout
|
24
25
|
@http_client.write_timeout = write_timeout
|
26
|
+
@http_client.set_debug_output(debug_output) if debug_output
|
25
27
|
end
|
26
28
|
|
27
|
-
def send_request(request
|
29
|
+
def send_request(request)
|
28
30
|
@http_client.request(request)
|
29
31
|
rescue *NETWORK_ERRORS => e
|
30
32
|
raise NetworkError, "Network error: #{e.message}"
|
@@ -36,5 +38,9 @@ module X
|
|
36
38
|
|
37
39
|
@base_url = uri
|
38
40
|
end
|
41
|
+
|
42
|
+
def debug_output
|
43
|
+
@http_client.instance_variable_get(:@debug_output)
|
44
|
+
end
|
39
45
|
end
|
40
46
|
end
|
data/lib/x/errors/error.rb
CHANGED
@@ -8,7 +8,7 @@ module X
|
|
8
8
|
include ClientDefaults
|
9
9
|
attr_reader :object
|
10
10
|
|
11
|
-
def initialize(msg
|
11
|
+
def initialize(msg, response:, array_class: DEFAULT_ARRAY_CLASS, object_class: DEFAULT_OBJECT_CLASS)
|
12
12
|
if json_response?(response)
|
13
13
|
@object = JSON.parse(response.body, array_class: array_class, object_class: object_class)
|
14
14
|
end
|
@@ -6,21 +6,21 @@ module X
|
|
6
6
|
class TooManyRequestsError < ClientError
|
7
7
|
include ClientDefaults
|
8
8
|
|
9
|
-
def initialize(msg
|
9
|
+
def initialize(msg, response:, array_class: DEFAULT_ARRAY_CLASS, object_class: DEFAULT_OBJECT_CLASS)
|
10
10
|
@response = response
|
11
11
|
super
|
12
12
|
end
|
13
13
|
|
14
14
|
def limit
|
15
|
-
@response
|
15
|
+
@response.fetch("x-rate-limit-limit", 0).to_i
|
16
16
|
end
|
17
17
|
|
18
18
|
def remaining
|
19
|
-
@response
|
19
|
+
@response.fetch("x-rate-limit-remaining", 0).to_i
|
20
20
|
end
|
21
21
|
|
22
22
|
def reset_at
|
23
|
-
Time.at(@response
|
23
|
+
Time.at(@response.fetch("x-rate-limit-reset", 0).to_i).utc
|
24
24
|
end
|
25
25
|
|
26
26
|
def reset_in
|
data/lib/x/request_builder.rb
CHANGED
@@ -13,13 +13,13 @@ module X
|
|
13
13
|
|
14
14
|
attr_accessor :content_type, :user_agent
|
15
15
|
|
16
|
-
def initialize(content_type
|
16
|
+
def initialize(content_type, user_agent)
|
17
17
|
@content_type = content_type
|
18
18
|
@user_agent = user_agent
|
19
19
|
end
|
20
20
|
|
21
|
-
def build(authenticator
|
22
|
-
url = URI.join(base_url, endpoint)
|
21
|
+
def build(authenticator, http_method, base_url, endpoint, body: nil)
|
22
|
+
url = URI.join(base_url.to_s, endpoint)
|
23
23
|
request = create_request(http_method, url, body)
|
24
24
|
add_authorization(request, authenticator)
|
25
25
|
add_content_type(request)
|
@@ -40,11 +40,7 @@ module X
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def add_authorization(request, authenticator)
|
43
|
-
|
44
|
-
request.add_field("Authorization", "Bearer #{authenticator.bearer_token}")
|
45
|
-
else
|
46
|
-
authenticator.sign!(request)
|
47
|
-
end
|
43
|
+
authenticator.sign!(request)
|
48
44
|
end
|
49
45
|
|
50
46
|
def add_content_type(request)
|
data/lib/x/response_handler.rb
CHANGED
@@ -10,20 +10,18 @@ module X
|
|
10
10
|
|
11
11
|
attr_accessor :array_class, :object_class
|
12
12
|
|
13
|
-
def initialize(array_class
|
13
|
+
def initialize(array_class, object_class)
|
14
14
|
@array_class = array_class
|
15
15
|
@object_class = object_class
|
16
16
|
end
|
17
17
|
|
18
|
-
def handle(response
|
18
|
+
def handle(response)
|
19
19
|
if successful_json_response?(response)
|
20
20
|
return JSON.parse(response.body, array_class: array_class, object_class: object_class)
|
21
21
|
end
|
22
22
|
|
23
23
|
error_class = ERROR_CLASSES[response.code.to_i] || Error
|
24
24
|
error_message = "#{response.code} #{response.message}"
|
25
|
-
raise error_class, error_message if empty_response_body?(response)
|
26
|
-
|
27
25
|
raise error_class.new(error_message, response: response, array_class: array_class, object_class: object_class)
|
28
26
|
end
|
29
27
|
|
@@ -32,9 +30,5 @@ module X
|
|
32
30
|
def successful_json_response?(response)
|
33
31
|
response.is_a?(Net::HTTPSuccess) && response.body && response["content-type"] == DEFAULT_CONTENT_TYPE
|
34
32
|
end
|
35
|
-
|
36
|
-
def empty_response_body?(response)
|
37
|
-
response.body.nil? || response.body.empty?
|
38
|
-
end
|
39
33
|
end
|
40
34
|
end
|
data/lib/x/version.rb
CHANGED
data/sig/x.rbs
CHANGED
@@ -1,4 +1,145 @@
|
|
1
1
|
module X
|
2
|
-
VERSION:
|
3
|
-
|
2
|
+
VERSION: Gem::Version
|
3
|
+
|
4
|
+
class Authenticator
|
5
|
+
OAUTH_VERSION: String
|
6
|
+
OAUTH_SIGNATURE_METHOD: String
|
7
|
+
|
8
|
+
attr_accessor api_key: String
|
9
|
+
attr_accessor api_key_secret: String
|
10
|
+
attr_accessor access_token: String
|
11
|
+
attr_accessor access_token_secret: String
|
12
|
+
def initialize: (String api_key, String api_key_secret, String access_token, String access_token_secret) -> void
|
13
|
+
def sign!: (Net::HTTPRequest request) -> void
|
14
|
+
|
15
|
+
private
|
16
|
+
def split_uri: (URI::Generic uri) -> [String, Hash[String, String]]
|
17
|
+
def oauth_header: (String method, String uri, Hash[String, String] query_params) -> String
|
18
|
+
def default_oauth_params: -> Hash[String, String]
|
19
|
+
def generate_signature: (String method, String uri, Hash[String, String] params) -> String
|
20
|
+
def signature_base_string: (String method, String uri, Hash[String, String] params) -> String
|
21
|
+
def encode_params: (Hash[String, String] params) -> String
|
22
|
+
def signing_key: -> String
|
23
|
+
def formatted_oauth_header: (Hash[String, String] params) -> String
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClientDefaults
|
27
|
+
DEFAULT_BASE_URL: String
|
28
|
+
DEFAULT_CONTENT_TYPE: String
|
29
|
+
DEFAULT_OPEN_TIMEOUT: Integer
|
30
|
+
DEFAULT_READ_TIMEOUT: Integer
|
31
|
+
DEFAULT_WRITE_TIMEOUT: Integer
|
32
|
+
DEFAULT_USER_AGENT: String
|
33
|
+
DEFAULT_ARRAY_CLASS: Class
|
34
|
+
DEFAULT_OBJECT_CLASS: Class
|
35
|
+
end
|
36
|
+
|
37
|
+
class Error < StandardError
|
38
|
+
include ClientDefaults
|
39
|
+
|
40
|
+
attr_reader object: untyped
|
41
|
+
def initialize: (String msg, response: Net::HTTPResponse, ?array_class: Class, ?object_class: Class) -> void
|
42
|
+
|
43
|
+
private
|
44
|
+
def json_response?: (Net::HTTPResponse response) -> bool
|
45
|
+
end
|
46
|
+
|
47
|
+
class ClientError < Error
|
48
|
+
end
|
49
|
+
|
50
|
+
class BadRequestError < ClientError
|
51
|
+
end
|
52
|
+
|
53
|
+
class AuthenticationError < ClientError
|
54
|
+
end
|
55
|
+
|
56
|
+
class ForbiddenError < ClientError
|
57
|
+
end
|
58
|
+
|
59
|
+
class NotFoundError < ClientError
|
60
|
+
end
|
61
|
+
|
62
|
+
class TooManyRequestsError < ClientError
|
63
|
+
include ClientDefaults
|
64
|
+
@response: Net::HTTPResponse
|
65
|
+
|
66
|
+
def initialize: (String msg, response: Net::HTTPResponse, ?array_class: Class, ?object_class: Class) -> void
|
67
|
+
def limit: -> Integer
|
68
|
+
def remaining: -> Integer
|
69
|
+
def reset_at: -> Time
|
70
|
+
def reset_in: -> Integer?
|
71
|
+
end
|
72
|
+
|
73
|
+
class ServerError < Error
|
74
|
+
end
|
75
|
+
|
76
|
+
class ServiceUnavailableError < ServerError
|
77
|
+
end
|
78
|
+
|
79
|
+
module Errors
|
80
|
+
ERROR_CLASSES: Hash[Integer, singleton(AuthenticationError) | singleton(BadRequestError) | singleton(ForbiddenError) | singleton(NotFoundError) | singleton(ServerError) | singleton(ServiceUnavailableError) | singleton(TooManyRequestsError)]
|
81
|
+
NETWORK_ERRORS: Array[(singleton(::Errno::ECONNREFUSED) | singleton(::Net::OpenTimeout) | singleton(::Net::ReadTimeout))]
|
82
|
+
end
|
83
|
+
|
84
|
+
class NetworkError < Error
|
85
|
+
end
|
86
|
+
|
87
|
+
class Connection
|
88
|
+
extend Forwardable
|
89
|
+
include Errors
|
90
|
+
@http_client: Net::HTTP
|
91
|
+
|
92
|
+
attr_reader base_url: URI::Generic
|
93
|
+
def initialize: (URI::Generic | String url, Float | Integer open_timeout, Float | Integer read_timeout, Float | Integer write_timeout, ?debug_output: IO?) -> void
|
94
|
+
def send_request: (Net::HTTPRequest request) -> Net::HTTPResponse
|
95
|
+
def base_url=: (URI::Generic | String new_base_url) -> URI::Generic
|
96
|
+
def debug_output: -> IO?
|
97
|
+
end
|
98
|
+
|
99
|
+
class RequestBuilder
|
100
|
+
HTTP_METHODS: Hash[::Symbol, (singleton(::Net::HTTP::Get) | singleton(::Net::HTTP::Post) | singleton(::Net::HTTP::Put) | singleton(::Net::HTTP::Delete))]
|
101
|
+
|
102
|
+
attr_accessor content_type: String
|
103
|
+
attr_accessor user_agent: String
|
104
|
+
def initialize: (String content_type, String user_agent) -> void
|
105
|
+
def build: (Authenticator authenticator, :delete | :get | :post | :put http_method, URI::Generic base_url, String endpoint, ?body: nil) -> (Net::HTTPRequest)
|
106
|
+
|
107
|
+
private
|
108
|
+
def create_request: (:delete | :get | :post | :put http_method, URI::Generic url, nil body) -> (Net::HTTPRequest)
|
109
|
+
def add_authorization: (Net::HTTPRequest request, Authenticator authenticator) -> void
|
110
|
+
def add_content_type: (Net::HTTPRequest request) -> void
|
111
|
+
def add_user_agent: (Net::HTTPRequest request) -> void
|
112
|
+
end
|
113
|
+
|
114
|
+
class ResponseHandler
|
115
|
+
include Errors
|
116
|
+
include ClientDefaults
|
117
|
+
|
118
|
+
attr_accessor array_class: Class
|
119
|
+
attr_accessor object_class: Class
|
120
|
+
def initialize: (Class array_class, Class object_class) -> void
|
121
|
+
def handle: (Net::HTTPResponse response) -> untyped
|
122
|
+
|
123
|
+
private
|
124
|
+
def successful_json_response?: (Net::HTTPResponse response) -> bool
|
125
|
+
end
|
126
|
+
|
127
|
+
class Client
|
128
|
+
extend Forwardable
|
129
|
+
include ClientDefaults
|
130
|
+
@authenticator: Authenticator
|
131
|
+
@connection: Connection
|
132
|
+
@request_builder: RequestBuilder
|
133
|
+
@response_handler: ResponseHandler
|
134
|
+
|
135
|
+
attr_reader base_url: URI::Generic
|
136
|
+
def initialize: (api_key: String, api_key_secret: String, access_token: String, access_token_secret: String, ?base_url: URI::Generic | String, ?content_type: String, ?user_agent: String, ?open_timeout: Float | Integer, ?read_timeout: Float | Integer, ?write_timeout: Float | Integer, ?debug_output: IO?, ?array_class: Class, ?object_class: Class) -> void
|
137
|
+
def get: (String endpoint) -> untyped
|
138
|
+
def post: (String endpoint, ?nil body) -> untyped
|
139
|
+
def put: (String endpoint, ?nil body) -> untyped
|
140
|
+
def delete: (String endpoint) -> untyped
|
141
|
+
|
142
|
+
private
|
143
|
+
def send_request: (:delete | :get | :post | :put http_method, String endpoint, ?nil body) -> untyped
|
144
|
+
end
|
4
145
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: x
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.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-
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: oauth
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.1'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.1'
|
11
|
+
date: 2023-09-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
27
13
|
description:
|
28
14
|
email:
|
29
15
|
- sferik@gmail.com
|
@@ -37,6 +23,7 @@ files:
|
|
37
23
|
- LICENSE.txt
|
38
24
|
- README.md
|
39
25
|
- Rakefile
|
26
|
+
- Steepfile
|
40
27
|
- lib/x.rb
|
41
28
|
- lib/x/authenticator.rb
|
42
29
|
- lib/x/client.rb
|
@@ -57,12 +44,12 @@ files:
|
|
57
44
|
- lib/x/response_handler.rb
|
58
45
|
- lib/x/version.rb
|
59
46
|
- sig/x.rbs
|
60
|
-
homepage: https://github.
|
47
|
+
homepage: https://sferik.github.io/x-ruby
|
61
48
|
licenses:
|
62
49
|
- MIT
|
63
50
|
metadata:
|
64
51
|
allowed_push_host: https://rubygems.org
|
65
|
-
homepage_uri: https://github.
|
52
|
+
homepage_uri: https://sferik.github.io/x-ruby
|
66
53
|
source_code_uri: https://github.com/sferik/x-ruby
|
67
54
|
changelog_uri: https://github.com/sferik/x-ruby/blob/master/CHANGELOG.md
|
68
55
|
rubygems_mfa_required: 'true'
|