Class: Location
- Inherits:
-
Struct
- Object
- Struct
- Location
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/location.rb
Constant Summary collapse
- KEY_ORDER =
For mapping the JSON camelcase data from the location service into the Location structs:
[:id, :sasId, :name, :code, :kind, :lat, :lng, :timezone, :parentLocationId, :updatedAt, :createdAt, :childLocations, :beacons].map(&:to_s).freeze
- CACHE_KEY =
'location_service_request_cache'.freeze
Instance Attribute Summary collapse
-
#beacons ⇒ Object
Returns the value of attribute beacons.
-
#child_locations ⇒ Object
Returns the value of attribute child_locations.
-
#code ⇒ Object
Returns the value of attribute code.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent_location_id ⇒ Object
Returns the value of attribute parent_location_id.
-
#sas_id ⇒ Object
Returns the value of attribute sas_id.
-
#timezone ⇒ Object
Returns the value of attribute timezone.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
- .all ⇒ Object
- .cache ⇒ Object
- .children ⇒ Object
- .find(id) ⇒ Object
- .find_by_code(code) ⇒ Object
- .find_by_codes(codes, child_locations = nil) ⇒ Object
- .find_by_parent_code_and_child_name(parent_code, child_name) ⇒ Object (also: find_by_airport_and_gate)
- .flattened ⇒ Object
- .logger ⇒ Object
- .request(endpoint) ⇒ Object
- .reset ⇒ Object
- .time ⇒ Object
- .transform_data(data) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#beacons ⇒ Object
Returns the value of attribute beacons
8 9 10 |
# File 'lib/location.rb', line 8 def beacons @beacons end |
#child_locations ⇒ Object
Returns the value of attribute child_locations
8 9 10 |
# File 'lib/location.rb', line 8 def child_locations @child_locations end |
#code ⇒ Object
Returns the value of attribute code
8 9 10 |
# File 'lib/location.rb', line 8 def code @code end |
#created_at ⇒ Object
Returns the value of attribute created_at
8 9 10 |
# File 'lib/location.rb', line 8 def created_at @created_at end |
#id ⇒ Object
Returns the value of attribute id
8 9 10 |
# File 'lib/location.rb', line 8 def id @id end |
#kind ⇒ Object
Returns the value of attribute kind
8 9 10 |
# File 'lib/location.rb', line 8 def kind @kind end |
#lat ⇒ Object
Returns the value of attribute lat
8 9 10 |
# File 'lib/location.rb', line 8 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng
8 9 10 |
# File 'lib/location.rb', line 8 def lng @lng end |
#name ⇒ Object
Returns the value of attribute name
8 9 10 |
# File 'lib/location.rb', line 8 def name @name end |
#parent_location_id ⇒ Object
Returns the value of attribute parent_location_id
8 9 10 |
# File 'lib/location.rb', line 8 def parent_location_id @parent_location_id end |
#sas_id ⇒ Object
Returns the value of attribute sas_id
8 9 10 |
# File 'lib/location.rb', line 8 def sas_id @sas_id end |
#timezone ⇒ Object
Returns the value of attribute timezone
8 9 10 |
# File 'lib/location.rb', line 8 def timezone @timezone end |
#updated_at ⇒ Object
Returns the value of attribute updated_at
8 9 10 |
# File 'lib/location.rb', line 8 def updated_at @updated_at end |
Class Method Details
.all ⇒ Object
54 55 56 |
# File 'lib/location.rb', line 54 def all @locations ||= transform_data(request('/locations')).try(:compact) || [] end |
.cache ⇒ Object
91 92 93 |
# File 'lib/location.rb', line 91 def cache defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new end |
.children ⇒ Object
46 47 48 |
# File 'lib/location.rb', line 46 def children all.map{|l| l.child_locations }.compact end |
.find(id) ⇒ Object
24 25 26 |
# File 'lib/location.rb', line 24 def find(id) flattened.detect{|l| l.id == id } end |
.find_by_code(code) ⇒ Object
28 29 30 |
# File 'lib/location.rb', line 28 def find_by_code(code) flattened.detect{|l| l.code == code } end |
.find_by_codes(codes, child_locations = nil) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/location.rb', line 38 def find_by_codes(codes, child_locations=nil) child_locations ||= all codes = [codes].flatten code = codes.shift leaf = child_locations.detect{|l| l.code == code } codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations) end |
.find_by_parent_code_and_child_name(parent_code, child_name) ⇒ Object Also known as: find_by_airport_and_gate
32 33 34 35 |
# File 'lib/location.rb', line 32 def find_by_parent_code_and_child_name(parent_code, child_name) airport = all.detect {|l| l.code == parent_code} airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name } end |
.flattened ⇒ Object
50 51 52 |
# File 'lib/location.rb', line 50 def flattened (all + children).flatten end |
.logger ⇒ Object
95 96 97 |
# File 'lib/location.rb', line 95 def logger defined?(Rails) ? Rails.logger : Logger.new(STDOUT) end |
.request(endpoint) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/location.rb', line 58 def request(endpoint) user_token = Thread.current[:current_user].try(:authentication_token) url = Settings.location_service.url + "v1/" + endpoint + "?api_key=#{user_token||Settings.location_service.token}" cache.fetch(CACHE_KEY) do logger.debug url JSON.parse(RestClient.get(url)) end end |
.reset ⇒ Object
67 68 69 70 |
# File 'lib/location.rb', line 67 def reset Rails.cache.delete(CACHE_KEY) @locations = nil end |
.time ⇒ Object
88 89 90 |
# File 'lib/location.rb', line 88 def time Time.zone || Time end |
.transform_data(data) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/location.rb', line 72 def transform_data(data) if data.kind_of? Array data.map { |v| transform_data(v) } elsif data.kind_of? Hash #HashWithIndifferentAccess[data.map {|k, v| [k.to_s.underscore, transform_data(v)] }] l = Location.new(*data.values_at(*KEY_ORDER)) l.created_at = data['createdAt'] && time.at( data['createdAt']/1000 ) l.updated_at = data['updatedAt'] && time.at( data['updatedAt']/1000 ) l.child_locations = transform_data( data['childLocations'] ) || [] l else data end end |
Instance Method Details
#ruby_timezone ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/location.rb', line 11 def ruby_timezone begin TZInfo::Timezone.get(timezone) if timezone && !timezone.empty? rescue TZInfo::InvalidTimezoneIdentifier raise TZInfo::InvalidTimezoneIdentifier.new("'#{timezone}' is an invalid identifier") end end |