adwords_location 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a07d9ade4c72aef5c7e48a4ea85fd460fe1be206
4
+ data.tar.gz: d6983e9933b3537669d4c5c96b0e5d50e23512c3
5
+ SHA512:
6
+ metadata.gz: f94f0bc590548e8640e8b87fa59864046acea6e8da7e8460965a16223467e0298c3662fa8a883ccc841052866f849916d9712689ee49e5c24bcf0ea68515727e
7
+ data.tar.gz: 43dda2ec4700a84903e5302f94e5fb613c400bd8c14eecd32728da88cf5a1234ee10b83e526aa84f677d8daffb0d8be2acdf555aefaa976535b4023b0d03cc8a
@@ -0,0 +1,121 @@
1
+ require 'csv'
2
+
3
+ module AdWords
4
+
5
+ Location = Struct.new(:criteria_id, :name, :canonical_name, :parent_id, :country_code, :target_type, :status) do
6
+
7
+ def description
8
+ "#{canonical_name} (#{target_type})"
9
+ end
10
+
11
+ def self.all
12
+ data.drop(1).map do |row|
13
+ AdWords::Location.new(row[0].to_i, row[1], row[2], row[3].to_i, row[4], row[5], row[6])
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def self.data
20
+ adwords_location_file = 'adwords_api_location_criteria_2014-05-21.csv'
21
+ @data ||= CSV.read(File.join(File.dirname(__FILE__), '..', 'data', adwords_location_file))
22
+ end
23
+
24
+ end
25
+
26
+ City2DMA = Struct.new(:city, :criteria_id, :state, :dma_region, :dma_region_code) do
27
+
28
+ def ==(other)
29
+ self.criteria_id == other.criteria_id && self.dma_region_code == other.dma_region_code
30
+ end
31
+
32
+ def description
33
+ "#{dma_region}, #{state}"
34
+ end
35
+
36
+ def self.all
37
+ data.drop(1).map do |row|
38
+ AdWords::City2DMA.new(row[0], row[1].to_i, row[2], row[3], row[4].to_i)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def self.data
45
+ adwords_dma_file = 'adwords_api_cities_dma_regions_2014-05-21.csv'
46
+ @data ||= CSV.read(File.join(File.dirname(__FILE__), '..', 'data', adwords_dma_file))
47
+ end
48
+
49
+ end
50
+
51
+ class LocationDb
52
+
53
+ attr_reader :location_records, :location_records_by_name, :city_to_dma_records
54
+
55
+ def initialize
56
+ @location_records = {}
57
+ @location_records_by_name = {}
58
+ records = Location.all
59
+ records.each do |r|
60
+ @location_records[r.criteria_id] = r
61
+ @location_records_by_name[r.name] = r
62
+ end
63
+ @location_records.freeze
64
+ @location_records_by_name.freeze
65
+
66
+ @city_to_dma_records = {}
67
+ cities = City2DMA.all
68
+ cities.each do |r|
69
+ @city_to_dma_records[r.criteria_id] ||= []
70
+ @city_to_dma_records[r.criteria_id] << r unless @city_to_dma_records[r.criteria_id].include? r
71
+ end
72
+ @city_to_dma_records.freeze
73
+ end
74
+
75
+ def criteria_ids
76
+ @location_records.keys
77
+ end
78
+
79
+ def find(criteria_id)
80
+ @location_records[criteria_id.to_i]
81
+ end
82
+
83
+ def find_by_name(name)
84
+ @location_records_by_name[name]
85
+ end
86
+
87
+ def path(criteria_id)
88
+ path = []
89
+ location = find criteria_id
90
+ while location do
91
+ path << location.criteria_id
92
+ location = find location.parent_id
93
+ end
94
+ path
95
+ end
96
+
97
+ def city_dmas(criteria_id)
98
+ @city_to_dma_records[criteria_id.to_i]
99
+ end
100
+
101
+ def lookup_table
102
+ ltable = {}
103
+ criteria_ids.map do |id|
104
+ path = path(id)
105
+ city_dmas = (city_dmas(id) || [])
106
+ dma_criteria_ids = city_dmas.map { |dma| find_by_name(dma.dma_region)[:criteria_id]}
107
+ ltable[id] = path + dma_criteria_ids
108
+ end
109
+ ltable
110
+ end
111
+
112
+ def output_lookup_table
113
+ lookup_table.each do |id, values|
114
+ puts values.join(',')
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
@@ -0,0 +1 @@
1
+ require_relative 'adwords_location/location'