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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56b5f5486e19eadd1c14d44b474699d7803ba22f
4
- data.tar.gz: 90000cddf0e18ce52096f48c5712ac8aa057dd3f
3
+ metadata.gz: 2039f0d1911215ae70c7d3bf087a058957b3a619
4
+ data.tar.gz: 3c93f9335247015659c65b45021833fe4ecd794f
5
5
  SHA512:
6
- metadata.gz: d8b840d2571bd8c98f05baf7246d4be5d13521b007eb6fb0edbe940949655e755319dd2086436b4f520800240c9dc8122df43356555ca003966cc6eec282192c
7
- data.tar.gz: eefb7e43b4713562381bb0fd9e4c72ffdde678c16f720f1baf4f7d1368c5d9a62b42fed3c8ad37395065e574245db668b363f10845aaf8f62562fc4c881fe59e
6
+ metadata.gz: 8aca6a08dd4b5efae3b07351c67ff82e254c56e7c79f7bdd9fe6150e65e788044f9986a8138eacddbe9dd84318131a8f8f245f25b15dc86a93bd8f5a90a4ec74
7
+ data.tar.gz: 3bddc36e797c7dc4b579d813d279e2829b4e0b4b175b1e0592a09f2501bb6ea9f302374614b243b4b9851e5c7d7f1feec7a7e97701bbe41da0c2408c25e8e234
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ spec/dummy/log/*.log
8
8
  spec/dummy/tmp/
9
9
  spec/dummy/public/system/
10
10
  spec/dummy/.sass-cache
11
+ tmp/
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
- flattened.detect{|l| l.id == id }
39
+ tables[:id_table][id]
26
40
  end
27
41
 
28
42
  def find_by_code(code)
29
- flattened.detect{|l| l.code == code }
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 = all.detect {|l| l.code == parent_code}
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 + children).flatten
61
+ (all + all.map {|l| l.child_locations}.compact).flatten
52
62
  end
53
63
 
54
64
  def all
55
- @locations ||= transform_data(request('/locations')).try(:compact) || []
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
- cache.fetch(CACHE_KEY) do
62
- logger.debug url
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
@@ -1,5 +1,5 @@
1
1
  module LocationServiceClient
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.3".freeze
3
3
 
4
4
  def self.version
5
5
  VERSION
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.0
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-24 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: config