Class: Octobat::ListObject

Inherits:
OctobatObject show all
Includes:
Enumerable, APIOperations::List
Defined in:
lib/octobat/list_object.rb

Instance Attribute Summary collapse

Attributes inherited from OctobatObject

#api_key, #parent_obj

Instance Method Summary collapse

Methods included from APIOperations::List

#list, #set_parent_resource

Methods inherited from OctobatObject

#[]=, #_dump, _load, #as_json, construct_from, #inspect, #keys, #refresh_from, #respond_to?, #to_hash, #to_json, #to_s, #values

Constructor Details

#initialize(*args) ⇒ ListObject

Returns a new instance of ListObject.



8
9
10
11
12
13
# File 'lib/octobat/list_object.rb', line 8

def initialize(*args)
  super
  self.filters = {}
  self.cursors = {}
  self.parent_resource = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Octobat::OctobatObject

Instance Attribute Details

#cursorsObject

Returns the value of attribute cursors.



6
7
8
# File 'lib/octobat/list_object.rb', line 6

def cursors
  @cursors
end

#filtersObject

Returns the value of attribute filters.



6
7
8
# File 'lib/octobat/list_object.rb', line 6

def filters
  @filters
end

#parent_resourceObject

Returns the value of attribute parent_resource.



6
7
8
# File 'lib/octobat/list_object.rb', line 6

def parent_resource
  @parent_resource
end

Instance Method Details

#[](k) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/octobat/list_object.rb', line 15

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

#create(params = {}, opts = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/octobat/list_object.rb', line 53

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

#each(&blk) ⇒ Object



24
25
26
# File 'lib/octobat/list_object.rb', line 24

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

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/octobat/list_object.rb', line 28

def empty?
  self.data.empty?
end

#next_page_params(params = {}, opts = {}) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/octobat/list_object.rb', line 61

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

#previous_page_params(params = {}, opts = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/octobat/list_object.rb', line 71

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

#retrieve(id, opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/octobat/list_object.rb', line 32

def retrieve(id, opts={})
  api_key, headers = Util.parse_opts(opts)
  api_key ||= @api_key
  
  if id.kind_of?(Hash)
    retrieve_options = id.dup
    retrieve_options.delete(:id)
    id = id[:id]
  else
    retrieve_options = {}
  end
  
  headers = {}
  
  retrieve_options.merge!(opts.clone).delete(:api_key)
  headers['Octobat-Version'] = retrieve_options.delete('Octobat-Version') if retrieve_options.has_key?('Octobat-Version')
        
  response, api_key = Octobat.request(:get, "#{url}/#{CGI.escape(id)}", api_key, retrieve_options, headers)
  Util.convert_to_octobat_object(response, api_key, self.parent_resource)
end

#urlObject



81
82
83
# File 'lib/octobat/list_object.rb', line 81

def url
  self.request_url
end