elephant_in_the_room-the_one_api_sdk 1.9.9

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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +47 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +17 -0
  6. data/Gemfile.lock +71 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +67 -0
  9. data/Rakefile +12 -0
  10. data/design.md +18 -0
  11. data/elephant_in_the_room-the_one_api_sdk.gemspec +29 -0
  12. data/lib/elephant_in_the_room/the_one_api_sdk/api_paths/all_paths.rb +15 -0
  13. data/lib/elephant_in_the_room/the_one_api_sdk/api_paths/base.rb +60 -0
  14. data/lib/elephant_in_the_room/the_one_api_sdk/api_paths/movies.rb +46 -0
  15. data/lib/elephant_in_the_room/the_one_api_sdk/constants.rb +10 -0
  16. data/lib/elephant_in_the_room/the_one_api_sdk/errors.rb +45 -0
  17. data/lib/elephant_in_the_room/the_one_api_sdk/modified_sdk.rb +48 -0
  18. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/filters.rb +25 -0
  19. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/get_request.rb +51 -0
  20. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/json.rb +23 -0
  21. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/paginate.rb +32 -0
  22. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/pipeline_modifiers.rb +40 -0
  23. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/raise_http_errors.rb +26 -0
  24. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/response_body.rb +22 -0
  25. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/retry.rb +21 -0
  26. data/lib/elephant_in_the_room/the_one_api_sdk/pipeline/set_path.rb +22 -0
  27. data/lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/exponential_backoff.rb +41 -0
  28. data/lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/one_try.rb +11 -0
  29. data/lib/elephant_in_the_room/the_one_api_sdk/version.rb +7 -0
  30. data/lib/elephant_in_the_room/the_one_api_sdk.rb +57 -0
  31. data/sig/michael_taylor_sdk.rbs +4 -0
  32. metadata +75 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8345c8ab9be39afa54224bad4f1d183dcafcedba66e93d7733ff07beb2e72965
4
+ data.tar.gz: c36cd0c16c5502f0d42ae437e17cfde04556e681bc1ecd9447b3d21dd0c621a4
5
+ SHA512:
6
+ metadata.gz: 4fc3b34b2f691d13de1c972b3a7b9767b346d846d737aa35fe1c01ed8a7124c0a222c492ff3c721be2f83dd401c87c4268c703e259019e1de1c56645adf219b1
7
+ data.tar.gz: 3c27d27f66b840cdcd965f735ea5d4fb032c0be4a7fae1b987a98cf57de4eaa99db86ede3136aab274bb74e479bfb1af43b102874b10031771eb20ba3808d6f6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,47 @@
1
+ plugins:
2
+ - rubocop-rspec
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/ClassAndModuleChildren:
18
+ Enabled: false
19
+
20
+ Style/TrailingCommaInHashLiteral:
21
+ Enabled: true
22
+ EnforcedStyleForMultiline: comma
23
+
24
+ Style/TrailingCommaInArrayLiteral:
25
+ Enabled: true
26
+ EnforcedStyleForMultiline: comma
27
+
28
+ Layout/LineLength:
29
+ Max: 120
30
+
31
+ Metrics/AbcSize:
32
+ Exclude:
33
+ - spec/**/*.rb
34
+
35
+ Metrics/BlockLength:
36
+ Exclude:
37
+ - spec/**/*.rb
38
+
39
+ Metrics/MethodLength:
40
+ Exclude:
41
+ - spec/**/*.rb
42
+
43
+ RSpec/ExampleLength:
44
+ Enabled: false
45
+
46
+ RSpec/MultipleExpectations:
47
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.2
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ group :test, :development do
8
+ gem "rake", "~> 13.2"
9
+
10
+ gem "rspec", "~> 3.13"
11
+
12
+ gem "rubocop", "~> 1.72"
13
+
14
+ gem "rubocop-rake", "~> 0.7.1"
15
+
16
+ gem "rubocop-rspec", "~> 3.5"
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ elephant_in_the_room-the_one_api_sdk (1.9.9)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.6.0)
11
+ json (2.10.1)
12
+ language_server-protocol (3.17.0.4)
13
+ lint_roller (1.1.0)
14
+ parallel (1.26.3)
15
+ parser (3.3.7.1)
16
+ ast (~> 2.4.1)
17
+ racc
18
+ racc (1.8.1)
19
+ rainbow (3.1.1)
20
+ rake (13.2.1)
21
+ regexp_parser (2.10.0)
22
+ rspec (3.13.0)
23
+ rspec-core (~> 3.13.0)
24
+ rspec-expectations (~> 3.13.0)
25
+ rspec-mocks (~> 3.13.0)
26
+ rspec-core (3.13.3)
27
+ rspec-support (~> 3.13.0)
28
+ rspec-expectations (3.13.3)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.13.0)
31
+ rspec-mocks (3.13.2)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.13.0)
34
+ rspec-support (3.13.2)
35
+ rubocop (1.72.2)
36
+ json (~> 2.3)
37
+ language_server-protocol (~> 3.17.0.2)
38
+ lint_roller (~> 1.1.0)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.3.0.2)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 2.9.3, < 3.0)
43
+ rubocop-ast (>= 1.38.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 2.4.0, < 4.0)
46
+ rubocop-ast (1.38.0)
47
+ parser (>= 3.3.1.0)
48
+ rubocop-rake (0.7.1)
49
+ lint_roller (~> 1.1)
50
+ rubocop (>= 1.72.1)
51
+ rubocop-rspec (3.5.0)
52
+ lint_roller (~> 1.1)
53
+ rubocop (~> 1.72, >= 1.72.1)
54
+ ruby-progressbar (1.13.0)
55
+ unicode-display_width (3.1.4)
56
+ unicode-emoji (~> 4.0, >= 4.0.4)
57
+ unicode-emoji (4.0.4)
58
+
59
+ PLATFORMS
60
+ x86_64-linux
61
+
62
+ DEPENDENCIES
63
+ elephant_in_the_room-the_one_api_sdk!
64
+ rake (~> 13.2)
65
+ rspec (~> 3.13)
66
+ rubocop (~> 1.72)
67
+ rubocop-rake (~> 0.7.1)
68
+ rubocop-rspec (~> 3.5)
69
+
70
+ BUNDLED WITH
71
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Michael Taylor
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,67 @@
1
+ # ElephantInTheRoom::TheOneApiSdk
2
+
3
+ ## Installation
4
+
5
+ Install the gem and add to the application's Gemfile by executing:
6
+
7
+ $ bundle add elephant_in_the_room-the_one_api_sdk
8
+
9
+ If bundler is not being used to manage dependencies, install the gem by executing:
10
+
11
+ $ gem install elephant_in_the_room-the_one_api_sdk
12
+
13
+ ## Usage
14
+
15
+ An access key is required to use this SDK. Visit [The One API sign up](https://the-one-api.dev/sign-up) to get one.
16
+
17
+ Start using the SDK by passing the access key to get an instance of the SDK.
18
+
19
+ the_one = ElephantInTheRoom::TheOneApiSdk::TheOne.new("YOUR_ACCESS_KEY")
20
+
21
+ Access one type of data by calling the appropriate method on the SDK, such as `.movies` to get information about one or more movies.
22
+
23
+ the_one.movies.list # Get all movies
24
+ the_one.movies.get("#{movie_id}") # Get one movie by ID
25
+
26
+ The SDK behavior can be changed with modifier methods. These can be chained.
27
+
28
+ # Return at most 10 results
29
+ the_one.paginated(limit: 10).movies.list
30
+
31
+ # Try to make the call up to 3 times
32
+ exponential_backoff = ElephantInTheRoom::TheOneApiSdk::RetryStrategy::ExponentialBackoff.new(3)
33
+ the_one.with_retry_strategy(exponential_backoff).movies.list
34
+
35
+ # Both modifiers active
36
+ the_one.paginated(limit: 10).with_retry_strategy(exponential_backoff).movies.list
37
+
38
+ # Use modifiers more than once
39
+ the_one.paginated(limit: 10).with_retry_strategy(exponential_backoff) do |modified_sdk|
40
+ modified_sdk.movies.list
41
+ modified_sdk.movies.get("#{movie_id}")
42
+ end
43
+
44
+ Quotes from a movie can be queried by movie ID or name
45
+
46
+ the_one.movies.quotes_from_movie("#{movie_id}")
47
+ the_one.movies.quotes_from_movie_name("#{movie_name}")
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`.
54
+
55
+ ## Release
56
+
57
+ This gem is released every time code is pushed to the main branch.
58
+
59
+ To manually 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mwtaylor/TheOneApiRubySDK.
64
+
65
+ ## License
66
+
67
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/design.md ADDED
@@ -0,0 +1,18 @@
1
+ This SDK is designed around a pipeline that transforms an SDK method call
2
+ into an HTTP request and transforms the response into an easy to use result.
3
+ Each pipeline stage is defined under the pipeline directory.
4
+
5
+ Each pipeline stage is designed to have a simple transformation on the input and/or output.
6
+ * paginate add pagination parameters to the input and adds the returned pagination properties into the output
7
+ * filter adds query parameters to the input for queries like name=Movie Name
8
+ * json converts the output from a string to a Ruby Hash by parsing the string as JSON
9
+ * response_body pulls the body from the HTTP response and ensures it is present and not empty
10
+ * set_path adds the path information that will later be appended to the base URL
11
+ * retry runs a retry strategy to attempt to resolve server errors
12
+ * raise_http_errors converts the HTTP response classes into errors for 4XX and 5XX response codes
13
+ * get_request is the final stage that makes the HTTP request
14
+
15
+ The main SDK object has methods that provide access to the API paths. These are defined under the api_paths directory.
16
+ Currently there is just `.movies` but it could be expanded to include other paths like `.books` or `.characters`
17
+ by adding new subclasses of ElephantInTheRoom::TheOneApiSdk::ApiPaths::Base. These subclasses only need to define the main path used
18
+ in the API and any custom queries that abstract away complexity from the user by adding filters or combining related calls.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/elephant_in_the_room/the_one_api_sdk/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "elephant_in_the_room-the_one_api_sdk"
7
+ spec.version = ElephantInTheRoom::TheOneApiSdk::VERSION
8
+ spec.authors = ["Michael Taylor"]
9
+ spec.email = ["mwtaylor@users.noreply.github.com"]
10
+
11
+ spec.summary = "SDK for The One API to return data about The Lord of the Rings"
12
+ spec.homepage = "https://github.com/mwtaylor/TheOneApiRubySDK"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["rubygems_mfa_required"] = "true"
17
+ spec.metadata["github_repo"] = "https://github.com/mwtaylor/TheOneApiRubySDK"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/api_paths/movies"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::ApiPaths
6
+ ##
7
+ # Mixin to add all available API paths to the parent object
8
+ module AllPaths
9
+ ##
10
+ # Provides queries related to movies
11
+ def movies
12
+ ElephantInTheRoom::TheOneApiSdk::ApiPaths::Movies.new(@pipeline)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/pipeline_modifiers"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::ApiPaths
6
+ ##
7
+ # Provides common methods for all API paths
8
+ class Base
9
+ include ElephantInTheRoom::TheOneApiSdk::Pipeline::PipelineModifiers
10
+
11
+ attr_reader :pipeline
12
+
13
+ def initialize(pipeline)
14
+ @pipeline = pipeline
15
+ @pipeline = replace_existing_stage(
16
+ @pipeline,
17
+ :set_path,
18
+ ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::SetPath.new(next_stage, path) }
19
+ )
20
+ end
21
+
22
+ ##
23
+ # List all items
24
+ def list
25
+ execute_pipeline(@pipeline, {})
26
+ end
27
+
28
+ def get(id)
29
+ with_path(@pipeline, id_path(id)) do |pipeline|
30
+ execute_pipeline(pipeline, {})
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def id_path(id)
37
+ "#{path}/#{id}"
38
+ end
39
+
40
+ def execute_pipeline(pipeline_initializer, input)
41
+ pipeline = pipeline_initializer.call
42
+ stage_initializers = pipeline[:stages].map { |stage_key| pipeline[stage_key] }
43
+ next_stage = initialize_pipeline_stages(stage_initializers)
44
+ next_stage.execute_http_request(input)
45
+ next_stage.result_hash
46
+ end
47
+
48
+ def initialize_pipeline_stages(stage_initializers)
49
+ next_stage = nil
50
+ stage_initializers.reverse.each do |stage_initializer|
51
+ next_stage = if next_stage.nil?
52
+ stage_initializer.call
53
+ else
54
+ stage_initializer.call(next_stage)
55
+ end
56
+ end
57
+ next_stage
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("base")
4
+
5
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/set_path"
6
+ require "elephant_in_the_room/the_one_api_sdk/constants"
7
+
8
+ module ElephantInTheRoom::TheOneApiSdk::ApiPaths
9
+ ##
10
+ # Queries related to /movie
11
+ class Movies < Base
12
+ include ElephantInTheRoom::TheOneApiSdk::Constants
13
+
14
+ ##
15
+ # Find all quotes for a given movie
16
+ def quotes_from_movie(id)
17
+ with_path(@pipeline, quote_path(id)) do |pipeline|
18
+ execute_pipeline(pipeline, {})
19
+ end
20
+ end
21
+
22
+ ##
23
+ # Lookup a movie by name and get all quotes from it
24
+ def quotes_from_movie_name(name)
25
+ movies = with_filters(@pipeline, ["name=#{name}"]) do |filtered_pipeline|
26
+ with_pagination(filtered_pipeline, limit: 1) do |paginated_pipeline|
27
+ execute_pipeline(paginated_pipeline, {})
28
+ end
29
+ end
30
+ raise "Movie named #{name} not found" if movies[:items].empty?
31
+
32
+ matching_movie = movies[:items][0]
33
+ quotes_from_movie(matching_movie[:_id])
34
+ end
35
+
36
+ protected
37
+
38
+ def path
39
+ MOVIE_PATH
40
+ end
41
+
42
+ def quote_path(id)
43
+ "#{MOVIE_PATH}/#{id}/#{QUOTE_PATH}"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk
4
+ module Constants
5
+ MOVIE_PATH = "movie"
6
+ QUOTE_PATH = "quote"
7
+
8
+ EXPONENTIAL_BACKOFF_TIME_DIVISOR = 20
9
+ end
10
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Errors
4
+ ##
5
+ # Errors related to HTTP response codes
6
+ class HttpError < StandardError
7
+ def initialize(message_prefix, response)
8
+ super("(#{message_prefix}) #{response.code}: #{response.message}")
9
+ end
10
+ end
11
+
12
+ ##
13
+ # Errors in the HTTP 5XX range
14
+ class ServerError < HttpError
15
+ def initialize(response)
16
+ super("Server Error", response)
17
+ end
18
+ end
19
+
20
+ ##
21
+ # Errors in the HTTP 4XX range
22
+ class ClientError < HttpError
23
+ def initialize(response)
24
+ super("Client Error", response)
25
+ end
26
+ end
27
+
28
+ ##
29
+ # Errors related to processing the returned content
30
+ class ContentError < StandardError
31
+ def initialize(msg = nil)
32
+ super
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Error caused by a JSON parser error
38
+ class JsonParseError < ContentError
39
+ end
40
+
41
+ ##
42
+ # Error caused by no content being returned by the server
43
+ class NoContentError < ContentError
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/api_paths/all_paths"
4
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/pipeline_modifiers"
5
+
6
+ module ElephantInTheRoom::TheOneApiSdk
7
+ ##
8
+ # All modifiers that can modify the SDK behavior
9
+ module Modifiers
10
+ ##
11
+ # Replaces the default retry strategy
12
+ def with_retry_strategy(retry_strategy)
13
+ new_retry_stage = lambda { |next_stage|
14
+ ElephantInTheRoom::TheOneApiSdk::Pipeline::Retry.new(next_stage, retry_strategy)
15
+ }
16
+ new_pipeline = replace_existing_stage(@pipeline, :retry, new_retry_stage)
17
+ modified_sdk = ElephantInTheRoom::TheOneApiSdk::ModifiedSdk.new(new_pipeline)
18
+ if block_given?
19
+ yield(modified_sdk)
20
+ else
21
+ modified_sdk
22
+ end
23
+ end
24
+
25
+ def paginated(limit: nil, page: nil, offset: nil)
26
+ with_pagination(@pipeline, limit: limit, page: page, offset: offset) do |paginated_pipeline|
27
+ modified_sdk = ElephantInTheRoom::TheOneApiSdk::ModifiedSdk.new(paginated_pipeline)
28
+ if block_given?
29
+ yield(modified_sdk)
30
+ else
31
+ modified_sdk
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ ##
38
+ # An instance of the SDK with modified behavior
39
+ class ModifiedSdk
40
+ include Modifiers
41
+ include ElephantInTheRoom::TheOneApiSdk::ApiPaths::AllPaths
42
+ include ElephantInTheRoom::TheOneApiSdk::Pipeline::PipelineModifiers
43
+
44
+ def initialize(pipeline)
45
+ @pipeline = pipeline
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
4
+ ##
5
+ # Pipeline stage that adds filters to list requests
6
+ class Filters
7
+ def initialize(next_pipeline_stage, filters)
8
+ @next_pipeline_stage = next_pipeline_stage
9
+ @filters = filters
10
+ end
11
+
12
+ def execute_http_request(request_details)
13
+ request_details[:query_parameters] = {} unless request_details.key?(:query_parameters)
14
+ @filters.each do |filter|
15
+ request_details[:query_parameters][filter] = nil
16
+ end
17
+
18
+ @next_pipeline_stage.execute_http_request(request_details)
19
+ end
20
+
21
+ def result_hash
22
+ @next_pipeline_stage.result_hash
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "net/http"
5
+
6
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
7
+ ##
8
+ # Final pipeline stage that sends an HTTP request to the server
9
+ class GetRequest
10
+ HEADER_AUTHORIZATION = "Authorization"
11
+ AUTHENTICATION_SCHEME_BEARER = "Bearer"
12
+
13
+ def initialize(base_url, authorization_token)
14
+ @base_url = base_url
15
+ @authorization_token = authorization_token
16
+ end
17
+
18
+ def execute_http_request(request_details)
19
+ request = Net::HTTP::Get.new(uri(request_details))
20
+ add_headers(request)
21
+
22
+ @response = Net::HTTP.start(request.uri.hostname, use_ssl: true) do |http|
23
+ http.request(request)
24
+ end
25
+ end
26
+
27
+ def http_response
28
+ @response
29
+ end
30
+
31
+ private
32
+
33
+ def query_string(request_details)
34
+ request_details[:query_parameters]
35
+ .map { |key, value| value.nil? ? key : "#{key}=#{value}" }
36
+ .join("&")
37
+ end
38
+
39
+ def uri(request_details)
40
+ uri = "#{@base_url}/#{request_details[:path]}"
41
+ if request_details.key?(:query_parameters) && !request_details[:query_parameters].empty?
42
+ uri += "?#{query_string(request_details)}"
43
+ end
44
+ URI(uri)
45
+ end
46
+
47
+ def add_headers(request)
48
+ request[HEADER_AUTHORIZATION] = "#{AUTHENTICATION_SCHEME_BEARER} #{@authorization_token}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
6
+ ##
7
+ # Pipeline stage to transform an HTTP body to JSON
8
+ class Json
9
+ def initialize(next_pipeline_stage)
10
+ @next_pipeline_stage = next_pipeline_stage
11
+ end
12
+
13
+ def execute_http_request(request_details)
14
+ @result = @next_pipeline_stage.execute_http_request(request_details)
15
+ end
16
+
17
+ def result_hash
18
+ JSON.parse(@next_pipeline_stage.http_body, symbolize_names: true)
19
+ rescue JSON::ParserError => e
20
+ raise ElephantInTheRoom::TheOneApiSdk::Errors::JsonParseError, "JSON response could not be parsed: #{e.message}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
4
+ ##
5
+ # Pipeline stage that adds pagination parameters and adds pagination details into response
6
+ class Paginate
7
+ def initialize(next_pipeline_stage, limit: nil, page: nil, offset: nil)
8
+ @next_pipeline_stage = next_pipeline_stage
9
+ @limit = limit
10
+ @page = page
11
+ @offset = offset
12
+ end
13
+
14
+ def execute_http_request(request_details)
15
+ request_details[:query_parameters] = {} unless request_details.key?(:query_parameters)
16
+ request_details[:query_parameters]["limit"] = @limit unless @limit.nil?
17
+ request_details[:query_parameters]["page"] = @page unless @page.nil?
18
+ request_details[:query_parameters]["offset"] = @offset unless @offset.nil?
19
+
20
+ @next_pipeline_stage.execute_http_request(request_details)
21
+ end
22
+
23
+ def result_hash
24
+ next_result = @next_pipeline_stage.result_hash
25
+
26
+ {
27
+ items: next_result[:docs],
28
+ pagination: next_result.slice(:total, :limit, :offset, :page, :pages),
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
4
+ ##
5
+ # Common methods to work with pipelines
6
+ module PipelineModifiers
7
+ ##
8
+ # Replace a stage in a pipeline with a modified version with different parameters
9
+ def replace_existing_stage(pipeline, next_stage_key, next_stage_lambda)
10
+ lambda {
11
+ modified_pipeline = pipeline.call
12
+ modified_pipeline[next_stage_key] = next_stage_lambda
13
+ modified_pipeline
14
+ }
15
+ end
16
+
17
+ def with_path(pipeline, path)
18
+ yield replace_existing_stage(
19
+ pipeline,
20
+ :set_path,
21
+ ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::SetPath.new(next_stage, path) }
22
+ )
23
+ end
24
+
25
+ def with_filters(pipeline, filters)
26
+ yield replace_existing_stage(
27
+ pipeline,
28
+ :filter,
29
+ ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::Filters.new(next_stage, filters) }
30
+ )
31
+ end
32
+
33
+ def with_pagination(pipeline, limit: nil, page: nil, offset: nil)
34
+ new_paginate_stage = lambda { |next_stage|
35
+ ElephantInTheRoom::TheOneApiSdk::Pipeline::Paginate.new(next_stage, limit: limit, page: page, offset: offset)
36
+ }
37
+ yield replace_existing_stage(pipeline, :paginate, new_paginate_stage)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/errors"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
6
+ ##
7
+ # Pipeline stage that raises errors when the HTTP response is a 4xx or 5xx error
8
+ class RaiseHttpErrors
9
+ def initialize(next_pipeline_stage)
10
+ @next_pipeline_stage = next_pipeline_stage
11
+ end
12
+
13
+ def execute_http_request(request_details)
14
+ response = @next_pipeline_stage.execute_http_request(request_details)
15
+ if response.is_a? Net::HTTPServerError
16
+ raise ElephantInTheRoom::TheOneApiSdk::Errors::ServerError, response
17
+ elsif response.is_a? Net::HTTPClientError
18
+ raise ElephantInTheRoom::TheOneApiSdk::Errors::ClientError, response
19
+ end
20
+ end
21
+
22
+ def http_response
23
+ @next_pipeline_stage.http_response
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
4
+ ##
5
+ # Pipeline stage to get body content from HTTP response
6
+ class ResponseBody
7
+ def initialize(next_pipeline_stage)
8
+ @next_pipeline_stage = next_pipeline_stage
9
+ end
10
+
11
+ def execute_http_request(request_details)
12
+ @next_pipeline_stage.execute_http_request(request_details)
13
+ end
14
+
15
+ def http_body
16
+ body = @next_pipeline_stage.http_response.body
17
+ raise ElephantInTheRoom::TheOneApiSdk::Errors::NoContentError, "Server response was empty" if body.empty?
18
+
19
+ body
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/retry_strategy/one_try"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
6
+ ## Pipeline stage to run a given retry strategy
7
+ class Retry
8
+ def initialize(next_pipeline_stage, retry_strategy)
9
+ @next_pipeline_stage = next_pipeline_stage
10
+ @retry_strategy = retry_strategy
11
+ end
12
+
13
+ def execute_http_request(request_details)
14
+ @retry_strategy.run(-> { @next_pipeline_stage.execute_http_request(request_details) })
15
+ end
16
+
17
+ def http_response
18
+ @next_pipeline_stage.http_response
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::Pipeline
4
+ ##
5
+ # Pipeline stage to set the path into the HTTP request
6
+ class SetPath
7
+ def initialize(next_pipeline_stage, path)
8
+ @next_pipeline_stage = next_pipeline_stage
9
+ @path = path
10
+ end
11
+
12
+ def execute_http_request(request_details)
13
+ request_details[:path] = @path
14
+
15
+ @next_pipeline_stage.execute_http_request(request_details)
16
+ end
17
+
18
+ def http_response
19
+ @next_pipeline_stage.http_response
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/constants"
4
+
5
+ module ElephantInTheRoom::TheOneApiSdk::RetryStrategy
6
+ ##
7
+ # Retry strategy for exponential backoffs
8
+ #
9
+ # This will rerun an HTTP call up to max_tries times. Each retry will be delayed.
10
+ # The delay will have an exponential backoff that makes each delay about twice as long as the previous delay.
11
+ # The delay will also have a random jitter added to avoid overloading the server from many clients
12
+ # retrying simultaneously.
13
+ class ExponentialBackoff
14
+ include ElephantInTheRoom::TheOneApiSdk::Constants
15
+
16
+ def initialize(max_tries)
17
+ @max_tries = max_tries
18
+ end
19
+
20
+ def run(do_request)
21
+ tries = 0
22
+ begin
23
+ tries += 1
24
+ do_request.call
25
+ rescue ElephantInTheRoom::TheOneApiSdk::Errors::ServerError
26
+ raise if tries == @max_tries
27
+
28
+ jittered_sleep(tries)
29
+
30
+ retry
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def jittered_sleep(tries)
37
+ max_sleep_seconds = Float(2**tries) / EXPONENTIAL_BACKOFF_TIME_DIVISOR
38
+ sleep(rand((0.5 * max_sleep_seconds)..max_sleep_seconds))
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom::TheOneApiSdk::RetryStrategy
4
+ ##
5
+ # The default retry strategy that just runs a request once and passes along any errors
6
+ class OneTry
7
+ def run(do_request)
8
+ do_request.call
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElephantInTheRoom
4
+ module TheOneApiSdk
5
+ VERSION = "1.9.9"
6
+ end
7
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elephant_in_the_room/the_one_api_sdk/version"
4
+
5
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/pipeline_modifiers"
6
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/paginate"
7
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/filters"
8
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/json"
9
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/response_body"
10
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/retry"
11
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/raise_http_errors"
12
+ require "elephant_in_the_room/the_one_api_sdk/pipeline/get_request"
13
+ require "elephant_in_the_room/the_one_api_sdk/api_paths/all_paths"
14
+ require "elephant_in_the_room/the_one_api_sdk/modified_sdk"
15
+ require "elephant_in_the_room/the_one_api_sdk/constants"
16
+
17
+ module ElephantInTheRoom::TheOneApiSdk
18
+ ##
19
+ # Entry point into the SDK
20
+ class TheOne
21
+ include ElephantInTheRoom::TheOneApiSdk::Pipeline::PipelineModifiers
22
+ include ElephantInTheRoom::TheOneApiSdk::ApiPaths::AllPaths
23
+ include ElephantInTheRoom::TheOneApiSdk::Modifiers
24
+
25
+ DEFAULT_BASE_URL = "https://the-one-api.dev/v2"
26
+
27
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
28
+ def initialize(
29
+ access_token,
30
+ base_url: DEFAULT_BASE_URL,
31
+ default_retry_strategy: ElephantInTheRoom::TheOneApiSdk::RetryStrategy::OneTry.new
32
+ )
33
+ raise "Access token must be given as a string" unless access_token.is_a? String
34
+
35
+ @default_pipeline = lambda {
36
+ {
37
+ paginate: ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::Paginate.new(next_stage) },
38
+ filter: ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::Filters.new(next_stage, []) },
39
+ json: ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::Json.new(next_stage) },
40
+ response_body: ->(next_stage) { ElephantInTheRoom::TheOneApiSdk::Pipeline::ResponseBody.new(next_stage) },
41
+ set_path: ->(next_stage) {},
42
+ retry: lambda { |next_stage|
43
+ ElephantInTheRoom::TheOneApiSdk::Pipeline::Retry.new(next_stage, default_retry_strategy)
44
+ },
45
+ raise_http_errors: lambda { |next_stage|
46
+ ElephantInTheRoom::TheOneApiSdk::Pipeline::RaiseHttpErrors.new(next_stage)
47
+ },
48
+ get_request: -> { ElephantInTheRoom::TheOneApiSdk::Pipeline::GetRequest.new(base_url, access_token) },
49
+ stages: %i[paginate filter json response_body set_path retry raise_http_errors get_request],
50
+ }
51
+ }
52
+
53
+ @pipeline = @default_pipeline
54
+ end
55
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
56
+ end
57
+ end
@@ -0,0 +1,4 @@
1
+ module ElephantInTheRoom::TheOneApiSdk
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elephant_in_the_room-the_one_api_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.9.9
5
+ platform: ruby
6
+ authors:
7
+ - Michael Taylor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - mwtaylor@users.noreply.github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".ruby-version"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - design.md
29
+ - elephant_in_the_room-the_one_api_sdk.gemspec
30
+ - lib/elephant_in_the_room/the_one_api_sdk.rb
31
+ - lib/elephant_in_the_room/the_one_api_sdk/api_paths/all_paths.rb
32
+ - lib/elephant_in_the_room/the_one_api_sdk/api_paths/base.rb
33
+ - lib/elephant_in_the_room/the_one_api_sdk/api_paths/movies.rb
34
+ - lib/elephant_in_the_room/the_one_api_sdk/constants.rb
35
+ - lib/elephant_in_the_room/the_one_api_sdk/errors.rb
36
+ - lib/elephant_in_the_room/the_one_api_sdk/modified_sdk.rb
37
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/filters.rb
38
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/get_request.rb
39
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/json.rb
40
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/paginate.rb
41
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/pipeline_modifiers.rb
42
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/raise_http_errors.rb
43
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/response_body.rb
44
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/retry.rb
45
+ - lib/elephant_in_the_room/the_one_api_sdk/pipeline/set_path.rb
46
+ - lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/exponential_backoff.rb
47
+ - lib/elephant_in_the_room/the_one_api_sdk/retry_strategy/one_try.rb
48
+ - lib/elephant_in_the_room/the_one_api_sdk/version.rb
49
+ - sig/michael_taylor_sdk.rbs
50
+ homepage: https://github.com/mwtaylor/TheOneApiRubySDK
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ rubygems_mfa_required: 'true'
55
+ github_repo: https://github.com/mwtaylor/TheOneApiRubySDK
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.0.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.5.11
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: SDK for The One API to return data about The Lord of the Rings
75
+ test_files: []