Sha256: 1a851db8ee44410357be8e2fc6026be6a1fefd1814394ae60d907f8ae06a0209

Contents?: true

Size: 858 Bytes

Versions: 2

Compression:

Stored size: 858 Bytes

Contents

module Eucalyptus
  class Response < OpenStruct
    # extend OpenStruct to work with nested hashes
    def initialize(hash)
      @table = {}
      @hash_table = {}

      hash.each do |k, v|
        @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
        @hash_table[k.to_sym] = v

        new_ostruct_member(k)
      end
    end
  end

  class ResponseCollection
    def initialize(klass, koala_response)
      @koala_response = koala_response
      @klass = klass
      @array = elements(@koala_response)
    end


    def next_page
      next_page = @koala_response.next_page
      self.class.new(@klass, next_page)
    end

    # for array methods
    def method_missing(method_sym, *args, &block)
      @array.send(method_sym)
    end

    private
    def elements(response)
      response.collect{ |res| @klass.new(res) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eucalyptus-0.3.1 lib/eucalyptus/response.rb
eucalyptus-0.3.0 lib/eucalyptus/response.rb