Class: Justifi::ListObject

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/justifi/list_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(justifi_object:, path:, params: {}, headers: {}) ⇒ ListObject

Returns a new instance of ListObject.



21
22
23
24
25
26
# File 'lib/justifi/list_object.rb', line 21

def initialize(justifi_object:, path:, params: {}, headers: {})
  @justifi_object = justifi_object
  @path = path
  @request_params = params
  @request_headers = headers
end

Instance Attribute Details

#justifi_objectObject (readonly)

Returns the value of attribute justifi_object.



7
8
9
# File 'lib/justifi/list_object.rb', line 7

def justifi_object
  @justifi_object
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/justifi/list_object.rb', line 8

def path
  @path
end

#request_headersObject

Returns the value of attribute request_headers.



8
9
10
# File 'lib/justifi/list_object.rb', line 8

def request_headers
  @request_headers
end

#request_paramsObject

Returns the value of attribute request_params.



8
9
10
# File 'lib/justifi/list_object.rb', line 8

def request_params
  @request_params
end

Class Method Details

.empty_list(headers = {}) ⇒ Object

An empty list object to return when has_next or has_previous does not exist



17
18
19
# File 'lib/justifi/list_object.rb', line 17

def self.empty_list(headers = {})
  JustifiObject.construct_from({data: []}, headers)
end

.list(path, params = {}, headers = {}) ⇒ Object



73
74
75
76
# File 'lib/justifi/list_object.rb', line 73

def self.list(path, params = {}, headers = {})
  justifi_object = JustifiOperations.execute_get_request(path, params, headers)
  new(justifi_object: justifi_object, path: path, params: params, headers: headers)
end

Instance Method Details

#each(&block) ⇒ Object

Iterates through each resource in the page represented by the current ‘ListObject`.



30
31
32
# File 'lib/justifi/list_object.rb', line 30

def each(&block)
  data.each(&block)
end

#empty?Boolean

Returns true if the page object contains no elements.

Returns:

  • (Boolean)


35
36
37
# File 'lib/justifi/list_object.rb', line 35

def empty?
  data.empty?
end

#end_cursorObject



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

def end_cursor
  page_info.end_cursor
end

#has_nextObject



43
44
45
# File 'lib/justifi/list_object.rb', line 43

def has_next
  page_info.has_next
end

#has_previousObject



39
40
41
# File 'lib/justifi/list_object.rb', line 39

def has_previous
  page_info.has_previous
end

#next_page(params = {}, headers = {}) ⇒ Object

Fetches the next page based on page_info paginaton



56
57
58
59
60
61
62
# File 'lib/justifi/list_object.rb', line 56

def next_page(params = {}, headers = {})
  return self.class.empty_list(headers) unless has_next

  params[:after_cursor] = end_cursor

  Justifi::ListObject.list(path, @request_params.merge(params), @request_headers.merge(headers))
end

#previous_page(params = {}, headers = {}) ⇒ Object

Fetches the next page based on page_info paginaton



65
66
67
68
69
70
71
# File 'lib/justifi/list_object.rb', line 65

def previous_page(params = {}, headers = {})
  return self.class.empty_list(headers) unless has_previous

  params[:before_cursor] = start_cursor

  Justifi::ListObject.list(path, @request_params.merge(params), @request_headers.merge(headers))
end

#start_cursorObject



47
48
49
# File 'lib/justifi/list_object.rb', line 47

def start_cursor
  page_info.start_cursor
end