Class: WeatherSage::Census::Geocoder

Inherits:
Object
  • Object
show all
Defined in:
lib/weather-sage/census/geocoder.rb

Overview

Wrapper around Census geocoder API.

Constant Summary collapse

URL =

URL endpoint for Census Geocoder API.

'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress'
PARAMS =

Static parameters for geocoder requests.

Source: geocoding.geo.census.gov/geocoder/Geocoding_Services_API.pdf

{
  returntype: 'locations',
  benchmark:  'Public_AR_Current',
  format:     'json',
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Geocoder

Create new Geocoder instance.



24
25
26
# File 'lib/weather-sage/census/geocoder.rb', line 24

def initialize(ctx)
  @ctx = ctx
end

Instance Method Details

#run(s) ⇒ Object

Geocode given address string s and return an array of Match objects.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/weather-sage/census/geocoder.rb', line 32

def run(s)
  # exec request
  data = @ctx.cache.get(URL, PARAMS.merge({
    address: s,
  }))

  # log data
  @ctx.log.debug('Geocoder#run') do
    'data = %p' % [data]
  end

  # map matches and return result
  data['result']['addressMatches'].map { |row|
    ::WeatherSage::Census::Match.new(@ctx, row)
  }
end