Class: KinopoiskDevApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kinopoisk_dev_api/client.rb

Constant Summary collapse

API_VERSION =
"v1.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, url: "https://api.kinopoisk.dev") ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/kinopoisk_dev_api/client.rb', line 9

def initialize(api_key, url: "https://api.kinopoisk.dev")
  @api_key = api_key
  @url = url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/kinopoisk_dev_api/client.rb', line 23

def method_missing(method_name, *args, &block)
  return super unless ENDPOINTS.key?(method_name)

  endpoint = ENDPOINTS[method_name]

  result = call(endpoint, *args)

  endpoint_type(endpoint).call(result)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/kinopoisk_dev_api/client.rb', line 5

def api_key
  @api_key
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/kinopoisk_dev_api/client.rb', line 5

def url
  @url
end

Instance Method Details

#call(endpoint, raw_params = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/kinopoisk_dev_api/client.rb', line 39

def call(endpoint, raw_params = {})
  params = build_params(raw_params)
  path = build_path(endpoint, params)
  response = connection.get(path, params)
  raise Exceptions::ResponseError.new(response: response) unless response.status == 200

  JSON.parse(response.body)
end

#connectionObject



14
15
16
17
18
19
20
21
# File 'lib/kinopoisk_dev_api/client.rb', line 14

def connection
  @connection ||= Faraday.new(url: url, headers: { "X-API-KEY" => api_key }) do |faraday|
    faraday.adapter KinopoiskDevApi.configuration.adapter
    faraday.options.timeout = KinopoiskDevApi.configuration.connection_timeout
    faraday.options.open_timeout = KinopoiskDevApi.configuration.connection_open_timeout
    faraday.options.params_encoder = Faraday::FlatParamsEncoder
  end
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/kinopoisk_dev_api/client.rb', line 33

def respond_to_missing?(*args)
  method_name = args[0]

  ENDPOINTS.key?(method_name) || super
end