Skip to content

Commit bbede35

Browse files
committed
adding a RemoteModel class as a superclass to RemoteTag and RemoteCity; implemented client-side caching in the superclass, using ActiveSupport::Cache::MemoryStore
1 parent 5ebabbd commit bbede35

File tree

5 files changed

+123
-73
lines changed

5 files changed

+123
-73
lines changed

app/models/remote_city.rb

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
1-
class RemoteCity
1+
class RemoteCity < RemoteModel
22
class << self
3-
attr_accessor :host, :api_version, :max_concurrency
4-
5-
def max_concurrency
6-
@max_concurrency ||= 1
7-
end
8-
9-
def api_version
10-
@api_version ||= '1'
11-
end
12-
133
def find_nearby_city_id(id, within = 15)
14-
raise "RemoteCity.host needs to be set" unless host
15-
JSON.parse(Typhoeus.get("#{host}/api/v#{api_version}/cities/#{id}/nearby?within=#{within}").body).map do |hsh|
16-
Hashie::Mash.new hsh
4+
with_possible_cache(cache_key_for('find_nearby_city_id', id, within)) do
5+
JSON.parse(Typhoeus.get("#{host}/api/v#{api_version}/#{resources}/#{id}/nearby?within=#{within}").body).map do |hsh|
6+
Hashie::Mash.new hsh
7+
end
178
end
189
end
1910

2011
def find_by_city_ids(ids)
21-
return [] unless ids.size > 0
22-
raise "RemoteCity.host needs to be set" unless host
23-
requests = []
24-
ids.each do |id|
25-
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/cities/#{id}", followlocation: true)
26-
end
27-
requests.each { |r| hydra.queue(r) }
28-
hydra.run
29-
requests.keep_if{ |req| req.response.success? }
30-
requests.map {|req| JSON.parse(req.response.body)}.flatten(1).map {|hsh| Hashie::Mash.new(hsh)}
31-
end
32-
33-
private
34-
def hydra
35-
@hydra ||= begin
36-
Typhoeus::Config.memoize = true
37-
Typhoeus::Hydra.new(max_concurrency: max_concurrency)
38-
end
12+
find_by_ids(ids, true)
3913
end
4014
end
4115
end

app/models/remote_model.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class RemoteModel
2+
class << self
3+
attr_accessor :host, :api_version, :max_concurrency, :cache
4+
5+
def max_concurrency
6+
@max_concurrency ||= 1
7+
end
8+
9+
def api_version
10+
@api_version ||= '1'
11+
end
12+
13+
def host
14+
raise "#{self.name}.host needs to be set" unless @host
15+
@host
16+
end
17+
18+
def resource
19+
@resource ||= self.name.underscore.split('_').last
20+
end
21+
22+
def resources
23+
resource.pluralize
24+
end
25+
26+
def find_by_name(name)
27+
with_possible_cache(cache_key_for('find_by_name', name)) do
28+
Hashie::Mash.new(
29+
JSON.parse(Typhoeus.get("#{host}/api/v#{api_version}/#{resources}/#{name}").body)
30+
)
31+
end
32+
end
33+
34+
def find_by_id(id)
35+
find_by_name(id)
36+
end
37+
38+
def find_by_ids(ids, concurrent = false)
39+
return [] unless ids.size > 0
40+
with_possible_cache(cache_key_for('find_by_ids', *ids)) do
41+
if !concurrent
42+
ids.map{ |id| find_by_id(id) }
43+
else
44+
requests = []
45+
ids.each do |id|
46+
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}/#{id}", followlocation: true)
47+
end
48+
get_concurrent_results(requests)
49+
end
50+
end
51+
end
52+
53+
def destroy_by_name(name)
54+
Typhoeus::Request.delete("#{host}/api/v#{api_version}/#{resources}/#{name}", followlocation: true)
55+
end
56+
57+
def destroy_by_id(id)
58+
destroy_by_name(id)
59+
end
60+
61+
def destroy_by_ids(ids, concurrent = false)
62+
return [] unless ids.size > 0
63+
if !concurrent
64+
ids.map{ |id| destroy_by_id(id) }
65+
else
66+
requests = []
67+
ids.each do |id|
68+
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}/#{id}", method: :delete, followlocation: true)
69+
end
70+
get_concurrent_results(requests)
71+
end
72+
end
73+
74+
def get_concurrent_results(requests)
75+
return [] unless requests.size > 0
76+
requests.each { |r| hydra.queue(r) }
77+
hydra.run
78+
requests.keep_if{ |req| req.response.code >= 200 && req.response.code < 300 }
79+
requests.map {|req| req.response.body.blank? ? [] : JSON.parse(req.response.body)}.flatten(1).map {|hsh| Hashie::Mash.new(hsh)}
80+
end
81+
82+
private
83+
84+
def with_possible_cache(cache_key)
85+
if cache
86+
cache.fetch(cache_key) do
87+
yield
88+
end
89+
else
90+
yield
91+
end
92+
end
93+
94+
def cache_key_for(*parts)
95+
([resource, host, api_version] + parts).join('-')
96+
end
97+
98+
def hydra
99+
@hydra ||= begin
100+
Typhoeus::Config.memoize = true
101+
Typhoeus::Hydra.new(max_concurrency: max_concurrency)
102+
end
103+
end
104+
end
105+
end

app/models/remote_tag.rb

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,25 @@
1-
class RemoteTag
1+
class RemoteTag < RemoteModel
22
class << self
3-
attr_accessor :host, :api_version, :max_concurrency
4-
5-
def max_concurrency
6-
@max_concurrency ||= 1
7-
end
8-
9-
def api_version
10-
@api_version ||= '1'
11-
end
12-
13-
def find_by_name(name)
14-
raise "RemoteTag.host needs to be set" unless host
15-
Hashie::Mash.new(
16-
JSON.parse(Typhoeus.get("#{host}/api/v#{api_version}/tags/#{name}").body)
17-
)
18-
end
193

204
def find_by_inventory_item_ids(ids)
21-
return [] unless ids.size > 0
22-
raise "RemoteTag.host needs to be set" unless host
23-
requests = []
24-
ids.each do |id|
25-
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/tags?item_id=#{id}", followlocation: true)
5+
with_possible_cache(cache_key_for('find_by_inventory_item_ids', *ids)) do
6+
requests = []
7+
ids.each do |id|
8+
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}?item_id=#{id}", followlocation: true)
9+
end
10+
get_concurrent_results(requests)
2611
end
27-
requests.each { |r| hydra.queue(r) }
28-
hydra.run
29-
requests.keep_if{ |req| req.response.success? }
30-
requests.map {|req| JSON.parse(req.response.body)}.flatten(1).map {|hsh| Hashie::Mash.new(hsh)}
3112
end
3213

3314
def destroy_tags_for_inventory_item (item_id, tag_names = [])
3415
return false unless item_id
3516
return true if tag_names && tag_names.size == 0
36-
raise "RemoteTag.host needs to be set" unless host
37-
3817
requests = []
3918
tag_names.each do |tag_name|
40-
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/tags/#{tag_name}/tagged_items/#{item_id}", method: :delete, followlocation: true)
41-
end
42-
requests.each { |r| hydra.queue(r) }
43-
hydra.run
44-
requests.keep_if{ |req| req.response.code == 204 }
45-
return requests.size > 0
46-
end
47-
48-
private
49-
def hydra
50-
@hydra ||= begin
51-
Typhoeus::Config.memoize = true
52-
Typhoeus::Hydra.new(max_concurrency: max_concurrency)
19+
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/#{resources}/#{tag_name}/tagged_items/#{item_id}", method: :delete, followlocation: true)
5320
end
21+
results = get_concurrent_results(requests)
22+
return results.size > 0
5423
end
5524
end
5625
end

config/initializers/cities_service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
RemoteCity.host = config['port'] ? "#{config['host']}:#{config['port']}" : "#{config['host']}"
44
RemoteCity.api_version = "#{config['api_version'] || '1'}"
55
RemoteCity.max_concurrency = config['max_concurrency'] || 1
6+
RemoteCity.cache = ActiveSupport::Cache.lookup_store(:memory_store, size: 6.megabytes)

config/initializers/tags_service.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
RemoteTag.host = config['port'] ? "#{config['host']}:#{config['port']}" : "#{config['host']}"
44
RemoteTag.api_version = "#{config['api_version'] || '1'}"
55
RemoteTag.max_concurrency = config['max_concurrency'] || 1
6+
RemoteTag.cache = ActiveSupport::Cache.lookup_store(:memory_store)

0 commit comments

Comments
 (0)