Class: Extensiv::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/extensiv/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, next_page:, prev_page:) ⇒ Collection

Returns a new instance of Collection.



16
17
18
19
20
# File 'lib/extensiv/collection.rb', line 16

def initialize(data:, next_page:, prev_page:)
  @data = data
  @next_page = next_page
  @prev_page = prev_page
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/extensiv/collection.rb', line 5

def data
  @data
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



5
6
7
# File 'lib/extensiv/collection.rb', line 5

def next_page
  @next_page
end

#prev_pageObject (readonly)

Returns the value of attribute prev_page.



5
6
7
# File 'lib/extensiv/collection.rb', line 5

def prev_page
  @prev_page
end

Class Method Details

.from_response(response, type:, params: nil) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/extensiv/collection.rb', line 7

def self.from_response(response, type:, params: nil)
  body = response.body
  new(
    data: body["response"].map { |attrs| type.new(attrs) },
    next_page: params.empty? ? 2 : next_page(params),
    prev_page: params.empty? ? nil : prev_page(params)
  )
end

.next_page(params) ⇒ Object



22
23
24
25
26
# File 'lib/extensiv/collection.rb', line 22

def self.next_page(params)
  return @next_page = params[:page].to_i + 1 if params[:page].present? && params[:page].to_i >= 1

  @next_page = 2
end

.prev_page(params) ⇒ Object



28
29
30
31
32
# File 'lib/extensiv/collection.rb', line 28

def self.prev_page(params)
  return @prev_page = params[:page].to_i - 1 if params[:page].present? && params[:page].to_i > 1

  @prev_page = nil
end