x 0.6.0 → 0.7.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: 7ab2b0ef63fad89db285efc1527b16a3b199691a5cb51013cc55f32fd1fcde56
4
- data.tar.gz: e035f65fea8a8409a69a58f08ade709c2affe7c0afc12f62e6928d095ac2aa7f
3
+ metadata.gz: b4e43c461d1aa0e058f2dfcbb53f85fbcf5ade26e1f4a169a044454d9110f2b9
4
+ data.tar.gz: 267f92093e37ecc6e82c1fdd6cc2eb8615a58e6e586e4eaa63a4d8b890e1afc6
5
5
  SHA512:
6
- metadata.gz: 41f24b7dc58d4d886da758a0d2b1430c68617c2594975c1fd17c9a04c7cde8a826e6672dd03c4be3ae3ba0d0022cfe8d0f6c6b5a97e2df64511a92e56e2c6361
7
- data.tar.gz: ffc98321391ec7b71bed31dc82832086c39a79a76b18d0f0ae284a062d329f49fb96cd66421c8bae3b204a48978cc97a079be54b05883f6e462c2be2827f3a94
6
+ metadata.gz: 6c68483c77e776f13a7178775148ca16046515dad0ce3d259667b49d0194b89714d70fe0bddf9b14b0cd80514e276ad545fbf4913e9b65a8f106ec1e08443fa7
7
+ data.tar.gz: 58490badac233a7233331051fc7f2940e6648cd2c5a7de74261b968656b5617d01eeeea3eddfd514a25bac77628b54d78e09085924993700b8a52dc5817fd248
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.7.0] - 2023-09-02
4
+
5
+ - Remove OAuth gem (7c29bb1)
6
+
3
7
  ## [0.6.0] - 2023-08-30
4
8
 
5
9
  - Add configurable debug output stream for logging (fd2d4b0)
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 by executing:
7
+ Install the gem and add to the application's Gemfile:
8
8
 
9
- $ bundle add x
9
+ bundle add x
10
10
 
11
- If bundler is not being used to manage dependencies, install the gem by executing:
11
+ Or, if Bundler is not being used to manage dependencies:
12
12
 
13
- $ gem install x
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
- x_oauth_credentials = {
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
23
  access_token: "INSERT YOUR X ACCESS TOKEN HERE",
22
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(**x_oauth_credentials)
27
+ # Initialize an X API client with your OAuth credentials
28
+ x_client = X::Client.new(**x_credentials)
27
29
 
28
- # Request yourself
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 a tweet
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/", **x_oauth_credentials)
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/", **x_oauth_credentials)
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 potential new features must be weighed against the benefits of simplicity:
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): “No code is faster than no code.” 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 one dependency ([oauth](https://rubygems.org/gems/oauth)) and I’d like to keep it that way. If anything, it should have fewer.
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 tests for the previous version of this library ran 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. At the same time, we aim to maintain 100% C0 code coverage.
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
- 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—just read the code. The code is always right.
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 project conforms to [Standard Ruby](https://github.com/standardrb/standard). Patches that don’t maintain that standard will not be accepted.
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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/Steepfile CHANGED
@@ -1,9 +1,13 @@
1
1
  target :lib do
2
2
  signature "sig"
3
3
  check "lib"
4
+ library "base64"
5
+ library "cgi"
4
6
  library "forwardable"
5
7
  library "json"
6
8
  library "net-http"
9
+ library "openssl"
10
+ library "securerandom"
7
11
  library "uri"
8
12
  configure_code_diagnostics(Steep::Diagnostic::Ruby.strict)
9
13
  end
@@ -1,27 +1,81 @@
1
- require "oauth"
2
- require "forwardable"
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
9
  # Handles OAuth authentication
6
10
  class Authenticator
7
- extend Forwardable
11
+ attr_accessor :api_key, :api_key_secret, :access_token, :access_token_secret
8
12
 
9
- def_delegator :@access_token, :secret, :access_token_secret
10
- def_delegator :@access_token, :secret=, :access_token_secret=
11
- def_delegator :@access_token, :token, :access_token
12
- def_delegator :@access_token, :token=, :access_token=
13
- def_delegator :@consumer, :key, :api_key
14
- def_delegator :@consumer, :key=, :api_key=
15
- def_delegator :@consumer, :secret, :api_key_secret
16
- def_delegator :@consumer, :secret=, :api_key_secret=
13
+ OAUTH_VERSION = "1.0".freeze
14
+ OAUTH_SIGNATURE_METHOD = "HMAC-SHA1".freeze
17
15
 
18
16
  def initialize(api_key, api_key_secret, access_token, access_token_secret)
19
- @consumer = OAuth::Consumer.new(api_key, api_key_secret, site: ClientDefaults::DEFAULT_BASE_URL)
20
- @access_token = OAuth::Token.new(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
21
21
  end
22
22
 
23
23
  def sign!(request)
24
- @consumer.sign!(request, @access_token)
24
+ method = request.method
25
+ uri, query_params = split_uri(request.uri)
26
+ request.add_field("Authorization", oauth_header(method, uri, query_params))
27
+ end
28
+
29
+ private
30
+
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
76
+
77
+ def formatted_oauth_header(params)
78
+ "OAuth #{params.sort.map { |k, v| "#{k}=\"#{CGI.escape(v.to_s)}\"" }.join(", ")}"
25
79
  end
26
80
  end
27
81
  end
data/lib/x/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "rubygems/version"
2
2
 
3
3
  module X
4
- VERSION = Gem::Version.create("0.6.0")
4
+ VERSION = Gem::Version.create("0.7.0")
5
5
  end
data/sig/x.rbs CHANGED
@@ -1,23 +1,26 @@
1
- module OAuth
2
- class Consumer
3
- def initialize: (String api_key, String api_key_secret, site: String) -> void
4
- def sign!: (Net::HTTPRequest request, OAuth::Token token) -> void
5
- end
6
- class Token
7
- def initialize: (String access_token, String access_token_secret) -> void
8
- end
9
- end
10
-
11
1
  module X
12
2
  VERSION: Gem::Version
13
3
 
14
4
  class Authenticator
15
- extend Forwardable
16
- @consumer: OAuth::Consumer
17
- @access_token: OAuth::Token
5
+ OAUTH_VERSION: String
6
+ OAUTH_SIGNATURE_METHOD: String
18
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
19
12
  def initialize: (String api_key, String api_key_secret, String access_token, String access_token_secret) -> void
20
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
21
24
  end
22
25
 
23
26
  module ClientDefaults
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.6.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-08-30 00:00:00.000000000 Z
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
@@ -58,12 +44,12 @@ files:
58
44
  - lib/x/response_handler.rb
59
45
  - lib/x/version.rb
60
46
  - sig/x.rbs
61
- homepage: https://github.com/sferik/x-ruby
47
+ homepage: https://sferik.github.io/x-ruby
62
48
  licenses:
63
49
  - MIT
64
50
  metadata:
65
51
  allowed_push_host: https://rubygems.org
66
- homepage_uri: https://github.com/sferik/x-ruby
52
+ homepage_uri: https://sferik.github.io/x-ruby
67
53
  source_code_uri: https://github.com/sferik/x-ruby
68
54
  changelog_uri: https://github.com/sferik/x-ruby/blob/master/CHANGELOG.md
69
55
  rubygems_mfa_required: 'true'