juggy-theone-sdk 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: da7f42a21a902e51b523d1d4af3b57db4cef3ca2d22dfd99b9d0c93f629d7300
4
+ data.tar.gz: ca2acfec738fefe0118e4f5167402679189178988a8f1445d7d384662d31bd49
5
+ SHA512:
6
+ metadata.gz: f9d1e5ba53c45fa6998310d9d10e674f10683216b5956063c1e7505925069faa51a06c8f18838d0d3d6b089b933168b2f880322e464b812858d7ea06f6286388
7
+ data.tar.gz: 3f0231c9f9cffde3c03c7d7883855b9137a0b93e18f473c19c49c3dfb594ceb01a83e02ee002e12ebafbe60ded2d20fc05f987bde82de505303366f1947309d6
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Enabled: false
14
+ Max: 120
15
+
16
+ Metrics/AbcSize:
17
+ Enabled: false
18
+
19
+ Metrics/MethodLength:
20
+ Enabled: false
21
+
22
+ Metrics/BlockLength:
23
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in juggy-theone-sdk.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+ gem "webmock", "~> 3.18"
12
+
13
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ juggy-theone-sdk (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.4)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ ast (2.4.2)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
15
+ hashdiff (1.0.1)
16
+ json (2.6.3)
17
+ parallel (1.23.0)
18
+ parser (3.2.2.1)
19
+ ast (~> 2.4.1)
20
+ public_suffix (5.0.1)
21
+ rainbow (3.1.1)
22
+ rake (13.0.6)
23
+ regexp_parser (2.8.0)
24
+ rexml (3.2.5)
25
+ rspec (3.12.0)
26
+ rspec-core (~> 3.12.0)
27
+ rspec-expectations (~> 3.12.0)
28
+ rspec-mocks (~> 3.12.0)
29
+ rspec-core (3.12.2)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-expectations (3.12.3)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-mocks (3.12.5)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-support (3.12.0)
38
+ rubocop (1.50.2)
39
+ json (~> 2.3)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.0.0)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.28.0, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.28.0)
49
+ parser (>= 3.2.1.0)
50
+ ruby-progressbar (1.13.0)
51
+ unicode-display_width (2.4.2)
52
+ webmock (3.18.1)
53
+ addressable (>= 2.8.0)
54
+ crack (>= 0.3.2)
55
+ hashdiff (>= 0.4.0, < 2.0.0)
56
+
57
+ PLATFORMS
58
+ x86_64-darwin-21
59
+
60
+ DEPENDENCIES
61
+ juggy-theone-sdk!
62
+ rake (~> 13.0)
63
+ rspec (~> 3.0)
64
+ rubocop (~> 1.21)
65
+ webmock (~> 3.18)
66
+
67
+ BUNDLED WITH
68
+ 2.3.24
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Julien Guimont
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,81 @@
1
+ # Theone
2
+
3
+ Ruby SDK implementation for [The One API](https://the-one-api.dev/documentation)
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add juggy-theone-sdk
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install juggy-theone-sdk
14
+
15
+ ## Usage
16
+
17
+ Once the gem is installed, you can query movies and quotes like so:
18
+
19
+ ```
20
+ sdk = Theone::Sdk.new(access_token: "my access token")
21
+ ```
22
+
23
+ The SDK can be configured with different endpoint and api version as such
24
+
25
+ ```
26
+ sdk = Theone::Sdk.new(
27
+ access_token: "my access token"
28
+ endpoint: "https://example.com",
29
+ version: "v3"
30
+ )
31
+ ```
32
+ This can be useful if you plan on using a test endpoint or an image hosted elsewhere.
33
+
34
+ Once you have a valid SDK object, you can access Movies:
35
+
36
+ ```
37
+ # get all movies, pagination is handled for you.
38
+ sdk.movies
39
+
40
+ # get a specific movie
41
+ my_movie = sdk.movie(movie_id)
42
+
43
+ # access the movie attributes like so
44
+ my_movie.name
45
+ # or all movie attributes
46
+ my_movie.attributes
47
+ ```
48
+
49
+ Or Quotes:
50
+ ```
51
+ # get all quotes, pagination is handled for you.
52
+ sdk.quotes
53
+
54
+ # get a specific quote
55
+ my_quote = sdk.quote(quote_id)
56
+
57
+ # access the quote attributes like so
58
+ my_quote.name
59
+ # or all quote attributes
60
+ my_quote.attributes
61
+ ```
62
+
63
+ You can also get all quotes from a specific movie:
64
+ ```
65
+ my_movie = sdk.movie(movie_id)
66
+ my_movie.quotes.first
67
+ ```
68
+
69
+ ## Development
70
+
71
+ 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.
72
+
73
+ 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).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/juggy/juggy-theone-sdk.
78
+
79
+ ## License
80
+
81
+ 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,16 @@
1
+ # Design
2
+
3
+ ## Dependencies
4
+
5
+ This gem uses the default Ruby HTTP client and JSON parser. It was a concious decision to avoid forcing the consumer to bring more dependencies in order to use the SDK.
6
+
7
+ ## Object Model
8
+
9
+ The object model uses a base class and can handle any type of data. The base class relies on `method_missing` to link the json attributes to real method calls. There is no further interpretation of the JSON data. In fact each type (Movie or Quote) does not implement specific logic for the data. A differenciation can be added in the futur as the types are loaded and defined.
10
+
11
+ ## Pagination
12
+
13
+ The pagination is handled using an `Enumerator` which makes it transparent to the SDK consumer. A nice side effect of using an `Enumerator` is that the data is lazily loaded. This allows for a better use of resources.
14
+
15
+ It also provides a central place to implement throtling mitigation (as the API is limited to 100 calls per 10 minutes). This would be a futur improvement.
16
+
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/theone/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "juggy-theone-sdk"
7
+ spec.version = Theone::VERSION
8
+ spec.authors = ["Julien Guimont"]
9
+ spec.email = ["julien.guimont@gmail.com"]
10
+
11
+ spec.summary = "SDK implementation for The One API"
12
+ spec.description = "https://the-one-api.dev/documentation"
13
+ spec.homepage = "http://github.com/juggy/juggy-theone-sdk"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "http://github.com/juggy/juggy-theone-sdk"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
data/lib/.DS_Store ADDED
Binary file
Binary file
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Theone
4
+ # Base class to provide access to the json data
5
+ class Base
6
+ attr_reader :attributes, :client
7
+
8
+ def initialize(client, json)
9
+ @client = client
10
+ @attributes = json
11
+ end
12
+
13
+ def method_missing(att, *_args, &_block)
14
+ return attributes["_id"] if att == :id
15
+
16
+ attributes.key?(att.to_s) ? attributes[att.to_s] : super
17
+ end
18
+
19
+ def respond_to_missing?(att, _)
20
+ att = :_id if att == :id
21
+ attributes.key?(att.to_s) || super
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "JSON"
5
+
6
+ module Theone
7
+ # HTTP client to connect to the One API
8
+ class Client
9
+ DEFAULT_ENDPOINT = "https://the-one-api.dev/"
10
+ VERSION = "v2"
11
+ ALLOWED_QUERY_KEYS = %i[page limit offset].freeze
12
+
13
+ attr_reader :access_token, :endpoint, :version
14
+
15
+ def initialize(access_token:, endpoint: DEFAULT_ENDPOINT, version: VERSION)
16
+ @endpoint = endpoint
17
+ @access_token = access_token
18
+ @version = version
19
+ end
20
+
21
+ def api_request(path, query = {})
22
+ query_string = ALLOWED_QUERY_KEYS.intersection(query.keys).map { |k| "#{k}=#{query[k]}" }.join("&")
23
+ uri = URI("#{endpoint}#{version}#{path}?#{query_string}")
24
+
25
+ headers = {
26
+ "Authorization" => "Bearer #{access_token}",
27
+ "Content-Type" => "application/json",
28
+ "Accept" => "application/json"
29
+ }
30
+
31
+ http = Net::HTTP.new(uri.hostname, uri.port)
32
+ http.use_ssl = uri.instance_of? URI::HTTPS
33
+ res = http.get(uri.request_uri, headers)
34
+
35
+ # value (weird name) will raise if the response code is not 200
36
+ res.value
37
+ JSON.parse(res.body)["docs"]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "quote"
5
+ require_relative "paginator"
6
+
7
+ module Theone
8
+ # Movie API def
9
+ class Movie < Base
10
+ BASE_PATH = "/movie"
11
+
12
+ class << self
13
+ def all(client)
14
+ Paginator.paginate(client, BASE_PATH) { |movie_json| Movie.new(client, movie_json) }
15
+ end
16
+
17
+ def one(client, id)
18
+ movie_json = client.api_request("#{BASE_PATH}/#{id}").first
19
+ Movie.new(client, movie_json)
20
+ end
21
+ end
22
+
23
+ def quotes
24
+ Paginator.paginate(client, "#{BASE_PATH}/#{id}/quote") { |quote_json| Quote.new(client, quote_json) }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Theone
4
+ # iterator class to provide transparent pagination
5
+ module Paginator
6
+ def self.paginate(client, path, page: 1, limit: 10, &block)
7
+ Enumerator.new do |y|
8
+ loop do
9
+ data = client.api_request(path, page: page, limit: limit)
10
+
11
+ data.each do |element|
12
+ object = block.call element
13
+ y.yield object
14
+ end
15
+
16
+ # stop if empty
17
+ break if data.empty? || data.size < limit
18
+
19
+ # go to next page
20
+ page += 1
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+ require_relative "paginator"
5
+
6
+ module Theone
7
+ # Quote API def
8
+ class Quote < Base
9
+ BASE_PATH = "/quote"
10
+
11
+ class << self
12
+ def all(client)
13
+ Paginator.paginate(client, BASE_PATH) { |quote_json| Quote.new(client, quote_json) }
14
+ end
15
+
16
+ def one(client, id)
17
+ quote_json = client.api_request("#{BASE_PATH}/#{id}").first
18
+ Quote.new(client, quote_json)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Theone
4
+ VERSION = "0.1.0"
5
+ end
data/lib/theone.rb ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "theone/version"
4
+ require_relative "theone/client"
5
+ require_relative "theone/quote"
6
+ require_relative "theone/movie"
7
+
8
+ module Theone
9
+ # Main SDK entry point
10
+ class Sdk
11
+ attr_reader :client
12
+
13
+ def initialize(options)
14
+ @client = Client.new(options)
15
+ end
16
+
17
+ def movies
18
+ Movie.all(client)
19
+ end
20
+
21
+ def movie(id)
22
+ Movie.one(client, id)
23
+ end
24
+
25
+ def quotes
26
+ Quote.all(client)
27
+ end
28
+
29
+ def quote(id)
30
+ Quote.one(client, id)
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: juggy-theone-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Julien Guimont
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: https://the-one-api.dev/documentation
14
+ email:
15
+ - julien.guimont@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - design.md
28
+ - juggy-theone-sdk.gemspec
29
+ - lib/.DS_Store
30
+ - lib/theone.rb
31
+ - lib/theone/.DS_Store
32
+ - lib/theone/base.rb
33
+ - lib/theone/client.rb
34
+ - lib/theone/movie.rb
35
+ - lib/theone/paginator.rb
36
+ - lib/theone/quote.rb
37
+ - lib/theone/version.rb
38
+ homepage: http://github.com/juggy/juggy-theone-sdk
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ homepage_uri: http://github.com/juggy/juggy-theone-sdk
43
+ source_code_uri: http://github.com/juggy/juggy-theone-sdk
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.6
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: SDK implementation for The One API
63
+ test_files: []