Class: MyJohnDeere::ListObject

Inherits:
Requestable show all
Includes:
Enumerable
Defined in:
lib/myjohndeere/list_object.rb

Constant Summary collapse

OPTION_ATTRIBUTES =
[:count, :start, :etag]

Instance Attribute Summary collapse

Attributes inherited from Requestable

#access_token, #links

Instance Method Summary collapse

Methods inherited from Requestable

#extract_link_with_rel_from_list, get_created_id_from_response_headers

Constructor Details

#initialize(listable, access_token, json_data, options: {}) ⇒ ListObject

Returns a new instance of ListObject.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/myjohndeere/list_object.rb', line 16

def initialize(listable, access_token, json_data,
  options: {})
  @options = options
  # Confirm object is listable? 
  @listable = listable
  @data = json_data["values"].collect { |i| listable.new(i, access_token) }
  if self.using_etag?
    MyJohnDeere.logger.info("Using etag, ignoring any specification about start/count")
    self.start = 0
    self.count = @data.length
  else
    MyJohnDeere.logger.info("Etag omitted using start/count")
    self.start ||= 0
    self.count ||= 10
  end
  # Total is the total record count as specified by the john deere response
  @total = json_data["total"] || data.length
  super(json_data, access_token)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/myjohndeere/list_object.rb', line 4

def data
  @data
end

#listableObject (readonly)

Returns the value of attribute listable.



4
5
6
# File 'lib/myjohndeere/list_object.rb', line 4

def listable
  @listable
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/myjohndeere/list_object.rb', line 4

def options
  @options
end

#totalObject (readonly)

Returns the value of attribute total.



4
5
6
# File 'lib/myjohndeere/list_object.rb', line 4

def total
  @total
end

Instance Method Details

#each(&blk) ⇒ Object



36
37
38
# File 'lib/myjohndeere/list_object.rb', line 36

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

#has_more?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/myjohndeere/list_object.rb', line 51

def has_more?()
  return !self.using_etag? && self.start + self.data.length < self.total
end

#next_page!Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/myjohndeere/list_object.rb', line 40

def next_page!()
  return if !self.has_more?()
  new_list = @listable.list(self.access_token, 
    count: self.count, 
    start: self.start + self.count,
    etag: self.etag)
  new_list.instance_variables.each do |iv|
    self.instance_variable_set(iv, new_list.instance_variable_get(iv))
  end
end

#using_etag?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/myjohndeere/list_object.rb', line 55

def using_etag?
  # will be equal "" or some other string
  return !self.etag.nil?
end