Class: Lighthouse::Facilities::Response

Inherits:
Common::Base show all
Defined in:
lib/lighthouse/facilities/response.rb

Instance Attribute Summary

Attributes inherited from Common::Base

#errors_hash, #metadata

Instance Method Summary collapse

Methods inherited from Common::Base

#changed, #changed?, #changes, default_sort, filterable_attributes, max_per_page, per_page, sortable_attributes

Constructor Details

#initialize(body, status) ⇒ Response

Returns a new instance of Response.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lighthouse/facilities/response.rb', line 18

def initialize(body, status)
  super()
  self.body = body
  self.status = status
  parsed_body = JSON.parse(body)
  self.data = parsed_body['data']
  self.meta = parsed_body['meta']
  self.links = parsed_body['links']
  if meta
    self.current_page = meta['pagination']['current_page']
    self.per_page = meta['pagination']['per_page']
    self.total_entries = meta['pagination']['total_entries']
  end
end

Instance Method Details

#facilitiesObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lighthouse/facilities/response.rb', line 33

def facilities
  facilities = data.each_with_index.map do |facility, index|
    fac = Lighthouse::Facilities::Facility.new(facility)
    fac.distance = meta['distances'][index]['distance'] unless meta['distances'].empty?
    fac
  end

  WillPaginate::Collection.create(current_page, per_page) do |pager|
    pager.replace(facilities)
    pager.total_entries = total_entries
  end
end

#facilityObject



46
47
48
# File 'lib/lighthouse/facilities/response.rb', line 46

def facility
  Lighthouse::Facilities::Facility.new(data)
end