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_PRE =
"location_service_client".freeze
- BENCHMARK =
false.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
-
.bench ⇒ Object
e.g.
- .cache ⇒ 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
- .tables ⇒ Object
- .testing? ⇒ Boolean
- .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
90 91 92 93 94 95 96 |
# File 'lib/location.rb', line 90 def all bench do cache.fetch("#{CACHE_PRE}_request") do transform_data(request('/locations').try(:compact) || []) end end end |
.bench ⇒ Object
e.g. logb(caller)
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/location.rb', line 38 def bench # e.g. logb(caller[0]) if !BENCHMARK yield else start = Time.now v = yield logger.fatal v.try(:name) logger.fatal "#{caller[0].split(' ').last} #{(Time.now-start)*1000}ms" v end end |
.cache ⇒ Object
133 134 135 |
# File 'lib/location.rb', line 133 def cache @cache ||= defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new('tmp/cache') end |
.find(id) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/location.rb', line 50 def find(id) bench do cache.fetch("#{CACHE_PRE}-id-#{id}") do tables[:id_table][id] end end end |
.find_by_code(code) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/location.rb', line 58 def find_by_code(code) bench do cache.fetch("#{CACHE_PRE}-code-#{code}") do tables[:code_table][code] end end end |
.find_by_codes(codes, child_locations = nil) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/location.rb', line 74 def find_by_codes(codes, child_locations=nil) bench do 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 end |
.find_by_parent_code_and_child_name(parent_code, child_name) ⇒ Object Also known as: find_by_airport_and_gate
66 67 68 69 70 71 |
# File 'lib/location.rb', line 66 def find_by_parent_code_and_child_name(parent_code, child_name) bench do airport = find_by_code(parent_code) airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name } end end |
.flattened ⇒ Object
84 85 86 87 88 |
# File 'lib/location.rb', line 84 def flattened bench do (all + all.map {|l| l.child_locations}.compact).flatten end end |
.logger ⇒ Object
141 142 143 |
# File 'lib/location.rb', line 141 def logger defined?(Rails) ? Rails.logger : Logger.new(STDOUT) end |
.request(endpoint) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/location.rb', line 98 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}" logger.debug url JSON.parse(RestClient.get(url)) end |
.reset ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/location.rb', line 105 def reset flattened.each do |l| Rails.cache.delete("#{CACHE_PRE}-id-#{l.id}") Rails.cache.delete("#{CACHE_PRE}-code-#{l.code}") end Rails.cache.delete("#{CACHE_PRE}_memos") Rails.cache.delete("#{CACHE_PRE}_request") end |
.tables ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/location.rb', line 26 def tables @t = nil if !testing? # test doesn't have a cache, so memoize in the test env @t ||= cache.fetch("#{CACHE_PRE}_memos") do t = { id_table: [], code_table: {} } flattened.sort_by(&:id).each do |l| t[:id_table][l.id] = l t[:code_table][l.code] = l end t end end |
.testing? ⇒ Boolean
137 138 139 |
# File 'lib/location.rb', line 137 def testing? defined?(Rails) && Rails.env.test? end |
.time ⇒ Object
129 130 131 |
# File 'lib/location.rb', line 129 def time Time.zone || Time end |
.transform_data(data) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/location.rb', line 114 def transform_data(data) if data.kind_of? Array data.map { |v| transform_data(v) } elsif data.kind_of? Hash 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
12 13 14 15 16 17 18 |
# File 'lib/location.rb', line 12 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 |