Sha256: d10ddda54f486bfb81f64fe86ded3d05588a2bd3bb503b719ec523cbe1392c4c
Contents?: true
Size: 1.81 KB
Versions: 1
Compression:
Stored size: 1.81 KB
Contents
module BrainstemAdaptor class Response attr_reader :response_data, :specification # @param response_data [String, Hash] # @param specification [BrainstemAdaptor::Specification] 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 # @param key [String, Symbol] # @return [Hash, nil] def [](key) response_data[key.to_s] end # Returns __TOTAL__ number of records # @return [Integer] def count response_data['count'] end alias_method :total_count, :count # @return [String] def to_hash response_data end # Returns results has with proper ordering # @return [Array<BrainstemAdaptor::Record>] def results self['results'].map do |result| BrainstemAdaptor::Record.new(result['key'], result['id'], self) end end def ==(other) other == response_data end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
brainstem-adaptor-0.0.4 | lib/brainstem_adaptor/response.rb |