Class: Apress::YandexMarket::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apress/yandex_market/api/client.rb

Overview

API клиент Яндекс.Маркета

Constant Summary collapse

TIMEOUT =
60
HOST =
'api.content.market.yandex.ru'.freeze
ACCEPT =
'*/*'.freeze
VERSION =
'v2'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(auth_token) ⇒ Client

Public: инициализация клиента

auth_token - String, токен для доступа к API

Returns an instance of Api::Client



21
22
23
# File 'lib/apress/yandex_market/api/client.rb', line 21

def initialize(auth_token)
  @auth_token = auth_token
end

Instance Method Details

#get(path, params = {}) ⇒ Object

Public: выполняет GET-запрос и парсит ответ в формате JSON

path - String, ресурс API params - Hash, дополнительные get-параметры запроса (default: {})

Returns Hash



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/apress/yandex_market/api/client.rb', line 31

def get(path, params = {})
  uri = api_uri(path)
  uri.query = URI.encode_www_form(params) unless params.empty?

  req = Net::HTTP::Get.new(uri)
  req['Host'] = HOST
  req['Accept'] = ACCEPT
  req['Authorization'] = @auth_token

  res = Net::HTTP.start(uri.hostname, uri.port, http_options) do |http|
    http.request(req)
  end

  if res['Content-Type'] && res['Content-Type'].start_with?('application/json')
    parse_response(res)
  else
    raise Api::Error.new(res.msg, res.code)
  end
end