Skip to content

Commit

Permalink
Replace Hashie with OpenStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Dec 3, 2023
1 parent 6cf8c14 commit a901413
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
ruby:
- "3.2"
- "3.0"

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
ruby:
- "3.2"
- "3.0"

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/type_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
ruby:
- "3.2"
- "3.0"

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Style/EmptyMethod:
Style/FrozenStringLiteralComment:
Enabled: false

Style/OpenStructUse:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source "https://rubygems.org"
# Specify your gem's dependencies in x.gemspec
gemspec

gem "hashie", ">= 5"
gem "minitest", ">= 5.19"
gem "mutant-license", source: "https://oss:[email protected]"
gem "mutant-minitest", ">= 0.11.24"
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ v1_client = X::Client.new(base_url: "https://api.twitter.com/1.1/", **x_credenti
# Get your account settings
v1_client.get("account/settings.json")

# Initialize an X Ads API client
# Initialize an Ads API client
ads_client = X::Client.new(base_url: "https://ads-api.twitter.com/12/", **x_credentials)

# Get your ad accounts
Expand All @@ -80,6 +80,23 @@ The tests for the previous version of this library executed in about 2 seconds.

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.

## Features

If this entire library is implemented in just 500 lines of code, why should you use it at all vs. writing your own Twitter client that suits your needs? If you feel inspired to do that, don’t let me discourage you, but this library has some advanced features that may not be apparent without diving into the code:

* OAuth 1.0 Revision A
* OAuth 2.0 Bearer Token
* Thread safety
* HTTP redirect following
* HTTP proxy support
* HTTP logging
* HTTP timeout configuration
* HTTP error handling
* Rate limit handling
* Parsing JSON into custom response objects (e.g. OpenStruct)
* Configurable base URLs for accessing different APIs/versions
* Parallel uploading of large media files in chunks

## Sponsorship

The X gem is free to use, but with X API pricing tiers, it actually costs money to develop and maintain. By contributing to the project, you help us:
Expand Down
6 changes: 3 additions & 3 deletions test/x/client_initailization_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "ostruct"
require_relative "../test_helper"
require "hashie"

module X
class ClientInitializationTest < Minitest::Test
Expand Down Expand Up @@ -106,12 +106,12 @@ def test_initialize_connection_options
end

def test_overwrite_defaults
@client = Client.new(base_url: "https://api.twitter.com/1.1/", max_redirects: 0, object_class: Hashie::Mash,
@client = Client.new(base_url: "https://api.twitter.com/1.1/", max_redirects: 0, object_class: OpenStruct,
array_class: Set)

assert_equal "https://api.twitter.com/1.1/", @client.base_url
assert_predicate @client.max_redirects, :zero?
assert_equal Hashie::Mash, @client.object_class
assert_equal OpenStruct, @client.object_class
assert_equal Set, @client.array_class
end

Expand Down
1 change: 0 additions & 1 deletion test/x/connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "hashie"
require "net/http"
require "uri"
require_relative "../test_helper"
Expand Down
6 changes: 3 additions & 3 deletions test/x/response_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "hashie"
require "ostruct"
require_relative "../test_helper"

module X
Expand Down Expand Up @@ -123,12 +123,12 @@ def test_default_response_objects
end

def test_custom_response_objects
response_parser = ResponseParser.new(object_class: Hashie::Mash, array_class: Set)
response_parser = ResponseParser.new(object_class: OpenStruct, array_class: Set)
stub_request(:get, @uri.to_s).to_return(body: '{"array": [1, 2, 2, 3]}',
headers: {"Content-Type" => "application/json"})
mash = response_parser.parse(response: response)

assert_kind_of Hashie::Mash, mash
assert_kind_of OpenStruct, mash
assert_kind_of Set, mash.array
assert_equal Set.new([1, 2, 2, 3]), mash.array
end
Expand Down

0 comments on commit a901413

Please sign in to comment.