Sha256: c5fcbe328afc07482fbe6ccc69aefbb25f17b6874ac8ad2be070917978451782

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module RubyLokaliseApi
  # This class is utilized to prepare Lokalise API response
  class Response
    # Lokalise returns pagination info in special headers
    PAGINATION_HEADERS = %w[x-pagination-total-count x-pagination-page-count x-pagination-limit
                            x-pagination-page x-pagination-next-cursor].freeze

    attr_reader :content, :endpoint, :headers

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

    # 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
    def patch_content_with(attribute, value)
      @content[attribute] = value
    end

    # 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
    def patch_endpoint_with(new_endpoint)
      @endpoint = new_endpoint
    end

    private

    # Keep only pagination headers
    def extract_headers_from(raw_headers)
      raw_headers.to_h.slice(*PAGINATION_HEADERS).transform_keys(&:to_sym)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-lokalise-api-9.4.0 lib/ruby_lokalise_api/response.rb
ruby-lokalise-api-9.3.0 lib/ruby_lokalise_api/response.rb
ruby-lokalise-api-9.2.1 lib/ruby_lokalise_api/response.rb
ruby-lokalise-api-9.2.0 lib/ruby_lokalise_api/response.rb