Class: Lighthouse::Facilities::V1::Response

Inherits:
Common::Base show all
Defined in:
lib/lighthouse/facilities/v1/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.



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

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']['currentPage']
    self.per_page = meta['pagination']['perPage']
    self.total_entries = meta['pagination']['totalEntries']
  end
end

Instance Method Details

#facilitiesObject



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

def facilities
  facilities = data.each_with_index.map do |facility, index|
    facility['attributes'] = facility['attributes'].transform_keys(&:underscore)
    fac = Lighthouse::Facilities::Facility.new(facility)
    fac.distance = meta['distances'][index]['distance'] if meta['distances'].present?
    fac
  end

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

#facilityObject



48
49
50
# File 'lib/lighthouse/facilities/v1/response.rb', line 48

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