class Lokalise::Collections::Base

Attributes

branch[R]
client[R]
collection[R]
current_page[R]
path[R]
project_id[R]
request_params[R]
results_per_page[R]
team_id[R]
total_pages[R]
total_results[R]
user_id[R]

Public Class Methods

all(client, path, params = {}) click to toggle source

Performs a batch query fetching multiple records

# File lib/ruby-lokalise-api/collections/base.rb, line 34
def all(client, path, params = {})
  new get(path, client, params),
      params
end
new(response, params = {}) click to toggle source

Initializes a new collection based on the response

@param response [Hash] @param params [Hash] @return [Lokalise::Collections::Base]

# File lib/ruby-lokalise-api/collections/base.rb, line 19
def initialize(response, params = {})
  produce_collection_for response
  populate_pagination_data_for response
  # Project, team id, user id, and branch may not be present in some cases
  @project_id = response['content']['project_id']
  @team_id = response['content']['team_id']
  @user_id = response['content']['user_id']
  @branch = response['content']['branch']
  @request_params = params
  @client = response['client']
  @path = response['path']
end

Public Instance Methods

first_page?() click to toggle source

@return [Boolean]

# File lib/ruby-lokalise-api/collections/base.rb, line 56
def first_page?
  !prev_page?
end
last_page?() click to toggle source

@return [Boolean]

# File lib/ruby-lokalise-api/collections/base.rb, line 46
def last_page?
  !next_page?
end
next_page() click to toggle source

@return [Integer]

# File lib/ruby-lokalise-api/collections/base.rb, line 61
def next_page
  return nil if last_page?

  fetch_page @current_page + 1
end
next_page?() click to toggle source

@return [Boolean]

# File lib/ruby-lokalise-api/collections/base.rb, line 41
def next_page?
  @current_page.positive? && @current_page < @total_pages
end
prev_page() click to toggle source

@return [Integer]

# File lib/ruby-lokalise-api/collections/base.rb, line 68
def prev_page
  return nil if first_page?

  fetch_page @current_page - 1
end
prev_page?() click to toggle source

@return [Boolean]

# File lib/ruby-lokalise-api/collections/base.rb, line 51
def prev_page?
  @current_page > 1
end

Private Instance Methods

fetch_page(page_num) click to toggle source

Gets the specified page

# File lib/ruby-lokalise-api/collections/base.rb, line 84
def fetch_page(page_num)
  self.class.all @client,
                 @path,
                 @request_params.merge(page: page_num)
end
populate_pagination_data_for(response) click to toggle source
# File lib/ruby-lokalise-api/collections/base.rb, line 76
def populate_pagination_data_for(response)
  @total_results = response['x-pagination-total-count'].to_i
  @total_pages = response['x-pagination-page-count'].to_i
  @results_per_page = response['x-pagination-limit'].to_i
  @current_page = response['x-pagination-page'].to_i
end
produce_collection_for(response) click to toggle source

Dynamically produces collection of resources based on the given response Collection example: `{ “content”: {“comments”: [ … ]} }`

# File lib/ruby-lokalise-api/collections/base.rb, line 92
def produce_collection_for(response)
  model_class = self.class.name.base_class_name
  data_key_plural = data_key_for model_class: model_class, plural: true, collection: true

  # Fetch collection data and instantiate an individual resource for each object
  # We also preserve the `client` to be able to chain API methods later
  @collection = response['content'][data_key_plural].map do |raw_model|
    Module.const_get("Lokalise::Resources::#{model_class}").new 'content' => raw_model,
                                                                'client' => response['client'],
                                                                'base_path' => response['path']
  end
end