Module: Atrium::Paginate

Included in:
Institution
Defined in:
lib/atrium/paginate.rb

Constant Summary collapse

DEFAULT_RECORDS_PER_PAGE =
25
INITIAL_PAGE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



8
9
10
# File 'lib/atrium/paginate.rb', line 8

def current_page
  @current_page
end

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/atrium/paginate.rb', line 8

def endpoint
  @endpoint
end

#total_pagesObject

Returns the value of attribute total_pages.



8
9
10
# File 'lib/atrium/paginate.rb', line 8

def total_pages
  @total_pages
end

Instance Method Details

#endpoint_name(query_params: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/atrium/paginate.rb', line 10

def endpoint_name(query_params: nil)
  @endpoint = if query_params.present?
     klass_name + "?" + URI.encode_www_form(query_params) + "&"
  else
    klass_name + "?"
  end
end

#get_total_pagesObject



18
19
20
21
22
23
24
25
26
# File 'lib/atrium/paginate.rb', line 18

def get_total_pages
  @current_page = INITIAL_PAGE

  paginated_endpoint = endpoint + "page=#{current_page}&records_per_page=#{records_per_page}"
  response = ::Atrium.client.make_request(:get, paginated_endpoint)

  pagination = response["pagination"]
  @total_pages  = pagination["total_pages"]
end

#klass_nameObject



28
29
30
# File 'lib/atrium/paginate.rb', line 28

def klass_name
  @klass_name ||= self.name.gsub("Atrium::", "").downcase.pluralize
end

#paginate_endpoint(query_params: nil, limit: nil) ⇒ Object



32
33
34
35
36
# File 'lib/atrium/paginate.rb', line 32

def paginate_endpoint(query_params: nil, limit: nil)
  endpoint_name(query_params: query_params)
  get_total_pages
  response_list(limit: limit)
end

#paginate_endpoint_in_batches(query_params: nil, limit: nil, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/atrium/paginate.rb', line 38

def paginate_endpoint_in_batches(query_params: nil, limit: nil, &block)
  return "method requires block to be passed" unless block_given?

  endpoint_name(query_params: query_params)
  get_total_pages
  response_list_in_batches(limit: limit, &block)
end

#records_per_pageObject



46
47
48
# File 'lib/atrium/paginate.rb', line 46

def records_per_page
  @records_per_page ||= DEFAULT_RECORDS_PER_PAGE
end

#response_list(limit: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/atrium/paginate.rb', line 50

def response_list(limit: nil)
  # "total_pages > 1" check exists since some query_params only return 1 page
  @total_pages = limit / records_per_page if limit.present? && total_pages > 1
  list = []

  until current_page > total_pages
    paginated_endpoint =  endpoint + "page=#{current_page}&records_per_page=#{records_per_page}"
    response = ::Atrium.client.make_request(:get, paginated_endpoint)

    # Add new objects to the list
    response["#{klass_name}"].each do |params|
      list << self.new(params)
    end
    @current_page += 1
  end
  list
end

#response_list_in_batches(limit: nil, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/atrium/paginate.rb', line 68

def response_list_in_batches(limit: nil, &block)
  # "total_pages > 1" check exists since some query_params only return 1 page
  @total_pages = limit / records_per_page if limit.present? && total_pages > 1

  until current_page > total_pages
    paginated_endpoint =  endpoint + "page=#{current_page}&records_per_page=#{records_per_page}"
    response = ::Atrium.client.make_request(:get, paginated_endpoint)
    list = []

    response["#{klass_name}"].each do |params|
      list << self.new(params)
    end
    @current_page += 1
    yield list
  end
end