Class: Lotr::Sdk::Client
- Inherits:
-
Object
- Object
- Lotr::Sdk::Client
- Defined in:
- lib/lotr/sdk/client.rb
Overview
Client instantiates an HTTP client and makes all API calls
Constant Summary collapse
- ID_REGEX =
/^[0-9a-f]{24}$/i.freeze
- MOVIES_WITH_QUOTES =
IDs for the original trilogy
%w[5cd95395de30eff6ebccde5c 5cd95395de30eff6ebccde5b 5cd95395de30eff6ebccde5d"].freeze
Instance Method Summary collapse
-
#initialize(access_token: nil) ⇒ Client
constructor
Initialize client with an access token 1.
-
#movie(id) ⇒ Movie
Fetch a specified movie.
-
#movie_quotes(movie_id, movie = nil) ⇒ Array<Quote>
Fetch all quotes belonging to a movie.
-
#movie_quotes_from_movie(movie) ⇒ Array<Quote>
Fetch all quotes belonging to a movie.
-
#movies(q: {}) ⇒ Array<Movie>
Fetch all movies.
-
#quote(id) ⇒ Movie
Fetch a specified quote.
-
#quotes(q: {}) ⇒ Array<Movie>
Fetch all quotes.
Constructor Details
#initialize(access_token: nil) ⇒ Client
Initialize client with an access token
1. Sign up at https://the-one-api.dev/sign-up
2. Provide the access token.
You can pass in you token as a parameter
or
Set the token to the THE_ONE_ACCESS_TOKEN env variable
16 17 18 19 20 21 22 23 24 |
# File 'lib/lotr/sdk/client.rb', line 16 def initialize(access_token: nil) @api_url = "https://the-one-api.dev/v2" if access_token.nil? access_token = ENV["THE_ONE_ACCESS_TOKEN"] end raise Exception::MissingAccessTokenError if access_token.nil? @client = HTTP.headers(accept: "application/json").auth("Bearer #{access_token}") end |
Instance Method Details
#movie(id) ⇒ Movie
Fetch a specified movie
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lotr/sdk/client.rb', line 44 def movie(id) id = validate_id(id) endpoint = "movie/#{id}" uri = URI("#{@api_url}/#{endpoint}") response = JSON.parse(@client.get(uri).body) raise Exception::ResourceNotFoundError.new(:movie, id) if response["total"].zero? movie_data = response["docs"].first Movie.new(movie_data) end |
#movie_quotes(movie_id, movie = nil) ⇒ Array<Quote>
Fetch all quotes belonging to a movie. Only Lord of the Rings movie are supported. These movies are:
The Fellowship of the Ring
The Two Towers
The Return of the King
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lotr/sdk/client.rb', line 76 def movie_quotes(movie_id, movie = nil) raise Exception::MovieNotSupportedError.new(MOVIES_WITH_QUOTES) unless ENV["DISABLE_MOVIE_CHECK"] || MOVIES_WITH_QUOTES.include?(movie_id) endpoint = "/movie/#{movie_id}/quote" uri = URI("#{@api_url}/#{endpoint}") response = JSON.parse(@client.get(uri).body) response["docs"].map do |quote_data| Quote.new(quote_data) end end |
#movie_quotes_from_movie(movie) ⇒ Array<Quote>
Fetch all quotes belonging to a movie. Uses movie_quotes with same limitations
90 91 92 |
# File 'lib/lotr/sdk/client.rb', line 90 def movie_quotes_from_movie(movie) movie_quotes(movie.id, movie) end |
#movies(q: {}) ⇒ Array<Movie>
Fetch all movies
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lotr/sdk/client.rb', line 29 def movies(q: {}) endpoint = "movie" if q.any? endpoint += "?" + transform_query_parameters(q) end uri = URI("#{@api_url}/#{endpoint}") response = JSON.parse(@client.get(uri).body) response["docs"].map do |movie_data| Movie.new(movie_data) end end |
#quote(id) ⇒ Movie
Fetch a specified quote
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lotr/sdk/client.rb', line 97 def quote(id) quote_id = validate_id(id) endpoint = "/quote/#{quote_id}" uri = URI("#{@api_url}/#{endpoint}") response = JSON.parse(@client.get(uri).body) raise Exception::ResourceNotFoundError.new(:quote, id) if response["total"].zero? quote_data = response["docs"].first Quote.new(quote_data) end |
#quotes(q: {}) ⇒ Array<Movie>
Fetch all quotes
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lotr/sdk/client.rb', line 58 def quotes(q: {}) endpoint = "/quote" if q.any? endpoint += "?" + transform_query_parameters(q) end uri = URI("#{@api_url}/#{endpoint}") response = JSON.parse(@client.get(uri).body) response["docs"].map do |quote_data| Quote.new(quote_data) end end |