Class: Lotr::Sdk::Client

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • id (String)

    A 24-char string identifying the movie

Returns:

  • (Movie)

    The movie corresponding to the ID

Raises:



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

Returns:

  • (Array<Quote>)

    All quotes belonging to the movie

Raises:



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

Returns:

  • (Array<Quote>)

    All quotes belonging to the movie



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

Parameters:

  • q (Object) (defaults to: {})

    Hash of query parameters. For example: { name: “Series” }

Returns:

  • (Array<Movie>)

    A list of 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

Parameters:

  • id (String)

    A 24-char string identifying the quote

Returns:

  • (Movie)

    The quote corresponding to the ID

Raises:



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

Parameters:

  • q (Object) (defaults to: {})

    Hash of query and pagination parameters. For example: { dialog: “Oooh”, page: 2 }

Returns:

  • (Array<Movie>)

    A list of all movies



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