class Square::ListResponse

Constants

RegExp used for parsing Link headers when the API paginates data. Don't care about rel attributes right now because this is the only thing this is used for.

Attributes

response[RW]

Public Class Methods

new(response, data_type) click to toggle source

Initialize.

@param response [RestClient::Response] Raw resonse object. @param data_type [Square::DataType] Data type to new up records.

@return [ListResponse]

# File lib/square/list_response.rb, line 18
def initialize(response, data_type)
  @response = response
  @data_type = data_type
  @values = {}
  parse_response(@response)
end

Public Instance Methods

each(&block) click to toggle source

Each.

@param block [Block]

# File lib/square/list_response.rb, line 28
def each(&block)
  @values.each(&block)
end
has_more?() click to toggle source

Check if there are more pages.

@return [Boolean]

# File lib/square/list_response.rb, line 47
def has_more?
  !@next_link.nil?
end
method_missing(name, *args, &block) click to toggle source

Pass through methods to the original response object.

# File lib/square/list_response.rb, line 52
def method_missing(name, *args, &block)
  @response.send(name, *args, &block)
end
more() click to toggle source

Get more records.

@return [Array] parsed records.

# File lib/square/list_response.rb, line 35
def more
  if !has_more?
    return nil
  end

  @response = Square.make_request(url: @next_link)
  self.class.new(@response, @data_type)
end

Private Instance Methods

parse_response(response) click to toggle source

Parse a response.

@return [Array] parsed records.

# File lib/square/list_response.rb, line 61
def parse_response(response)
  # Detect a Link header.
  if !response.headers[:link].nil?
    match = LINK_REGEXP.match(response.headers[:link])

    if match.nil?
      @next_link = nil
    else
      @next_link = match.captures[0]
    end
  else
    @next_link = nil
  end

  parsed_response = JSON.parse(response)
  @values = parsed_response.map {|record| @data_type.new(record)}
end