Class: KinopoiskDevApi::Client
- Inherits:
-
Object
- Object
- KinopoiskDevApi::Client
- Defined in:
- lib/kinopoisk_dev_api/client.rb
Constant Summary collapse
- API_VERSION =
"v1.4"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #call(endpoint, raw_params = {}) ⇒ Object
- #connection ⇒ Object
-
#initialize(api_key, url: "https://api.kinopoisk.dev") ⇒ Client
constructor
A new instance of Client.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(*args) ⇒ Boolean
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_key ⇒ Object (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 |
#url ⇒ Object (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 |
#connection ⇒ Object
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..timeout = KinopoiskDevApi.configuration.connection_timeout faraday..open_timeout = KinopoiskDevApi.configuration.connection_open_timeout faraday..params_encoder = Faraday::FlatParamsEncoder end end |
#respond_to_missing?(*args) ⇒ 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 |