module YandexLocator::API

Public Instance Methods

call() click to toggle source
# File lib/yandex_locator/api.rb, line 9
def call
  connection(url: YandexLocator.configuration.url_prefix)
end
connection(url:) click to toggle source
# File lib/yandex_locator/api.rb, line 13
def connection(url:)
  Faraday.new(url) do |builder|
    builder.request  :url_encoded
    builder.response :logger
    builder.adapter  Faraday.default_adapter
  end
end
post(url, options = {}) click to toggle source
# File lib/yandex_locator/api.rb, line 5
def post(url, options = {})
  request :post, url, options
end

Private Instance Methods

pars_response(body) click to toggle source
# File lib/yandex_locator/api.rb, line 43
def pars_response(body)
  data = JSON.parse(body)
  OpenStruct.new(data)
end
request(method, path, data) click to toggle source
# File lib/yandex_locator/api.rb, line 23
def request(method, path, data)
  response = call.send(method) do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.params['json'] = request_data(data).to_json
  end

  pars_response(response.body)
end
request_data(data) click to toggle source
# File lib/yandex_locator/api.rb, line 33
def request_data(data)
  {
    common:
    {
      version: YandexLocator.configuration.version,
      api_key: YandexLocator.configuration.api_key
    }
  }.merge(data)
end