whos_using_what 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,8 @@ require "rest-client"
|
|
6
6
|
|
7
7
|
class GeoTagger
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize log
|
10
|
+
@log = log
|
10
11
|
@mongo_client = MongoHelper.get_mongo_connection
|
11
12
|
@companies_coll = @mongo_client['companies']
|
12
13
|
@coords_coll = @mongo_client['coordinates']
|
@@ -38,89 +39,95 @@ class GeoTagger
|
|
38
39
|
|
39
40
|
end
|
40
41
|
|
42
|
+
def insert_new_zip_entry zip_code
|
41
43
|
|
42
|
-
|
44
|
+
if (zip_acceptance_predicate (zip_code))
|
43
45
|
|
44
|
-
|
46
|
+
begin
|
47
|
+
#todo figure how to do this now that we are using instance variables instead of class variables
|
48
|
+
process_zip_closure = lambda {
|
45
49
|
|
46
|
-
|
47
|
-
next
|
48
|
-
end
|
50
|
+
resp_map = @locations_client.api_get_google_location_data zip_code
|
49
51
|
|
52
|
+
doc = {}
|
50
53
|
|
51
|
-
keys_arr = ['locations', 'values']
|
52
|
-
locations = MapDataExtractionUtil.safe_extract_helper keys_arr, company, :nil, nil
|
53
|
-
|
54
|
-
if !locations
|
55
|
-
next
|
56
|
-
end
|
57
|
-
locations.each do |location|
|
58
54
|
|
59
|
-
|
55
|
+
doc[:zip] = zip_code
|
60
56
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
coords = @locations_client.get_coords_from_google_location_resp_helper resp_map
|
58
|
+
if coords[0] && coords[1]
|
59
|
+
doc[:loc] = {"lon" => coords[0], "lat" => coords[1]}
|
60
|
+
end
|
65
61
|
|
66
|
-
zip_code = zip_code[0...5]
|
67
62
|
|
68
|
-
|
63
|
+
keys_arr = ["AddressDetails", "Country", "AdministrativeArea", "Locality", "LocalityName"]
|
64
|
+
MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :city, doc
|
69
65
|
|
66
|
+
keys_arr = ["AddressDetails", "Country", "AdministrativeArea", "AdministrativeAreaName"]
|
67
|
+
MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :state, doc
|
70
68
|
|
71
|
-
|
72
|
-
|
73
|
-
process_zip_closure = lambda {
|
69
|
+
keys_arr = ["AddressDetails", "Country", "CountryNameCode"]
|
70
|
+
MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :country, doc
|
74
71
|
|
75
|
-
resp_map = @locations_client.api_get_google_location_data zip_code
|
76
72
|
|
77
|
-
|
73
|
+
if (doc.size > 1 && doc[:country] == "US")
|
74
|
+
coll = @coords_coll.find(zip: zip_code).to_a
|
75
|
+
end
|
78
76
|
|
77
|
+
if coll && coll.size < 1
|
79
78
|
|
80
|
-
doc[:zip] = zip_code
|
81
79
|
|
82
|
-
|
83
|
-
if coords[0] && coords[1]
|
84
|
-
doc[:loc] = {"lon" => coords[0], "lat" => coords[1]}
|
85
|
-
end
|
80
|
+
@coords_coll.insert(doc)
|
86
81
|
|
82
|
+
end
|
83
|
+
}
|
87
84
|
|
88
|
-
|
89
|
-
|
85
|
+
process_zip_closure.call
|
86
|
+
rescue Exception => e
|
87
|
+
puts e.message
|
88
|
+
puts e.backtrace
|
90
89
|
|
91
|
-
|
92
|
-
|
90
|
+
end
|
91
|
+
end
|
93
92
|
|
94
|
-
|
95
|
-
MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :country, doc
|
93
|
+
end
|
96
94
|
|
97
95
|
|
98
|
-
|
99
|
-
coll = @coords_coll.find(zip: zip_code).to_a
|
100
|
-
end
|
96
|
+
def load_geolocations_into_db
|
101
97
|
|
102
|
-
|
98
|
+
@companies_coll.find().to_a.each do |company|
|
103
99
|
|
100
|
+
if !company
|
101
|
+
next
|
102
|
+
end
|
104
103
|
|
105
|
-
@coords_coll.insert(doc)
|
106
104
|
|
107
|
-
|
108
|
-
|
105
|
+
keys_arr = ['locations', 'values']
|
106
|
+
locations = MapDataExtractionUtil.safe_extract_helper keys_arr, company, :nil, nil
|
109
107
|
|
108
|
+
if !locations
|
109
|
+
next
|
110
|
+
end
|
111
|
+
locations.each do |location|
|
110
112
|
|
111
|
-
|
112
|
-
rescue Exception => e
|
113
|
-
puts e.message
|
114
|
-
puts e.backtrace
|
113
|
+
zip_code = location['address']['postalCode']
|
115
114
|
|
116
|
-
|
115
|
+
#strip off anything past 5 characters, as we only want main part of zip code
|
116
|
+
if !zip_code || zip_code.size < 5
|
117
|
+
next
|
117
118
|
end
|
119
|
+
|
120
|
+
zip_code = zip_code[0...5]
|
121
|
+
|
122
|
+
insert_new_zip_entry zip_code
|
118
123
|
end
|
119
124
|
end
|
120
125
|
end
|
121
126
|
|
122
127
|
def update_companies_with_latitude_longitude
|
123
128
|
|
129
|
+
@log.info "beginning updating of companies with latitude and longitude data"
|
130
|
+
|
124
131
|
@companies_coll.find().to_a.each do |company|
|
125
132
|
|
126
133
|
|
@@ -1,12 +1,13 @@
|
|
1
1
|
class CompaniesSearcher
|
2
2
|
|
3
|
-
|
3
|
+
#geo_tagger = instance of GeoTagger
|
4
|
+
def initialize geo_tagger
|
4
5
|
|
5
6
|
@mongo_client = MongoHelper.get_mongo_connection
|
6
7
|
@companies_coll = @mongo_client['companies']
|
7
8
|
@coords_coll = @mongo_client['coordinates']
|
8
9
|
|
9
|
-
@geo_tagger =
|
10
|
+
@geo_tagger = geo_tagger
|
10
11
|
|
11
12
|
end
|
12
13
|
|
@@ -18,14 +19,11 @@ class CompaniesSearcher
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def zip_code_search zip_code
|
22
|
+
|
21
23
|
zip_doc = @coords_coll.find_one({:zip => zip_code})
|
22
24
|
if zip_doc == nil
|
23
25
|
|
24
|
-
|
25
|
-
#todo need to figure out how to call this from other class
|
26
|
-
closure = @geo_tagger.process_zip_closure zip_code
|
27
|
-
|
28
|
-
closure.call
|
26
|
+
@geo_tagger.insert_new_zip_entry zip_code
|
29
27
|
|
30
28
|
end
|
31
29
|
zip_doc = @coords_coll.find_one({:zip => zip_code})
|