class BrainstemAdaptor::Response

Attributes

response_data[R]
specification[R]

Public Class Methods

new(response_data, specification = BrainstemAdaptor.default_specification, **options) click to toggle source

@param response_data [String, Hash] @param specification [BrainstemAdaptor::Specification]

# File lib/brainstem_adaptor/response.rb, line 7
def initialize(response_data, specification = BrainstemAdaptor.default_specification, **options)
  @specification = specification or raise ArgumentError, 'Specification is not set'

  case response_data
  when String
    @response_data = BrainstemAdaptor.parser.parse(response_data)
  when Hash
    @response_data = response_data
  when Array
    @response_data = BrainstemAdaptor::Parsers::ArrayParser.parse(response_data, options[:collection_name])
  else
    raise ArgumentError, "Expected String, got #{@response_data.class.name}"
  end

  raise InvalidResponseError, "count isn't returned" unless @response_data['count']
  raise InvalidResponseError, "results collection isn't returned" unless @response_data['results']
  raise InvalidResponseError, "results collection is not an array" unless @response_data['results'].is_a?(Array)

rescue JSON::ParserError => e
  raise BrainstemAdaptor::InvalidResponseError, response_data, e.message
end

Public Instance Methods

==(other) click to toggle source
# File lib/brainstem_adaptor/response.rb, line 55
def ==(other)
  other == response_data
end
[](key) click to toggle source

@param key [String, Symbol] @return [Hash, nil]

# File lib/brainstem_adaptor/response.rb, line 31
def [](key)
  response_data[key.to_s]
end
count() click to toggle source

Returns __TOTAL__ number of records @return [Integer]

# File lib/brainstem_adaptor/response.rb, line 37
def count
  response_data['count']
end
Also aliased as: total_count
results() click to toggle source

Returns results has with proper ordering @return [Array<BrainstemAdaptor::Record>]

# File lib/brainstem_adaptor/response.rb, line 49
def results
  self['results'].map do |result|
    BrainstemAdaptor::Record.new(result['key'], result['id'], self)
  end
end
to_hash() click to toggle source

@return [String]

# File lib/brainstem_adaptor/response.rb, line 43
def to_hash
  response_data
end
total_count()
Alias for: count