x 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 480eb23ebeb098bcc5c2d4924279d2d4608f8cf2a8e5eb7897edf45cc0311bc4
4
+ data.tar.gz: c97f4694a91b3d3ad9f60d55001d48080329254d28db75aff74d646264fe38f2
5
+ SHA512:
6
+ metadata.gz: 86adf1889c2e820353c4455a08c443480682d4827addd074a7e1fe3e60937e70b638118d31802c8cc57d457865c917d2f1a83d739448659519f42c9cbede74ed
7
+ data.tar.gz: 714dcea3deff757a2d15548ed1fdbb17749ca5b901e9463a5b3a223120b05974ab792d1c3230406dc07984358a6d9e7c4d781402aa025fa5e257d967ee5ecbcf
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-performance
4
+ - rubocop-rake
5
+
6
+ AllCops:
7
+ NewCops: enable
8
+ TargetRubyVersion: 3.0
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-02
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in x.gemspec
4
+ gemspec
5
+
6
+ gem "minitest", ">= 5.19"
7
+ gem "rake", ">= 13.0.6"
8
+ gem "rubocop", ">= 1.21"
9
+ gem "rubocop-minitest", ">= 0.31"
10
+ gem "rubocop-performance", ">= 1.18"
11
+ gem "rubocop-rake", ">= 0.6"
12
+ gem "webmock", ">= 3.18.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Erik Berlin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # X
2
+
3
+ A Ruby interface to the X 2.0 API.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add x
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install x
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ api_key = "YOUR_X_API_KEY"
19
+ api_key_secret = "YOUR_X_API_KEY_SECRET"
20
+ access_token = "YOUR_X_ACCESS_TOKEN"
21
+ access_token_secret = "YOUR_X_ACCESS_TOKEN_SECRET"
22
+
23
+ x_client = X::Client.new(api_key: api_key, api_key_secret: api_key_secret, access_token: access_token, access_token_secret: access_token_secret)
24
+
25
+ begin
26
+ response = x_client.get("users/me")
27
+ puts JSON.pretty_generate(response)
28
+ rescue StandardError => e
29
+ puts "Error: #{e.message}"
30
+ end
31
+ ```
32
+
33
+ ## Development
34
+
35
+ 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.
36
+
37
+ 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).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/x.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ end
9
+
10
+ require "rubocop/rake_task"
11
+
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: %i[test rubocop]
data/lib/x/client.rb ADDED
@@ -0,0 +1,95 @@
1
+ require "oauth"
2
+ require "json"
3
+ require "net/http"
4
+
5
+ module X
6
+ # Main client that handles HTTP authentication and requests
7
+ class Client
8
+ BASE_URL = "https://api.twitter.com/2/".freeze
9
+ HTTP_METHODS = {
10
+ get: Net::HTTP::Get,
11
+ post: Net::HTTP::Post,
12
+ put: Net::HTTP::Put,
13
+ delete: Net::HTTP::Delete
14
+ }.freeze
15
+
16
+ def initialize(bearer_token: nil, api_key: nil, api_key_secret: nil, access_token: nil, access_token_secret: nil)
17
+ @use_bearer_token = !bearer_token.nil?
18
+
19
+ if @use_bearer_token
20
+ @bearer_token = bearer_token
21
+ else
22
+ unless api_key && api_key_secret && access_token && access_token_secret
23
+ raise ArgumentError, "Missing OAuth credentials."
24
+ end
25
+
26
+ @consumer = OAuth::Consumer.new(api_key, api_key_secret, site: BASE_URL)
27
+ @access_token = OAuth::Token.new(access_token, access_token_secret)
28
+ end
29
+ end
30
+
31
+ def get(endpoint)
32
+ response = send_request(:get, endpoint)
33
+ handle_response(response)
34
+ end
35
+
36
+ def post(endpoint, body = nil)
37
+ response = send_request(:post, endpoint, body)
38
+ handle_response(response)
39
+ end
40
+
41
+ def put(endpoint, body = nil)
42
+ response = send_request(:put, endpoint, body)
43
+ handle_response(response)
44
+ end
45
+
46
+ def delete(endpoint)
47
+ response = send_request(:delete, endpoint)
48
+ handle_response(response)
49
+ end
50
+
51
+ private
52
+
53
+ def send_request(http_method, endpoint, body = nil)
54
+ url = URI.parse(BASE_URL + endpoint)
55
+ http = Net::HTTP.new(url.host, url.port)
56
+ http.use_ssl = true
57
+
58
+ request = create_request(http_method, url, body)
59
+ add_authorization(request)
60
+
61
+ http.request(request)
62
+ end
63
+
64
+ def create_request(http_method, url, body)
65
+ http_method_class = HTTP_METHODS[http_method]
66
+
67
+ raise ArgumentError, "Unsupported HTTP method: #{http_method}" unless http_method_class
68
+
69
+ request = http_method_class.new(url)
70
+ request.body = body if body && http_method != :get
71
+ request
72
+ end
73
+
74
+ def add_authorization(request)
75
+ if @use_bearer_token
76
+ request["Authorization"] = "Bearer #{@bearer_token}"
77
+ else
78
+ @consumer.sign!(request, @access_token)
79
+ end
80
+ end
81
+
82
+ def handle_response(response)
83
+ case response
84
+ when Net::HTTPSuccess
85
+ JSON.parse(response.body)
86
+ when Net::HTTPUnauthorized
87
+ raise X::AuthenticationError, "Authentication failed. Please check your credentials."
88
+ when Net::HTTPServerError
89
+ raise X::ServerError, "An internal server error occurred."
90
+ else
91
+ raise X::Error, "Unexpected response: #{response.code} #{response.message}"
92
+ end
93
+ end
94
+ end
95
+ end
data/lib/x/error.rb ADDED
@@ -0,0 +1,5 @@
1
+ module X
2
+ class Error < StandardError; end
3
+ class AuthenticationError < Error; end
4
+ class ServerError < Error; end
5
+ end
data/lib/x/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module X
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/x.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative "x/version"
2
+ require_relative "x/client"
3
+ require_relative "x/error"
data/sig/x.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module X
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: x
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Berlin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-02 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'
27
+ description:
28
+ email:
29
+ - sferik@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rubocop.yml"
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/x.rb
41
+ - lib/x/client.rb
42
+ - lib/x/error.rb
43
+ - lib/x/version.rb
44
+ - sig/x.rbs
45
+ homepage: https://github.com/sferik/x-ruby
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ allowed_push_host: https://rubygems.org
50
+ homepage_uri: https://github.com/sferik/x-ruby
51
+ source_code_uri: https://github.com/sferik/x-ruby
52
+ changelog_uri: https://github.com/sferik/x-ruby/blob/master/CHANGELOG.md
53
+ rubygems_mfa_required: 'true'
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.12
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A Ruby interface to the X 2.0 API.
73
+ test_files: []