Class: GeoTagger

Inherits:
Base
  • Object
show all
Defined in:
lib/whos_using_what/data_gatherers/geo_tagger.rb

Instance Attribute Summary

Attributes inherited from Base

#set_paths

Instance Method Summary collapse

Methods inherited from Base

set_paths

Constructor Details

#initialize(log) ⇒ GeoTagger

Returns a new instance of GeoTagger.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/whos_using_what/data_gatherers/geo_tagger.rb', line 11

def initialize log
  @log = log
  @mongo_client = MongoHelper.get_mongo_connection
  @companies_coll = @mongo_client['companies']
  @coords_coll = @mongo_client['coordinates']

  @locations_client = GoogleLocationsClient.new

  #todo this code should possibly be moved somewhere else for clarity / encapsulation
  @coords_coll.remove({"loc" => nil})

  @coords_coll.ensure_index([["loc", Mongo::GEO2D]])
  @companies_coll.ensure_index([["loc", Mongo::GEO2D]])

end

Instance Method Details

#insert_new_zip_entry(zip_code) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/whos_using_what/data_gatherers/geo_tagger.rb', line 44

def insert_new_zip_entry zip_code

  if (zip_acceptance_predicate (zip_code))

    begin
      #todo figure how to do this now that we are using instance variables instead of class variables
      process_zip_closure = lambda {

        resp_map = @locations_client.api_get_google_location_data zip_code

        doc = {}


        doc[:zip] = zip_code

        coords = @locations_client.get_coords_from_google_location_resp_helper resp_map
        if coords[0] && coords[1]
          doc[:loc] = {"lon" => coords[0], "lat" => coords[1]}
        end


        keys_arr = ["AddressDetails", "Country", "AdministrativeArea", "Locality", "LocalityName"]
        MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :city, doc

        keys_arr = ["AddressDetails", "Country", "AdministrativeArea", "AdministrativeAreaName"]
        MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :state, doc

        keys_arr = ["AddressDetails", "Country", "CountryNameCode"]
        MapDataExtractionUtil.safe_extract_helper keys_arr, resp_map, :country, doc


        if (doc.size > 1 && doc[:country] == "US")
          coll = @coords_coll.find(zip: zip_code).to_a
        end

        if coll && coll.size < 1


          @coords_coll.insert(doc)

        end
      }

      process_zip_closure.call
    rescue Exception => e
      puts e.message
      puts e.backtrace

    end
  end

end

#load_geolocations_into_dbObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/whos_using_what/data_gatherers/geo_tagger.rb', line 98

def load_geolocations_into_db

  companies = @companies_coll.find()
  companies_arr = companies.to_a
  companies_arr.each do |company|

    if !company
      next
    end


    keys_arr = ['locations', 'values']
    locations = MapDataExtractionUtil.safe_extract_helper keys_arr, company, :nil, nil

    if !locations
      next
    end
    locations.each do |location|

      zip_code = location['address']['postalCode']

      #strip off anything past 5 characters, as we only want main part of zip code
      if !zip_code || zip_code.size < 5
        next
      end

      zip_code = zip_code[0...5]

      insert_new_zip_entry zip_code
    end
  end
end

#update_companies_with_latitude_longitudeObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/whos_using_what/data_gatherers/geo_tagger.rb', line 131

def update_companies_with_latitude_longitude

  puts "beginning updating of companies with latitude and longitude data"

  @companies_coll.find('loc' => {
      '$exists' => false
  }).to_a.each do |company|


    locations = MapDataExtractionUtil.safe_extract_helper ["locations", "values"], company, nil, nil

    if locations

      locations.each do |location|

        zip = MapDataExtractionUtil.safe_extract_helper ["address", "postalCode"], location, nil, nil

        if zip

          zip = zip[0..4]
          coords = @coords_coll.find_one({:zip => zip})
          if coords != nil

            company["loc"] = coords["loc"]

            puts "updating  geo-coordinates for company with id: " << company["_id"].to_s
            @companies_coll.update({"_id" => company["_id"]}, company)

          end
        end
      end
    end
  end
end

#zip_acceptance_predicate(zip_code) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/whos_using_what/data_gatherers/geo_tagger.rb', line 28

def zip_acceptance_predicate zip_code

  if !zip_code
    return false
  end

  accept = true

  if !zip_code.start_with? ("9")
    accept = false
  end

  accept

end