Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 2, 2023
0 parents commit 07d71b0
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.0.6'

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
22 changes: 22 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require:
- rubocop-minitest
- rubocop-performance
- rubocop-rake

AllCops:
NewCops: enable
TargetRubyVersion: 3.0

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

Style/FrozenStringLiteralComment:
Enabled: false
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-08-02

- Initial release
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source "https://rubygems.org"

# Specify your gem's dependencies in x.gemspec
gemspec

gem "minitest", ">= 5.19"
gem "rake", ">= 13.0.6"
gem "rubocop", ">= 1.21"
gem "rubocop-minitest", ">= 0.31"
gem "rubocop-performance", ">= 1.18"
gem "rubocop-rake", ">= 0.6"
gem "webmock", ">= 3.18.1"
81 changes: 81 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
PATH
remote: .
specs:
x (0.1.0)
oauth (~> 1.1)

GEM
remote: https://rubygems.org/
specs:
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
crack (0.4.5)
rexml
hashdiff (1.0.1)
hashie (5.0.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
minitest (5.19.0)
oauth (1.1.0)
oauth-tty (~> 1.0, >= 1.0.1)
snaky_hash (~> 2.0)
version_gem (~> 1.1)
oauth-tty (1.0.5)
version_gem (~> 1.1, >= 1.1.1)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
racc
public_suffix (5.0.3)
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.6)
rubocop (1.55.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.3)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-minitest (0.31.0)
rubocop (>= 1.39, < 2.0)
rubocop-performance (1.18.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.13.0)
snaky_hash (2.0.1)
hashie
version_gem (~> 1.1, >= 1.1.1)
unicode-display_width (2.4.2)
version_gem (1.1.3)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
arm64-darwin-22

DEPENDENCIES
minitest (>= 5.19)
rake (>= 13.0.6)
rubocop (>= 1.21)
rubocop-minitest (>= 0.31)
rubocop-performance (>= 1.18)
rubocop-rake (>= 0.6)
webmock (>= 3.18.1)
x!

BUNDLED WITH
2.4.12
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Erik Berlin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# X

A Ruby interface to the X 2.0 API.

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add x

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install x

## Usage

```ruby
api_key = "YOUR_X_API_KEY"
api_key_secret = "YOUR_X_API_KEY_SECRET"
access_token = "YOUR_X_ACCESS_TOKEN"
access_token_secret = "YOUR_X_ACCESS_TOKEN_SECRET"

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)

begin
response = x_client.get("users/me")
puts JSON.pretty_generate(response)
rescue StandardError => e
puts "Error: #{e.message}"
end
```

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/x.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
14 changes: 14 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[test rubocop]
10 changes: 10 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "x"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
3 changes: 3 additions & 0 deletions lib/x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require_relative "x/version"
require_relative "x/client"
require_relative "x/error"
88 changes: 88 additions & 0 deletions lib/x/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require "oauth"
require "json"
require "net/http"

module X
# Main client that handles HTTP authentication and requests
class Client
BASE_URL = "https://api.twitter.com/2/".freeze
HTTP_METHODS = {
get: Net::HTTP::Get,
post: Net::HTTP::Post,
put: Net::HTTP::Put,
delete: Net::HTTP::Delete
}.freeze

def initialize(bearer_token: nil, api_key: nil, api_key_secret: nil, access_token: nil, access_token_secret: nil)
@use_bearer_token = !bearer_token.nil?

if @use_bearer_token
@bearer_token = bearer_token
else
unless api_key && api_key_secret && access_token && access_token_secret
raise ArgumentError, "Missing OAuth credentials."
end

@consumer = OAuth::Consumer.new(api_key, api_key_secret, site: BASE_URL)
@access_token = OAuth::Token.new(access_token, access_token_secret)
end
end

def get(endpoint)
response = send_request(:get, endpoint)
handle_response(response)
end

def post(endpoint, body = nil)
response = send_request(:post, endpoint, body)
handle_response(response)
end

def put(endpoint, body = nil)
response = send_request(:put, endpoint, body)
handle_response(response)
end

def delete(endpoint)
response = send_request(:delete, endpoint)
handle_response(response)
end

private

def send_request(http_method, endpoint, body = nil)
url = URI.parse(BASE_URL + endpoint)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = create_request(http_method, url, body)
add_authorization(request)

http.request(request)
end

def create_request(http_method, url, body)
http_method_class = HTTP_METHODS[http_method]

raise ArgumentError, "Unsupported HTTP method: #{http_method}" unless http_method_class

request = http_method_class.new(url)
request.body = body if body && http_method != :get
request
end

def add_authorization(request)
if @use_bearer_token
request["Authorization"] = "Bearer #{@bearer_token}"
else
@consumer.sign!(request, @access_token)
end
end

def handle_response(response)
raise "Error #{response.code}: #{response.body}" unless response.code.to_i == 200

JSON.parse(response.body)
end
end
end
3 changes: 3 additions & 0 deletions lib/x/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module X
class Error < StandardError; end
end
3 changes: 3 additions & 0 deletions lib/x/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module X
VERSION = "0.1.0".freeze
end
4 changes: 4 additions & 0 deletions sig/x.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module X
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "x"

require "minitest/autorun"
Loading

0 comments on commit 07d71b0

Please sign in to comment.