Skip to content

Commit

Permalink
Combine everything into a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 3, 2023
1 parent ae08913 commit c11b145
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 132 deletions.
2 changes: 0 additions & 2 deletions lib/x.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
require_relative "x/version"
require_relative "x/errors"
require_relative "x/client"
13 changes: 12 additions & 1 deletion lib/x/client.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
require "json"
require "net/http"
require "oauth"
require_relative "version"

module X
class Error < ::StandardError; end
class ClientError < Error; end
class AuthenticationError < ClientError; end
class BadRequestError < ClientError; end
class ForbiddenError < ClientError; end
class NotFoundError < ClientError; end
class TooManyRequestsError < ClientError; end
class ServerError < Error; end
class ServiceUnavailableError < ServerError; end

# HTTP client that handles authentication and requests
class Client
DEFAULT_BASE_URL = "https://api.twitter.com/2/".freeze
DEFAULT_USER_AGENT = "X-Client/#{VERSION} Ruby/#{RUBY_VERSION}".freeze
DEFAULT_USER_AGENT = "X-Client/#{X::Version} Ruby/#{RUBY_VERSION}".freeze

def initialize(bearer_token: nil, api_key: nil, api_key_secret: nil, access_token: nil, access_token_secret: nil,
base_url: DEFAULT_BASE_URL, user_agent: DEFAULT_USER_AGENT)
Expand Down
11 changes: 0 additions & 11 deletions lib/x/errors.rb

This file was deleted.

38 changes: 37 additions & 1 deletion lib/x/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
module X
VERSION = "0.2.0".freeze
# The version of this library
module Version
module_function

def major
0
end

def minor
2
end

def patch
0
end

def pre
nil
end

def to_h
{
major: major,
minor: minor,
patch: patch,
pre: pre
}
end

def to_a
[major, minor, patch, pre].compact
end

def to_s
to_a.join(".")
end
end
end
18 changes: 16 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@
require "minitest/autorun"
require "webmock/minitest"

def client_bearer
X::Client.new(bearer_token: "TEST_BEARER_TOKEN")
end

def client_oauth
api_key = "TEST_API_KEY"
api_key_secret = "TEST_API_KEY_SECRET"
access_token = "TEST_ACCESS_TOKEN"
access_token_secret = "TEST_ACCESS_TOKEN_SECRET"

X::Client.new(api_key: api_key, api_key_secret: api_key_secret, access_token: access_token,
access_token_secret: access_token_secret)
end

def stub_bearer_request(method, endpoint, status)
stub_request(method, "https://api.twitter.com/2/#{endpoint}")
.with(headers: { "Authorization" => "Bearer #{@bearer_token}" })
.with(headers: { "Authorization" => /Bearer/ })
.to_return(status: status, body: {}.to_json)
end

def stub_oauth_request(method, endpoint, status)
stub_request(method, "https://api.twitter.com/2/#{endpoint}")
.with(headers: { "Authorization" => /OAuth/ }) # Match any Authorization header containing 'OAuth'
.with(headers: { "Authorization" => /OAuth/ })
.to_return(status: status, body: {}.to_json)
end
102 changes: 85 additions & 17 deletions test/x/test_client.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
require "test_helper"

class ClientTest < Minitest::Test
def setup
@bearer_token = "TEST_BEARER_TOKEN"
@api_key = "TEST_API_KEY"
@api_key_secret = "TEST_API_KEY_SECRET"
@access_token = "TEST_ACCESS_TOKEN"
@access_token_secret = "TEST_ACCESS_TOKEN_SECRET"

@client_bearer = X::Client.new(bearer_token: @bearer_token)
@client_oauth = X::Client.new(api_key: @api_key, api_key_secret: @api_key_secret, access_token: @access_token,
access_token_secret: @access_token_secret)
end

def test_bearer_token_get_request_success
stub_bearer_request(:get, "tweets", 200)

response = @client_bearer.get("tweets")
response = client_bearer.get("tweets")

assert_instance_of Hash, response
assert_requested :get, "https://api.twitter.com/2/tweets"
Expand All @@ -25,7 +13,7 @@ def test_bearer_token_get_request_success
def test_oauth_get_request_success
stub_oauth_request(:get, "tweets", 200)

response = @client_oauth.get("tweets")
response = client_oauth.get("tweets")

assert_instance_of Hash, response
assert_requested :get, "https://api.twitter.com/2/tweets"
Expand All @@ -35,7 +23,7 @@ def test_post_request_success
stub_oauth_request(:post, "tweets", 200)

body = '{"text":"Hello, World!"}'
response = @client_oauth.post("tweets", body)
response = client_oauth.post("tweets", body)

assert_instance_of Hash, response
assert_requested :post, "https://api.twitter.com/2/tweets"
Expand All @@ -45,7 +33,7 @@ def test_put_request_success
stub_oauth_request(:put, "tweets/123", 200)

body = '{"text":"Updated tweet!"}'
response = @client_oauth.put("tweets/123", body)
response = client_oauth.put("tweets/123", body)

assert_instance_of Hash, response
assert_requested :put, "https://api.twitter.com/2/tweets/123"
Expand All @@ -54,7 +42,7 @@ def test_put_request_success
def test_delete_request_success
stub_oauth_request(:delete, "tweets/123", 200)

response = @client_oauth.delete("tweets/123")
response = client_oauth.delete("tweets/123")

assert_instance_of Hash, response
assert_requested :delete, "https://api.twitter.com/2/tweets/123"
Expand All @@ -65,4 +53,84 @@ def test_missing_credentials
X::Client.new
end
end

def test_bearer_token_get_request_failure
stub_bearer_request(:get, "invalid_endpoint", 404)

assert_raises StandardError do
client_bearer.get("invalid_endpoint")
end
end

def test_oauth_get_request_failure
stub_oauth_request(:get, "invalid_endpoint", 404)

assert_raises StandardError do
client_oauth.get("invalid_endpoint")
end
end

def test_bad_request
stub_oauth_request(:get, "tweets", 400)

assert_raises X::BadRequestError do
client_oauth.get("tweets")
end
end

def test_unauthorized_request
stub_oauth_request(:get, "tweets", 401)

assert_raises X::AuthenticationError do
client_oauth.get("tweets")
end
end

def test_forbidden_request
stub_oauth_request(:get, "tweets", 403)

assert_raises X::ForbiddenError do
client_oauth.get("tweets")
end
end

def test_not_found_request
stub_oauth_request(:get, "tweets", 404)

assert_raises X::NotFoundError do
client_oauth.get("tweets")
end
end

def test_too_many_requests
stub_oauth_request(:get, "tweets", 429)

assert_raises X::TooManyRequestsError do
client_oauth.get("tweets")
end
end

def test_server_error
stub_oauth_request(:get, "tweets", 500)

assert_raises X::ServerError do
client_oauth.get("tweets")
end
end

def test_service_unavailable_error
stub_oauth_request(:get, "tweets", 503)

assert_raises X::ServiceUnavailableError do
client_oauth.get("tweets")
end
end

def test_unexpected_response
stub_oauth_request(:get, "tweets", 600)

assert_raises X::Error do
client_oauth.get("tweets")
end
end
end
95 changes: 0 additions & 95 deletions test/x/test_error.rb

This file was deleted.

28 changes: 26 additions & 2 deletions test/x/test_version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
require "test_helper"

class TestX < Minitest::Test
class VersionTest < Minitest::Test
def test_major_version
refute_nil X::Version.major
end

def test_minor_version
refute_nil X::Version.minor
end

def test_patch_version
refute_nil X::Version.patch
end

def test_to_h
assert_kind_of Hash, X::Version.to_h
end

def test_to_a
assert_kind_of Array, X::Version.to_a
end

def test_to_s
assert_kind_of String, X::Version.to_s
end

def test_that_it_has_a_version_number
refute_nil ::X::VERSION
refute_nil ::X::Version
end
end
2 changes: 1 addition & 1 deletion x.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require_relative "lib/x/version"

Gem::Specification.new do |spec|
spec.name = "x"
spec.version = X::VERSION
spec.version = X::Version
spec.authors = ["Erik Berlin"]
spec.email = ["[email protected]"]

Expand Down

0 comments on commit c11b145

Please sign in to comment.