Class: RubyLokaliseApi::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lokalise_api/response.rb

Overview

This class is utilized to prepare Lokalise API response

Constant Summary collapse

PAGINATION_HEADERS =

Lokalise returns pagination info in special headers

%w[x-pagination-total-count x-pagination-page-count x-pagination-limit
x-pagination-page].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, endpoint, raw_headers = {}) ⇒ Response

Returns a new instance of Response.



12
13
14
15
16
# File 'lib/ruby_lokalise_api/response.rb', line 12

def initialize(body, endpoint, raw_headers = {})
  @content = body
  @endpoint = endpoint
  @headers = extract_headers_from raw_headers
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/ruby_lokalise_api/response.rb', line 10

def content
  @content
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



10
11
12
# File 'lib/ruby_lokalise_api/response.rb', line 10

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/ruby_lokalise_api/response.rb', line 10

def headers
  @headers
end

Instance Method Details

#pagination_headersObject

Returns an array of pagination headers



35
36
37
# File 'lib/ruby_lokalise_api/response.rb', line 35

def pagination_headers
  self.class.const_get(:PAGINATION_HEADERS)
end

#patch_content_with(attribute, value) ⇒ Object

Adds attribute to the raw content returned by the API. Sometimes this is required to store important data like team_id or project_id that is not returned in the response but required to properly build resources URIs



21
22
23
# File 'lib/ruby_lokalise_api/response.rb', line 21

def patch_content_with(attribute, value)
  @content[attribute] = value
end

#patch_endpoint_with(new_endpoint) ⇒ Object

Sets endpoint to a new value. This is required in rare cases when the response contains a resource of different kind. For example, when restoring a snapshot, it returns a new project which means that initially we were working with a snapshots endpoint but the response should contain the projects endpoint to properly preserve method chaining



30
31
32
# File 'lib/ruby_lokalise_api/response.rb', line 30

def patch_endpoint_with(new_endpoint)
  @endpoint = new_endpoint
end