Skip to content

Commit c709caf

Browse files
author
Tim Schmelmer
committed
adding all code to retrieve (and destroy) tags for a city; includes tags_service config and after_destroy hooks on a city
1 parent 732b2bf commit c709caf

File tree

5 files changed

+102
-10
lines changed

5 files changed

+102
-10
lines changed

app/models/city.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,21 @@ class City < ActiveRecord::Base
99
validates_numericality_of :latitude, allow_nil: true
1010
validates_numericality_of :longitude, allow_nil: true
1111

12+
after_destroy :destroy_remote_tags
13+
14+
def tags(refresh = false)
15+
@tags = RemoteTag.find_by_city_ids([id]) if refresh || !@tags
16+
@tags
17+
end
18+
1219
def geocode_string
1320
"#{name}_#{state}_#{country}"
1421
end
22+
23+
private
24+
25+
def destroy_remote_tags
26+
RemoteTag.destroy_tags_for_city(id, tags.map(&:name))
27+
end
28+
1529
end

app/models/remote_tag.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class RemoteTag
2+
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 ||= '2'
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}?item_type=city").body)
17+
)
18+
end
19+
20+
def find_by_city_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}&item_type=city", 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+
def destroy_tags_for_city (city_id, tag_names = [])
34+
return false unless city_id
35+
return true if tag_names && tag_names.size == 0
36+
raise "RemoteTag.host needs to be set" unless host
37+
38+
requests = []
39+
tag_names.each do |tag_name|
40+
requests << Typhoeus::Request.new("#{host}/api/v#{api_version}/tags/#{tag_name}/tagged_items/#{city_id}?item_type=city", 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)
53+
end
54+
end
55+
end
56+
end

app/presenters/v1/city_presenter.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ class Presenters::V1::CityPresenter < ::Presenter
22

33
attr_accessor :city
44

5-
def initialize(item)
6-
@city = item
5+
def initialize(city)
6+
@city = city
77
super(@city)
88
end
99

10-
def to_hash(item = city)
10+
def to_hash(ct = city)
1111
HashWithIndifferentAccess.new(
1212
{
13-
id: city.id,
14-
name: city.name,
15-
state: city.state,
16-
country_name: city.country,
17-
latitude: city.latitude,
18-
longitude: city.longitude,
19-
path: city_path(self.class.version_number, city.id)
13+
id: ct.id,
14+
name: ct.name,
15+
state: ct.state,
16+
country_name: ct.country,
17+
latitude: ct.latitude,
18+
longitude: ct.longitude,
19+
path: city_path(self.class.version_number, ct.id),
20+
tags: ct.tags.map(&:name)
2021
})
2122
end
2223
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
config = YAML.load(File.read("#{Rails.root}/config/tags_service.yml"))[Rails.env]
2+
3+
RemoteTag.host = config['port'] ? "#{config['host']}:#{config['port']}" : "#{config['host']}"
4+
RemoteTag.api_version = "#{config['api_version'] || '2'}"
5+
RemoteTag.max_concurrency = config['max_concurrency'] || 1

config/tags_service.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
development:
2+
host: tags-service-development.herokuapp.com
3+
port: 80
4+
api_version: 2
5+
max_concurrency: 4
6+
7+
test:
8+
host: tags-service-development.herokuapp.com
9+
port: 80
10+
api_version: 2
11+
12+
production:
13+
host: tags-service-development.herokuapp.com
14+
port: 80
15+
api_version: 2
16+
max_concurrency: 10

0 commit comments

Comments
 (0)