Class: KinopoiskUnofficialApi::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key, url: "https://kinopoiskapiunofficial.tech") ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/kinopoisk_unofficial_api/client.rb', line 7

def initialize(secret_key, url: "https://kinopoiskapiunofficial.tech")
  @secret_key = secret_key
  @url = url
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



20
21
22
23
24
25
26
# File 'lib/kinopoisk_unofficial_api/client.rb', line 20

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

  result = call(method_name, *args)

  ENDPOINTS[method_name][:response_type].call(result)
end

Instance Attribute Details

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



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

def secret_key
  @secret_key
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

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



34
35
36
37
38
39
40
41
# File 'lib/kinopoisk_unofficial_api/client.rb', line 34

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

  JSON.parse(response.body)
end

#connectionObject



12
13
14
15
16
17
18
# File 'lib/kinopoisk_unofficial_api/client.rb', line 12

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

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/kinopoisk_unofficial_api/client.rb', line 28

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

  ENDPOINTS.key?(method_name) || super
end