Sha256: ade22bb5529d4054c0fed6d71526fdc9dc85758418c655e8a535804eec86a902

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

module Octobat
  class ListObject < OctobatObject
    include Enumerable
    include Octobat::APIOperations::List
    
    attr_accessor :filters, :cursors, :parent_resource
    
    def initialize(*args)
      super
      self.filters = {}
      self.cursors = {}
      self.parent_resource = {}
    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, opts={})
      api_key, headers = Util.parse_opts(opts)
      api_key ||= @api_key
      
      response, api_key = Octobat.request(:get, "#{url}/#{CGI.escape(id)}", api_key)
      Util.convert_to_octobat_object(response, api_key, self.parent_resource)
    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 !has_before
      first_id = data.first.id

      params = filters.merge({
        ending_before: first_id
      }).merge(params)
    end
    
    
    def url
      self.request_url
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
octobat-2.0.5 lib/octobat/list_object.rb
octobat-2.0.4 lib/octobat/list_object.rb
octobat-2.0.3 lib/octobat/list_object.rb
octobat-2.0.2 lib/octobat/list_object.rb
octobat-2.0.1 lib/octobat/list_object.rb
octobat-2.0.0 lib/octobat/list_object.rb