Sha256: 188f8d6dce63d104c4f79ebecd1bb5315c88cd87b91e998101ac505ab294a7d2

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module Octobat
  class ListObject < OctobatObject
    include Enumerable
    include Octobat::APIOperations::List
    
    attr_accessor :filters, :cursors
    
    def initialize(*args)
      super
      self.filters = {}
      self.cursors = {}
    end

    def [](k)
      case k
      when String, Symbol
        super
      else
        raise ArgumentError.new("You tried to access the #{k.inspect} index, but ListObject types only support Octobat keys. (HINT: List calls return an object with a 'data' (which is the data array). You likely want to call #data[#{k.inspect}])")
      end
    end

    def each(&blk)
      self.data.each(&blk)
    end
    
    def empty?
      self.data.empty?
    end

    def retrieve(id, api_key=nil)
      api_key ||= @api_key
      response, api_key = Octobat.request(:get, "#{url}/#{CGI.escape(id)}", api_key)
      Util.convert_to_octobat_object(response, api_key)
    end

    def create(params={}, opts={})
      api_key, headers = Util.parse_opts(opts)
      api_key ||= @api_key
      response, api_key = Octobat.request(:post, url, api_key, params, headers)
      Util.convert_to_octobat_object(response, api_key)
    end

    
    def next_page_params(params={}, opts={})
      return nil if !has_more
      last_id = data.last.id

      params = filters.merge({
        starting_after: last_id
      }).merge(params)
    end


    def previous_page_params(params={}, opts={})
      return nil if cursors[:starting_after].nil? || cursors[:starting_after].empty?
      first_id = data.first.id

      params = filters.merge({
        ending_before: first_id
      }).merge(params)
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
octobat-0.0.11 lib/octobat/list_object.rb