Class: Asari::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/makasi/asari_patch.rb

Instance Method Summary collapse

Constructor Details

#initialize(httparty_response, page_size) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/makasi/asari_patch.rb', line 5

def initialize(httparty_response, page_size)
  resp = httparty_response.parsed_response
  @total_entries = resp["hits"]["found"]
  @page_size = page_size

  complete_pages = (@total_entries / @page_size)
  @total_pages = (@total_entries % @page_size > 0) ? complete_pages + 1 : complete_pages
  # There's always one page, even for no results
  @total_pages = 1 if @total_pages == 0

  start = resp["hits"]["start"]
  @current_page = (start / page_size) + 1
  if resp["hits"]["hit"].first && resp["hits"]["hit"].first["fields"]
    @data = {}
    resp["hits"]["hit"].each { |hit|  @data[hit["id"]] = hit["fields"]}
  else
    @data = resp["hits"]["hit"].map { |hit| hit["id"] }
  end
end