the_one_api 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: eab63a106b4736953d1eaf773cfef554b7fe9a83984a7cd41596451e31c01834
4
+ data.tar.gz: 6be4a461d074122e439f9a6bb08cdb3018917d81c0a717d43239610e9b58d7d8
5
+ SHA512:
6
+ metadata.gz: fade730fc33c878c40b9d1fad97f63385d64217ddfd7fb3a0902288476638b1cbae460e253d8fb8977d7761bbae069edf8799ee46285281561f28987eb23dbec
7
+ data.tar.gz: 7ce96e59be310e63a6c70f04e44ec11f26ef04c61aba60ef3d8e61728b127359387e0d404fd942dcf501a0c1bf65567d5ae2d30522d393d3d93b27d10f0dd126
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ NewCops: disable
3
+ SuggestExtensions: false
4
+ TargetRubyVersion: 3.0
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Joshua Samberg
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,127 @@
1
+ # TheOneApi
2
+
3
+ This is a Ruby gem that provides an SDK for interacting with [The One API](https://the-one-api.dev/)
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ You don't need this source code unless you want to modify the gem. If you just want to use the package, just run:
10
+
11
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+
14
+ If you want to build the gem from source:
15
+
16
+ $ gem build UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
17
+
18
+ ### Installing with Bundler
19
+
20
+ Install the gem and add to the application's Gemfile by executing:
21
+
22
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
23
+
24
+ ### Requirements
25
+
26
+ + Ruby 3.0+
27
+
28
+ ## Usage
29
+
30
+ Interact with [The One API](https://the-one-api.dev/) through an instance of `TheOneApi::Client`. To create one you must specify your API Key, which is required by most endpoints:
31
+
32
+ ```ruby
33
+ require 'the_one_api'
34
+
35
+ # Replace "your-api-key" with your actual API key
36
+ client = TheOneApi::Client.new("your-API-key")
37
+ ```
38
+
39
+ ### Querying Data
40
+
41
+ Query root level API resources (e.g. `/movie`) through corresponding named methods on the client. API entities are returned as Ruby objects.
42
+
43
+ ```ruby
44
+ require 'the_one_api'
45
+
46
+ client = TheOneApi::Client.new("your-API-key")
47
+
48
+ # list movies with the movie resource (the /movie endpoint)
49
+ movies = client.movie.list
50
+ movies.count
51
+ # => 8
52
+
53
+ # find one movie by its id with the movie resource (the /movie/{id} endpoint)
54
+ movie = client.movie.find("5cd95395de30eff6ebccde57")
55
+ #=> #<TheOneApi::Movie _id: "5cd95395de30eff6ebccde57", name: "The Hobbit Se..
56
+
57
+ # properties of API entites available as snake-ized named members on the returned objects
58
+ movie.runtime_in_minutes
59
+ #=> 462
60
+
61
+ ```
62
+
63
+ ### Errors
64
+
65
+ The client will raise `TheOneApi::HttpResponseError` when a request returns a bad status code:
66
+
67
+ ```ruby
68
+ require 'the_one_api'
69
+
70
+ client = TheOneApi::Client.new("your-API-key")
71
+
72
+ begin
73
+ client.movie.find("does_not_exist") # raises an error
74
+ rescue TheOneApi::HttpResponseError => e
75
+ puts "Failed to find movie with id 'does_not_exist', error code: #{e.status}, error: #{e.message}"
76
+ end
77
+ ```
78
+
79
+ **Note:** [The One API](https://the-one-api.dev/) DOES NOT return standard HTTP errors, for example the above example returns a `500 Internal Server Error`, not a `404 Not Found`.
80
+
81
+ ## Supported API Features
82
+
83
+ ### Currently Supported Root Level Endpoints/Operations
84
+
85
+ + `/movie` - list all movies, find a movie by id
86
+
87
+ ### Currently Unsupported Features
88
+
89
+ The SDK currently does not explicitly support
90
+ + root level endpoints not listed above
91
+ + nested endpoints - e.g. `/movie/{id}/quote`
92
+ + Pagination, Sorting and Filtering as described [here](https://the-one-api.dev/documentation#5)
93
+
94
+ However, you can supply custom query parameters as a hash to any of the named resource methods on `TheOneApi::Client`, for example:
95
+
96
+ ```ruby
97
+ require 'the_one_api'
98
+
99
+ client = TheOneApi::Client.new("your-API-key")
100
+
101
+ # this request will have special_parameter=value added to its query string
102
+ client.movie.list(special_parameter: "value")
103
+ ```
104
+
105
+
106
+ ## Development
107
+
108
+ After checking out the repo:
109
+ + run `bin/setup` to install dependencies
110
+ + run `bundle exec rake test` to run the tests
111
+ + run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine:
114
+ + run `bundle exec rake install`
115
+
116
+ To release a new version
117
+ + update the version number in `version.rb`
118
+ + run `bundle exec rake release`
119
+ * this 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).
120
+
121
+ <!-- ## Contributing
122
+
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/the_one_api. -->
124
+
125
+ ## License
126
+
127
+ 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]
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "the_one_api/resource_wrapper"
4
+ require "the_one_api/resources/movie"
5
+
6
+ module TheOneApi
7
+ # A client for The One Api
8
+ class Client
9
+ BASE_URL = "https://the-one-api.dev/v2/"
10
+
11
+ Flexirest::Base.base_url = BASE_URL
12
+ Flexirest::Base.faraday_config do |faraday|
13
+ faraday.adapter(:net_http)
14
+ end
15
+
16
+ def initialize(api_key)
17
+ @api_key = api_key
18
+ end
19
+
20
+ def movie
21
+ @movie ||= ResourceWrapper.new(Movie, @api_key)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TheOneApi
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TheOneApi
4
+ # Our own error class to throw for HTTP response errors
5
+ #
6
+ # Good encapsulation to not expose our consumers to the
7
+ # error types of the underlying libraries unintentionally
8
+ class HttpResponseError < TheOneApi::Error
9
+ attr_reader :status
10
+
11
+ def initialize(status, msg = nil)
12
+ @status = status
13
+ super(msg)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TheOneApi
4
+ # A client for The One Api
5
+ class ResourceWrapper
6
+ def initialize(wrapped_resource, api_key)
7
+ @wrapped_resource = wrapped_resource
8
+ @api_key = api_key
9
+ end
10
+
11
+ def method_missing(method, *args, &block)
12
+ super unless @wrapped_resource._calls.key?(method)
13
+
14
+ # merge the api key in with the supplied
15
+ # arguments so that the request can be authenticated
16
+ args_with_api_key = if args[0].is_a?(Hash)
17
+ (args[0]).merge({ api_key: @api_key })
18
+ elsif args[0]
19
+ { id: args[0] }.merge({ api_key: @api_key })
20
+ else
21
+ { api_key: @api_key }
22
+ end
23
+
24
+ @wrapped_resource.send(method, args_with_api_key, &block)
25
+ end
26
+
27
+ def respond_to_missing?(method, include_private = false)
28
+ @wrapped_resource._calls.key?(method) || super
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flexirest"
4
+
5
+ module TheOneApi
6
+ # Helper class to implement and wire in the response
7
+ # transformations needed by BaseResource (see below)
8
+ class BaseResourceProxy < Flexirest::ProxyBase
9
+ # if this is a GET request to a single, non-nested resource by id
10
+ get(+"\\/[^\\/]+\\/[^\\/]+\\z") do
11
+ response = passthrough
12
+ translate(response) do |body|
13
+ # give back the entity instead of an array of one entity
14
+ body["docs"] && body["docs"][0] ? body["docs"][0] : body
15
+ end
16
+ end
17
+ # if this is a GET request to a root-level resource
18
+ get(+"\\/[^\\/]+\\z") do
19
+ response = passthrough
20
+ translate(response) do |body|
21
+ # give back the array of returned entities instead of the envelope
22
+ # structure that surrounds it
23
+ body["docs"] || body
24
+ end
25
+ end
26
+ end
27
+
28
+ # Common functionality for all API resources, including:
29
+ # + Adding authentication header
30
+ # + Unwrapping the `docs` array
31
+ # when listing all entities from a resource
32
+ # + Unwrapping the one item returned in the `docs` array
33
+ # when requesting a single entity by id
34
+ class BaseResource < Flexirest::Base
35
+ proxy BaseResourceProxy
36
+
37
+ before_request :add_authentication_header
38
+ after_request :handle_http_error
39
+
40
+ private
41
+
42
+ def add_authentication_header(_name, request)
43
+ return unless request.get_params[:api_key]
44
+
45
+ request.headers["Authorization"] = "Bearer #{request.get_params[:api_key]}"
46
+ request.get_params.delete(:api_key)
47
+ end
48
+
49
+ def handle_http_error(_name, response)
50
+ return unless response.status && response.status != 200
51
+
52
+ raise HttpResponseError.new(response.status,
53
+ response.body["message"])
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "the_one_api/resources/base_resource"
4
+
5
+ module TheOneApi
6
+ # Resource definition for Movies
7
+ class Movie < BaseResource
8
+ before_request :add_authentication_header
9
+
10
+ get :list, "/movie", rubify_names: true
11
+ get :find, "/movie/:id", rubify_names: true
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TheOneApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "the_one_api/version"
4
+
5
+ # Module containing everything our Gem exports
6
+ module TheOneApi
7
+ autoload :Client, "the_one_api/client"
8
+
9
+ autoload :HttpResponseError, "the_one_api/http_response_error"
10
+ autoload :Error, "the_one_api/error"
11
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_one_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Samberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flexirest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.11.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.11.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: dotenv
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.8.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.8.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.52'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 6.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 6.1.0
97
+ description: An Ruby gem providing an SDK for interacting with The One API
98
+ email:
99
+ - iceberg901@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - ".tool-versions"
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/the_one_api.rb
111
+ - lib/the_one_api/client.rb
112
+ - lib/the_one_api/error.rb
113
+ - lib/the_one_api/http_response_error.rb
114
+ - lib/the_one_api/resource_wrapper.rb
115
+ - lib/the_one_api/resources/base_resource.rb
116
+ - lib/the_one_api/resources/movie.rb
117
+ - lib/the_one_api/version.rb
118
+ homepage: https://github.com/iceberg901/joshua-samberg-sdk
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '3.0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.4.10
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: An Ruby gem providing an SDK for interacting with The One API
141
+ test_files: []