module Lokalise::Request

Constants

PAGINATION_HEADERS

Lokalise returns pagination info in special headers

Public Instance Methods

delete(path, client, params = {}) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 39
def delete(path, client, params = {})
  respond_with(
    # Rubocop tries to replace `delete` with `gsub` but that's a different method here!
    # rubocop:disable Style/CollectionMethods
    connection(client).delete(prepare(path)) do |req|
      # rubocop:enable Style/CollectionMethods
      req.body = custom_dump params
    end,
    client
  )
end
get(path, client, params = {}) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 11
def get(path, client, params = {})
  respond_with(
    connection(client).get(prepare(path), params),
    client
  )
end
patch(path, client, params = {}) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 32
def patch(path, client, params = {})
  respond_with(
    connection(client).patch(prepare(path), params.any? ? custom_dump(params) : nil),
    client
  )
end
post(path, client, params = {}) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 18
def post(path, client, params = {})
  respond_with(
    connection(client).post(prepare(path), custom_dump(params)),
    client
  )
end
put(path, client, params = {}) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 25
def put(path, client, params = {})
  respond_with(
    connection(client).put(prepare(path), custom_dump(params)),
    client
  )
end

Private Instance Methods

extract_headers_from(response) click to toggle source

Get only pagination headers

# File lib/ruby-lokalise-api/request.rb, line 69
def extract_headers_from(response)
  response.
    headers.
    to_h.
    keep_if { |k, _v| PAGINATION_HEADERS.include?(k) }
end
prepare(path) click to toggle source

Get rid of double slashes in the `path`, leading and trailing slash

# File lib/ruby-lokalise-api/request.rb, line 54
def prepare(path)
  path.delete_prefix('/').gsub(%r{//}, '/').gsub(%r{/+\z}, '')
end
respond_with(response, client) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 58
def respond_with(response, client)
  body = custom_load response.body
  uri = Addressable::URI.parse response.env.url
  respond_with_error(response.status, body) if body.respond_to?(:has_key?) && body.key?('error')
  extract_headers_from(response).
    merge('content' => body,
          'client' => client,
          'path' => uri.path.gsub(%r{/api2/}, ''))
end
respond_with_error(code, body) click to toggle source
# File lib/ruby-lokalise-api/request.rb, line 76
def respond_with_error(code, body)
  raise(Lokalise::Error, body['error']) unless Lokalise::Error::ERRORS.key? code

  raise Lokalise::Error::ERRORS[code].from_response(body)
end