location-service-client 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/location.rb +53 -22
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44b1dcf91c7fa1c4b582032349577547fbd00fe6
|
4
|
+
data.tar.gz: 8a799c3134b32d07744f66aa3e69c32efc1fd633
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa5da578d26aa90640c56e03d8b7bf09659251535d43e70e2629d0266d4920295bb2225b66e3c53d2750b70fd1038c0ce58afdd000ab2615ab2314646727c5eb
|
7
|
+
data.tar.gz: 496d678862cafce83cdbf99da8f8163ad0210fed5cb9fd112e8d0aa617023b4e2a2d44a0320b89f7dfb861811e67e403fa6e6275a3dbcd6dba2b0d2c040bd7ac
|
data/CHANGELOG.md
CHANGED
data/lib/location.rb
CHANGED
@@ -20,51 +20,78 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
20
20
|
class << self
|
21
21
|
# For mapping the JSON camelcase data from the location service into the Location structs:
|
22
22
|
KEY_ORDER = [:id, :sasId, :name, :code, :kind, :lat, :lng, :timezone, :parentLocationId, :updatedAt, :createdAt, :childLocations, :beacons].map(&:to_s).freeze
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
CACHE_PRE = "location_service_client".freeze
|
24
|
+
BENCHMARK = false.freeze
|
25
|
+
|
26
26
|
def tables
|
27
27
|
@t = nil if !testing? # test doesn't have a cache, so memoize in the test env
|
28
|
-
@t ||= cache.fetch(
|
29
|
-
t = { id_table:
|
30
|
-
flattened.each do |l|
|
28
|
+
@t ||= cache.fetch("#{CACHE_PRE}_memos") do
|
29
|
+
t = { id_table: [], code_table: {} }
|
30
|
+
flattened.sort_by(&:id).each do |l|
|
31
31
|
t[:id_table][l.id] = l
|
32
32
|
t[:code_table][l.code] = l
|
33
33
|
end
|
34
34
|
t
|
35
35
|
end
|
36
|
-
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def bench # e.g. logb(caller[0])
|
39
|
+
if !BENCHMARK
|
40
|
+
yield
|
41
|
+
else
|
42
|
+
start = Time.now
|
43
|
+
v = yield
|
44
|
+
logger.fatal v.try(:name)
|
45
|
+
logger.fatal "#{caller[0].split(' ').last} #{(Time.now-start)*1000}ms"
|
46
|
+
v
|
47
|
+
end
|
48
|
+
end
|
37
49
|
|
38
50
|
def find(id)
|
39
|
-
|
51
|
+
bench do
|
52
|
+
cache.fetch("#{CACHE_PRE}-id-#{id}") do
|
53
|
+
tables[:id_table][id]
|
54
|
+
end
|
55
|
+
end
|
40
56
|
end
|
41
57
|
|
42
58
|
def find_by_code(code)
|
43
|
-
|
59
|
+
bench do
|
60
|
+
cache.fetch("#{CACHE_PRE}-code-#{code}") do
|
61
|
+
tables[:code_table][code]
|
62
|
+
end
|
63
|
+
end
|
44
64
|
end
|
45
65
|
|
46
66
|
def find_by_parent_code_and_child_name(parent_code, child_name)
|
47
|
-
|
48
|
-
|
67
|
+
bench do
|
68
|
+
airport = find_by_code(parent_code)
|
69
|
+
airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name }
|
70
|
+
end
|
49
71
|
end
|
50
72
|
alias_method :find_by_airport_and_gate, :find_by_parent_code_and_child_name
|
51
73
|
|
52
74
|
def find_by_codes(codes, child_locations=nil)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
75
|
+
bench do
|
76
|
+
child_locations ||= all
|
77
|
+
codes = [codes].flatten
|
78
|
+
code = codes.shift
|
79
|
+
leaf = child_locations.detect{|l| l.code == code }
|
80
|
+
codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations)
|
81
|
+
end
|
58
82
|
end
|
59
83
|
|
60
84
|
def flattened
|
61
|
-
|
85
|
+
bench do
|
86
|
+
(all + all.map {|l| l.child_locations}.compact).flatten
|
87
|
+
end
|
62
88
|
end
|
63
89
|
|
64
90
|
def all
|
65
|
-
|
66
|
-
|
67
|
-
|
91
|
+
bench do
|
92
|
+
cache.fetch("#{CACHE_PRE}_request") do
|
93
|
+
transform_data(request('/locations').try(:compact) || [])
|
94
|
+
end
|
68
95
|
end
|
69
96
|
end
|
70
97
|
|
@@ -76,8 +103,12 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
76
103
|
end
|
77
104
|
|
78
105
|
def reset
|
79
|
-
|
80
|
-
|
106
|
+
flattened.each do |l|
|
107
|
+
Rails.cache.delete("#{CACHE_PRE}-id-#{l.id}")
|
108
|
+
Rails.cache.delete("#{CACHE_PRE}-code-#{l.code}")
|
109
|
+
end
|
110
|
+
Rails.cache.delete("#{CACHE_PRE}_memos")
|
111
|
+
Rails.cache.delete("#{CACHE_PRE}_request")
|
81
112
|
end
|
82
113
|
|
83
114
|
def transform_data(data)
|
data/lib/version.rb
CHANGED