location-service-client 0.1.0 → 0.1.3
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/.gitignore +1 -0
- data/CHANGELOG.md +11 -0
- data/lib/location.rb +31 -16
- data/lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2039f0d1911215ae70c7d3bf087a058957b3a619
|
4
|
+
data.tar.gz: 3c93f9335247015659c65b45021833fe4ecd794f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8aca6a08dd4b5efae3b07351c67ff82e254c56e7c79f7bdd9fe6150e65e788044f9986a8138eacddbe9dd84318131a8f8f245f25b15dc86a93bd8f5a90a4ec74
|
7
|
+
data.tar.gz: 3bddc36e797c7dc4b579d813d279e2829b4e0b4b175b1e0592a09f2501bb6ea9f302374614b243b4b9851e5c7d7f1feec7a7e97701bbe41da0c2408c25e8e234
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
|
2
|
+
0.1.2 6/28/2016
|
3
|
+
==============
|
4
|
+
|
5
|
+
Cache hash tables for lookup by id and code. Memoize that cache in the cacheless test environment to greatly speed up test coverage of models that have locations.
|
6
|
+
|
7
|
+
0.1.1 6/28/2016
|
8
|
+
==============
|
9
|
+
|
10
|
+
Replace memoization with the rails cache.
|
11
|
+
|
1
12
|
0.1.0 6/24/2016
|
2
13
|
==============
|
3
14
|
|
data/lib/location.rb
CHANGED
@@ -8,6 +8,7 @@ require 'active_record'
|
|
8
8
|
class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :timezone, :parent_location_id, :updated_at, :created_at, :child_locations, :beacons)
|
9
9
|
include ActiveModel::Validations
|
10
10
|
|
11
|
+
|
11
12
|
def ruby_timezone
|
12
13
|
begin
|
13
14
|
TZInfo::Timezone.get(timezone) if timezone && !timezone.empty?
|
@@ -20,17 +21,30 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
20
21
|
# For mapping the JSON camelcase data from the location service into the Location structs:
|
21
22
|
KEY_ORDER = [:id, :sasId, :name, :code, :kind, :lat, :lng, :timezone, :parentLocationId, :updatedAt, :createdAt, :childLocations, :beacons].map(&:to_s).freeze
|
22
23
|
CACHE_KEY = 'location_service_request_cache'.freeze
|
24
|
+
MEMOS_KEY = 'location_service_request_tables'.freeze
|
25
|
+
|
26
|
+
def tables
|
27
|
+
@t = nil if !testing? # test doesn't have a cache, so memoize in the test env
|
28
|
+
@t ||= cache.fetch(MEMOS_KEY) do
|
29
|
+
t = { id_table: {}, code_table: {} }
|
30
|
+
flattened.each do |l|
|
31
|
+
t[:id_table][l.id] = l
|
32
|
+
t[:code_table][l.code] = l
|
33
|
+
end
|
34
|
+
t
|
35
|
+
end
|
36
|
+
end
|
23
37
|
|
24
38
|
def find(id)
|
25
|
-
|
39
|
+
tables[:id_table][id]
|
26
40
|
end
|
27
41
|
|
28
42
|
def find_by_code(code)
|
29
|
-
|
43
|
+
tables[:code_table][code]
|
30
44
|
end
|
31
45
|
|
32
46
|
def find_by_parent_code_and_child_name(parent_code, child_name)
|
33
|
-
airport =
|
47
|
+
airport = find_by_code(parent_code)
|
34
48
|
airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name }
|
35
49
|
end
|
36
50
|
alias_method :find_by_airport_and_gate, :find_by_parent_code_and_child_name
|
@@ -43,30 +57,27 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
43
57
|
codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations)
|
44
58
|
end
|
45
59
|
|
46
|
-
def children
|
47
|
-
all.map{|l| l.child_locations }.compact
|
48
|
-
end
|
49
|
-
|
50
60
|
def flattened
|
51
|
-
(all +
|
61
|
+
(all + all.map {|l| l.child_locations}.compact).flatten
|
52
62
|
end
|
53
63
|
|
54
64
|
def all
|
55
|
-
|
65
|
+
cache.fetch(CACHE_KEY) do
|
66
|
+
Location.reset # make sure you reset the memo cache on a cache miss
|
67
|
+
transform_data(request('/locations').try(:compact) || [])
|
68
|
+
end
|
56
69
|
end
|
57
70
|
|
58
71
|
def request(endpoint)
|
59
72
|
user_token = Thread.current[:current_user].try(:authentication_token)
|
60
73
|
url = Settings.location_service.url + "v1/" + endpoint + "?api_key=#{user_token||Settings.location_service.token}"
|
61
|
-
|
62
|
-
|
63
|
-
JSON.parse(RestClient.get(url))
|
64
|
-
end
|
74
|
+
logger.debug url
|
75
|
+
JSON.parse(RestClient.get(url))
|
65
76
|
end
|
66
77
|
|
67
78
|
def reset
|
79
|
+
Rails.cache.delete(MEMOS_KEY)
|
68
80
|
Rails.cache.delete(CACHE_KEY)
|
69
|
-
@locations = nil
|
70
81
|
end
|
71
82
|
|
72
83
|
def transform_data(data)
|
@@ -74,7 +85,6 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
74
85
|
data.map { |v| transform_data(v) }
|
75
86
|
|
76
87
|
elsif data.kind_of? Hash
|
77
|
-
#HashWithIndifferentAccess[data.map {|k, v| [k.to_s.underscore, transform_data(v)] }]
|
78
88
|
l = Location.new(*data.values_at(*KEY_ORDER))
|
79
89
|
l.created_at = data['createdAt'] && time.at( data['createdAt']/1000 )
|
80
90
|
l.updated_at = data['updatedAt'] && time.at( data['updatedAt']/1000 )
|
@@ -88,8 +98,13 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
|
|
88
98
|
def time
|
89
99
|
Time.zone || Time
|
90
100
|
end
|
101
|
+
|
91
102
|
def cache
|
92
|
-
defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new
|
103
|
+
@cache ||= defined?(Rails) ? Rails.cache : ActiveSupport::Cache::FileStore.new('tmp/cache')
|
104
|
+
end
|
105
|
+
|
106
|
+
def testing?
|
107
|
+
defined?(Rails) && Rails.env.test?
|
93
108
|
end
|
94
109
|
|
95
110
|
def logger
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: location-service-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Buermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: config
|