lotr 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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +76 -0
- data/LICENSE.txt +21 -0
- data/README.md +99 -0
- data/Rakefile +12 -0
- data/design.md +132 -0
- data/lib/lotr/client.rb +59 -0
- data/lib/lotr/error.rb +68 -0
- data/lib/lotr/response.rb +28 -0
- data/lib/lotr/version.rb +5 -0
- data/lib/lotr.rb +9 -0
- data/sig/lotr.rbs +4 -0
- metadata +104 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fcd353deec930c674b8286a9c513c8599c04052d04832f29ba720dec0d7e80c6
|
|
4
|
+
data.tar.gz: 57fda492ff53fca8c2318e757896abf8fb1a7bc77459e510a6a46ae222dad4e1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 41925c416d4cda61a3e888d75e8273178560ba9ad78384fae57ba99cddfb09a05f9bc67179cf932369e0caac9608ff75773a0deb539a72a47ad690381781423f
|
|
7
|
+
data.tar.gz: 15ba5f558c2b2a5af3bca68a8c87e2d6f7f2823e893b480f72cc53b8e9da423699b331c311cdd855ed33f4510a0370315d304dd537179395d13e55ca4d5f35f7
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
22
|
+
advances of any kind
|
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
24
|
+
* Public or private harassment
|
|
25
|
+
* Publishing others' private information, such as a physical or email
|
|
26
|
+
address, without their explicit permission
|
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
28
|
+
professional setting
|
|
29
|
+
|
|
30
|
+
## Enforcement Responsibilities
|
|
31
|
+
|
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
33
|
+
|
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
35
|
+
|
|
36
|
+
## Scope
|
|
37
|
+
|
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
39
|
+
|
|
40
|
+
## Enforcement
|
|
41
|
+
|
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at fawwazally@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
43
|
+
|
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
45
|
+
|
|
46
|
+
## Enforcement Guidelines
|
|
47
|
+
|
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
49
|
+
|
|
50
|
+
### 1. Correction
|
|
51
|
+
|
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
53
|
+
|
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
55
|
+
|
|
56
|
+
### 2. Warning
|
|
57
|
+
|
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
59
|
+
|
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
61
|
+
|
|
62
|
+
### 3. Temporary Ban
|
|
63
|
+
|
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
65
|
+
|
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
67
|
+
|
|
68
|
+
### 4. Permanent Ban
|
|
69
|
+
|
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
71
|
+
|
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
73
|
+
|
|
74
|
+
## Attribution
|
|
75
|
+
|
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
78
|
+
|
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
|
80
|
+
|
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
|
82
|
+
|
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
lotr (0.1.0)
|
|
5
|
+
httparty
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
addressable (2.8.1)
|
|
11
|
+
public_suffix (>= 2.0.2, < 6.0)
|
|
12
|
+
ast (2.4.2)
|
|
13
|
+
crack (0.4.5)
|
|
14
|
+
rexml
|
|
15
|
+
diff-lcs (1.5.0)
|
|
16
|
+
hashdiff (1.0.1)
|
|
17
|
+
httparty (0.21.0)
|
|
18
|
+
mini_mime (>= 1.0.0)
|
|
19
|
+
multi_xml (>= 0.5.2)
|
|
20
|
+
json (2.6.3)
|
|
21
|
+
mini_mime (1.1.2)
|
|
22
|
+
multi_xml (0.6.0)
|
|
23
|
+
parallel (1.22.1)
|
|
24
|
+
parser (3.2.1.0)
|
|
25
|
+
ast (~> 2.4.1)
|
|
26
|
+
public_suffix (5.0.1)
|
|
27
|
+
rainbow (3.1.1)
|
|
28
|
+
rake (13.0.6)
|
|
29
|
+
regexp_parser (2.7.0)
|
|
30
|
+
rexml (3.2.5)
|
|
31
|
+
rspec (3.12.0)
|
|
32
|
+
rspec-core (~> 3.12.0)
|
|
33
|
+
rspec-expectations (~> 3.12.0)
|
|
34
|
+
rspec-mocks (~> 3.12.0)
|
|
35
|
+
rspec-core (3.12.1)
|
|
36
|
+
rspec-support (~> 3.12.0)
|
|
37
|
+
rspec-expectations (3.12.2)
|
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
39
|
+
rspec-support (~> 3.12.0)
|
|
40
|
+
rspec-mocks (3.12.3)
|
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
42
|
+
rspec-support (~> 3.12.0)
|
|
43
|
+
rspec-support (3.12.0)
|
|
44
|
+
rubocop (1.45.1)
|
|
45
|
+
json (~> 2.3)
|
|
46
|
+
parallel (~> 1.10)
|
|
47
|
+
parser (>= 3.2.0.0)
|
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
49
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
50
|
+
rexml (>= 3.2.5, < 4.0)
|
|
51
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
|
52
|
+
ruby-progressbar (~> 1.7)
|
|
53
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
|
54
|
+
rubocop-ast (1.26.0)
|
|
55
|
+
parser (>= 3.2.1.0)
|
|
56
|
+
ruby-progressbar (1.11.0)
|
|
57
|
+
unicode-display_width (2.4.2)
|
|
58
|
+
vcr (6.1.0)
|
|
59
|
+
webmock (3.18.1)
|
|
60
|
+
addressable (>= 2.8.0)
|
|
61
|
+
crack (>= 0.3.2)
|
|
62
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
|
63
|
+
|
|
64
|
+
PLATFORMS
|
|
65
|
+
x86_64-darwin-22
|
|
66
|
+
|
|
67
|
+
DEPENDENCIES
|
|
68
|
+
lotr!
|
|
69
|
+
rake (~> 13.0)
|
|
70
|
+
rspec (~> 3.0)
|
|
71
|
+
rubocop (~> 1.21)
|
|
72
|
+
vcr (~> 6.1)
|
|
73
|
+
webmock (~> 3.18.1)
|
|
74
|
+
|
|
75
|
+
BUNDLED WITH
|
|
76
|
+
2.3.26
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Fawaz Farid
|
|
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,99 @@
|
|
|
1
|
+
# Lotr
|
|
2
|
+
|
|
3
|
+
Ruby SDK for the [The Lord of the Rings API](https://the-one-api.dev/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
$ bundle add lotr
|
|
10
|
+
|
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
12
|
+
|
|
13
|
+
$ gem install lotr
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Making requests
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require 'lotr'
|
|
21
|
+
|
|
22
|
+
# Initialize the client and provide authentication credentials
|
|
23
|
+
client = Lotr::Client.new(api_key: 'your_api_key')
|
|
24
|
+
|
|
25
|
+
# List all movies
|
|
26
|
+
client.movies # or client.get_movies
|
|
27
|
+
|
|
28
|
+
# Retrieve a movie
|
|
29
|
+
client.movie("5cd95395de30eff6ebccde5b") # or client.get_movie("5cd95395de30eff6ebccde5b")
|
|
30
|
+
|
|
31
|
+
# List all movie quotes for one specific movie
|
|
32
|
+
client.quotes_for_movie("5cd95395de30eff6ebccde5b") # or client.get_quotes_for_movie("5cd95395de30eff6ebccde5b")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Additional query parameters
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
# Paginate movie quotes
|
|
39
|
+
client.quotes_for_movie("5cd95395de30eff6ebccde5b", page: 2, limit: 5)
|
|
40
|
+
|
|
41
|
+
# Sort movie quotes, sort by character asc
|
|
42
|
+
client.quotes_for_movie("5cd95395de30eff6ebccde5b", sort: 'dialog', order: 'asc')
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Consuming resources
|
|
46
|
+
|
|
47
|
+
The API methods return a Resource open struct object which provides dot notation and [] access for fields returned in the API response e.g.
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
movie = client.movie("5cd95395de30eff6ebccde5b")
|
|
51
|
+
puts movie['name']
|
|
52
|
+
# => "The Two Towers"
|
|
53
|
+
puts movie[:academy_award_wins]
|
|
54
|
+
# => 2
|
|
55
|
+
puts movie.runtime_in_minutes
|
|
56
|
+
# => 179
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Error handling
|
|
60
|
+
|
|
61
|
+
The API methods raise an error if the API returns an error response. The error object is a subclass of `Lotr::Error` e.g:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
client = Lotr::Client.new(api_key: "invalidAPKey")
|
|
65
|
+
begin
|
|
66
|
+
client.movies
|
|
67
|
+
# handle specific errors
|
|
68
|
+
rescue Lotr::Unauthorized => e
|
|
69
|
+
puts e.message
|
|
70
|
+
# => "Unauthorized"
|
|
71
|
+
rescue Lotr::Error => e
|
|
72
|
+
# could be any other error e.g. Lotr::Unauthorized, Lotr::Forbidden, Lotr::BadRequest, Lotr::InternalServerError, Lotr::ServiceUnavailable
|
|
73
|
+
# or any other error that might be raised by the underlying HTTP client.
|
|
74
|
+
|
|
75
|
+
# log the error, notify user, retry the request, etc.
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Design
|
|
80
|
+
|
|
81
|
+
See [DESIGN.md](DESIGN.md) for more details on the design of the SDK.
|
|
82
|
+
|
|
83
|
+
## Development
|
|
84
|
+
|
|
85
|
+
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.
|
|
86
|
+
|
|
87
|
+
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).
|
|
88
|
+
|
|
89
|
+
## Contributing
|
|
90
|
+
|
|
91
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/FawazFarid/fawaz-SDK. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/https://github.com/FawazFarid/fawaz-SDK/blob/main/CODE_OF_CONDUCT.md).
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
96
|
+
|
|
97
|
+
## Code of Conduct
|
|
98
|
+
|
|
99
|
+
Everyone interacting in the Lotr project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/FawazFarid/fawaz-SDK/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/design.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Design decisions
|
|
2
|
+
|
|
3
|
+
## Choice of libraries
|
|
4
|
+
|
|
5
|
+
- **Testing framework**: [RSpec](https://rspec.info/) - mostly because I'm familiar with it and very readable DSL.
|
|
6
|
+
|
|
7
|
+
- **HTTP client**: [HTTPParty](https://github.com/jnunemaker/httparty) - Who doesn't like to party? :tada: :tada: :tada: Just kidding, I chose this because it's simple and easy to use. I considered [Faraday](https://lostisland.github.io/faraday/) but I just needed a library that can do simple http requests, nothing fancy and easier to use than `Net::HTTP`.
|
|
8
|
+
|
|
9
|
+
- I chose `vcr` gem for recording and replaying HTTP responses and `webmock` for stubbing HTTP requests and responses.
|
|
10
|
+
|
|
11
|
+
## Making requests
|
|
12
|
+
|
|
13
|
+
I thought about going with a class-based approach for getting info about resources from the API. for example:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require 'lotr'
|
|
17
|
+
|
|
18
|
+
# List all movies
|
|
19
|
+
Lotr::Movie.all
|
|
20
|
+
|
|
21
|
+
# Retrieve a movie
|
|
22
|
+
Lotr::Movie.find("5cd95395de30eff6ebccde5b")
|
|
23
|
+
|
|
24
|
+
# List all movie quotes for one specific movie
|
|
25
|
+
Lotr::Movie.find("5cd95395de30eff6ebccde5b").quotes
|
|
26
|
+
|
|
27
|
+
# Paginate movie quotes
|
|
28
|
+
Lotr::Movie.find("5cd95395de30eff6ebccde5b").quotes(page: 2, limit: 5)
|
|
29
|
+
|
|
30
|
+
# Sort movie quotes, sort by character asc
|
|
31
|
+
Lotr::Movie.find("5cd95395de30eff6ebccde5b").quotes(sort: 'character', order: 'asc')
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
I love the readability of this approach but I figured this might be time consuming in terms of writing the implementation, error handling, testing, etc. Also this closely mirrors how [ActiveRecord](https://guides.rubyonrails.org/active_record_basics.html#read) models work which might be confusing to some users who might think we are retrieving results from a local datastore.
|
|
36
|
+
|
|
37
|
+
So I went with a simpler approach, where API calls are available as instance methods of a single class.
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require 'lotr'
|
|
41
|
+
|
|
42
|
+
# Initialize the client and provide authentication credentials
|
|
43
|
+
client = Lotr::Client.new(api_key: 'your_api_key')
|
|
44
|
+
|
|
45
|
+
# List all movies
|
|
46
|
+
client.movies
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Also provided alias methods for the API endpoints:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
# List all movies
|
|
53
|
+
client.get_movies # same as client.movies
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Accessing resources
|
|
57
|
+
|
|
58
|
+
At the moment The One API endpoints return a JSON response where `docs` is an array of objects whether we are fetching a single resource (`/movies/:id`) or a collection (`/movies`):
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"docs": [
|
|
63
|
+
{
|
|
64
|
+
"_id": "5cd95395de30eff6ebccde5b",
|
|
65
|
+
"name": "The Two Towers",
|
|
66
|
+
"runtimeInMinutes": 179,
|
|
67
|
+
"budgetInMillions": 94,
|
|
68
|
+
"boxOfficeRevenueInMillions": 342,
|
|
69
|
+
"academyAwardNominations": 7,
|
|
70
|
+
"academyAwardWins": 2,
|
|
71
|
+
"rottenTomatoesScore": 89
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
"total": 1,
|
|
75
|
+
"limit": 10,
|
|
76
|
+
"page": 1,
|
|
77
|
+
"pages": 1
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This looks like anti-pattern to me. I think it would be better if the API returned a single object for single resources and an array of objects for collections. This would make it easier for the client to consume the API. I also don't undertstand why the API returns a `docs`, `page`, `pages`, `limit` and `total` fields, especially for single resources.
|
|
82
|
+
|
|
83
|
+
In my opinion SDKs should follow the language's conventions and idioms and also make it easier for the application developer to consume the API. So, I decided to return an `OpenStruct` object for single resources and an array of `OpenStruct` objects for collections and transformed the keys from `camelCase` to `snake_case` so that users can access the data using dot notation or `[]` for fields returned in the API response e.g:
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
movie = client.movie("5cd95395de30eff6ebccde5b")
|
|
87
|
+
puts movie['name']
|
|
88
|
+
# => "The Two Towers"
|
|
89
|
+
puts movie[:academy_award_wins]
|
|
90
|
+
# => 2
|
|
91
|
+
puts movie.runtime_in_minutes
|
|
92
|
+
# => 179
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
With that being said, I think we can also give the user an option to access the raw response from the API. This would be useful for debugging and for users who want to access the `docs`, `page`, `pages`, `limit` and `total` fields. This can be done by providing the following methods inside the `Lotr::Client` class:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
movie = client.movie("5cd95395de30eff6ebccde5b")
|
|
99
|
+
response = client.last_response # returns the last HTTP response where the movie was being fetched
|
|
100
|
+
headers = response.headers
|
|
101
|
+
body = response.body
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
I didn't get around to doing this. This can be done in a future release.
|
|
105
|
+
|
|
106
|
+
## Configuring the client
|
|
107
|
+
|
|
108
|
+
I wanted to enhance how we can configure this SDK by using configuration block. I noticed this pattern with the Airbrake gem and really liked it:
|
|
109
|
+
|
|
110
|
+
I liked the way Airbrake did it and wanted to implement the same pattern:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
Lotr.configure do |config|
|
|
114
|
+
config.api_key = 'frodo_was_here'
|
|
115
|
+
config.api_version = 'v2' # In case of future API versions or if you want to use v1
|
|
116
|
+
config.auto_paginate = true # enable auto pagination
|
|
117
|
+
config.per_page = 10 # Default number of results per page for paginated endpoints
|
|
118
|
+
end
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Clean internal implementation and easy for the application developer to understand. This could be used in `config/initializers/lotr.rb` in a Rails app and gives our users multiple options for configuring the SDK.
|
|
122
|
+
|
|
123
|
+
I decided to go with a simpler approach for now and just allow the user to pass in the API key as an argument to the `Lotr::Client` constructor.
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
client = Lotr::Client.new(api_key: 'easy_peasy')
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Some other stuff I'd like to add (If I had more time)
|
|
130
|
+
|
|
131
|
+
- Implement sorting for `/movie` and `/quote` endpoints but it doesn't seem to be supported by the API
|
|
132
|
+
- fetching API key from local .env file using [dotenv](https://github.com/bkeepers/dotenv) gem.
|
data/lib/lotr/client.rb
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
require "httparty"
|
|
2
|
+
require "lotr/response"
|
|
3
|
+
|
|
4
|
+
module Lotr
|
|
5
|
+
# Client for the Lord of the Rings API
|
|
6
|
+
class Client
|
|
7
|
+
include HTTParty
|
|
8
|
+
base_uri "https://the-one-api.dev/v2"
|
|
9
|
+
|
|
10
|
+
def initialize(options = {})
|
|
11
|
+
@headers = { "Authorization" => "Bearer #{options[:api_key]}" }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def version
|
|
15
|
+
Lotr::VERSION
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def movies(options = {})
|
|
19
|
+
resp = self.class.get("/movie", headers: @headers, query: parse_query_params(options))
|
|
20
|
+
handle_response(resp, multiple_items: true)
|
|
21
|
+
end
|
|
22
|
+
alias get_movies movies
|
|
23
|
+
|
|
24
|
+
def movie(movie_id)
|
|
25
|
+
resp = self.class.get("/movie/#{movie_id}", headers: @headers)
|
|
26
|
+
handle_response(resp)
|
|
27
|
+
end
|
|
28
|
+
alias get_movie movie
|
|
29
|
+
|
|
30
|
+
def quotes_for_movie(movie_id, options = {})
|
|
31
|
+
resp = self.class.get("/movie/#{movie_id}/quote", headers: @headers, query: parse_query_params(options))
|
|
32
|
+
handle_response(resp, multiple_items: true)
|
|
33
|
+
end
|
|
34
|
+
alias get_quotes_for_movie quotes_for_movie
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def handle_response(resp, multiple_items: false)
|
|
39
|
+
handle_error_response(resp) unless resp.success?
|
|
40
|
+
|
|
41
|
+
docs = resp["docs"]
|
|
42
|
+
return Lotr::Response.parse_to_object!(docs.first) unless multiple_items
|
|
43
|
+
|
|
44
|
+
docs.map { |item| Lotr::Response.parse_to_object!(item) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def handle_error_response(resp)
|
|
48
|
+
error = Lotr::Error.from_response(resp)
|
|
49
|
+
raise error
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def parse_query_params(options)
|
|
53
|
+
# merge the sort and order params into one e.g sort=name:asc
|
|
54
|
+
options[:sort] = "#{options[:sort]}:#{options[:order]}" if options[:order] && options[:sort]
|
|
55
|
+
options.delete(:order)
|
|
56
|
+
options
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/lib/lotr/error.rb
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lotr
|
|
4
|
+
class Error < StandardError
|
|
5
|
+
# Returns the appropriate Lotr::Error subclass based
|
|
6
|
+
# on status and response message
|
|
7
|
+
#
|
|
8
|
+
# @param [Hash] response HTTP response
|
|
9
|
+
# @return [Lotr::Error]
|
|
10
|
+
def self.from_response(response)
|
|
11
|
+
status = response.code
|
|
12
|
+
klass = case status
|
|
13
|
+
when 400 then Lotr::BadRequest
|
|
14
|
+
when 401 then Lotr::Unauthorized
|
|
15
|
+
when 403 then Lotr::Forbidden
|
|
16
|
+
when 404 then Lotr::NotFound
|
|
17
|
+
when 422 then Lotr::UnprocessableEntity
|
|
18
|
+
when 429 then Lotr::TooManyRequests
|
|
19
|
+
when 400..499 then Lotr::ClientError
|
|
20
|
+
when 500 then Lotr::InternalServerError
|
|
21
|
+
when 501 then Lotr::NotImplemented
|
|
22
|
+
when 502 then Lotr::BadGateway
|
|
23
|
+
when 503 then Lotr::ServiceUnavailable
|
|
24
|
+
when 500..599 then Lotr::ServerError
|
|
25
|
+
end
|
|
26
|
+
klass.new(response.message)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Raised on errors in the 400-499 range
|
|
31
|
+
class ClientError < Error; end
|
|
32
|
+
|
|
33
|
+
# Raised when The One API returns a 400 HTTP status code
|
|
34
|
+
class BadRequest < ClientError; end
|
|
35
|
+
|
|
36
|
+
# Raised when The One API returns a 401 HTTP status code
|
|
37
|
+
class Unauthorized < ClientError; end
|
|
38
|
+
|
|
39
|
+
# Raised when The One API returns a 403 HTTP status code
|
|
40
|
+
class Forbidden < ClientError; end
|
|
41
|
+
|
|
42
|
+
# Raised when The One API returns a 404 HTTP status code
|
|
43
|
+
class NotFound < ClientError; end
|
|
44
|
+
|
|
45
|
+
# Raised when The One API returns a 422 HTTP status code
|
|
46
|
+
class UnprocessableEntity < ClientError; end
|
|
47
|
+
|
|
48
|
+
# Raised when The One API returns a 429 HTTP status code
|
|
49
|
+
class TooManyRequests < ClientError; end
|
|
50
|
+
|
|
51
|
+
# Raised on errors in the 500-599 range
|
|
52
|
+
class ServerError < Error; end
|
|
53
|
+
|
|
54
|
+
# Raised when The One API returns a 500 HTTP status code
|
|
55
|
+
class InternalServerError < ServerError; end
|
|
56
|
+
|
|
57
|
+
# Raised when The One API returns a 501 HTTP status code
|
|
58
|
+
class NotImplemented < ServerError; end
|
|
59
|
+
|
|
60
|
+
# Raised when The One API returns a 502 HTTP status code
|
|
61
|
+
class BadGateway < ServerError; end
|
|
62
|
+
|
|
63
|
+
# Raised when The One API returns a 503 HTTP status code
|
|
64
|
+
class ServiceUnavailable < ServerError; end
|
|
65
|
+
|
|
66
|
+
# Raised when a API key is required but none is provided
|
|
67
|
+
class MissingAPIKey < StandardError; end
|
|
68
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Lotr
|
|
2
|
+
class Response
|
|
3
|
+
# Parse the hash, convert the keys to snake_case and define a method dynamically for each key
|
|
4
|
+
def self.parse_to_object!(hash)
|
|
5
|
+
hash.transform_keys! do |k|
|
|
6
|
+
# convert the key to string
|
|
7
|
+
k = k.to_s
|
|
8
|
+
# remove the leading underscore from the key e.g _id => id
|
|
9
|
+
k = k.gsub(/^_/, "")
|
|
10
|
+
# convert the key to snake case
|
|
11
|
+
k.underscore!
|
|
12
|
+
k
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# define a method for every key to access their respective value
|
|
16
|
+
OpenStruct.new(hash)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# underscore! method is not available outside of Rails, had to write a simple implementation
|
|
22
|
+
class String
|
|
23
|
+
# ruby mutation methods have the expectation to return self if a mutation occurred, nil otherwise. (see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub-21)
|
|
24
|
+
def underscore!
|
|
25
|
+
gsub!(/(.)([A-Z])/, '\1_\2')
|
|
26
|
+
downcase!
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/lotr/version.rb
ADDED
data/lib/lotr.rb
ADDED
data/sig/lotr.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lotr
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Fawaz Farid
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-02-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: httparty
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: vcr
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '6.1'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '6.1'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: webmock
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.18.1
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.18.1
|
|
55
|
+
description: This gem provides access to the Lord of the Rings API. (https://the-one-api.dev/)
|
|
56
|
+
email:
|
|
57
|
+
- fawwazally@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".rspec"
|
|
63
|
+
- ".rubocop.yml"
|
|
64
|
+
- CHANGELOG.md
|
|
65
|
+
- CODE_OF_CONDUCT.md
|
|
66
|
+
- Gemfile
|
|
67
|
+
- Gemfile.lock
|
|
68
|
+
- LICENSE.txt
|
|
69
|
+
- README.md
|
|
70
|
+
- Rakefile
|
|
71
|
+
- design.md
|
|
72
|
+
- lib/lotr.rb
|
|
73
|
+
- lib/lotr/client.rb
|
|
74
|
+
- lib/lotr/error.rb
|
|
75
|
+
- lib/lotr/response.rb
|
|
76
|
+
- lib/lotr/version.rb
|
|
77
|
+
- sig/lotr.rbs
|
|
78
|
+
homepage: https://github.com/FawazFarid/fawaz-SDK
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata:
|
|
82
|
+
homepage_uri: https://github.com/FawazFarid/fawaz-SDK
|
|
83
|
+
source_code_uri: https://github.com/FawazFarid/fawaz-SDK
|
|
84
|
+
changelog_uri: https://github.com/FawazFarid/fawaz-SDK/blob/main/CHANGELOG.md
|
|
85
|
+
post_install_message:
|
|
86
|
+
rdoc_options: []
|
|
87
|
+
require_paths:
|
|
88
|
+
- lib
|
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: 2.6.0
|
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
|
+
requirements:
|
|
96
|
+
- - ">="
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: '0'
|
|
99
|
+
requirements: []
|
|
100
|
+
rubygems_version: 3.3.26
|
|
101
|
+
signing_key:
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: This gem provides access to the Lord of the Rings API. (https://the-one-api.dev/)
|
|
104
|
+
test_files: []
|