Class: GdsApi::Imminence

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/imminence.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #initialize, #url_for_slug

Constructor Details

This class inherits a constructor from GdsApi::Base

Class Method Details

.extract_address_hash(place_hash) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gds_api/imminence.rb', line 59

def self.extract_address_hash(place_hash)
  address_fields = [
    place_hash["address1"],
    place_hash["address2"],
  ].reject { |a| a.nil? || a == "" }
  { "address" => address_fields.map(&:strip).join(", ") }
end

.extract_location_hash(location) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gds_api/imminence.rb', line 44

def self.extract_location_hash(location)
  # Deal with all known location formats:
  #   Old style: [latitude, longitude]; empty array for no location
  #   New style: hash with keys "longitude", "latitude"; nil for no location
  case location
  when Array
    { "latitude" => location[0], "longitude" => location[1] }
  when Hash
    location
  when nil
    { "latitude" => nil, "longitude" => nil }
  end
end

.parse_place_hash(place_hash) ⇒ Object



22
23
24
25
26
27
# File 'lib/gds_api/imminence.rb', line 22

def self.parse_place_hash(place_hash)
  location = self.extract_location_hash(place_hash["location"])
  address = self.extract_address_hash(place_hash)

  place_hash.merge(location).merge(address)
end

Instance Method Details

#api_url(type, params) ⇒ Object



4
5
6
7
8
# File 'lib/gds_api/imminence.rb', line 4

def api_url(type, params)
  vals = %i[limit lat lng postcode].select { |p| params.include? p }
  querystring = URI.encode_www_form(vals.map { |p| [p, params[p]] })
  "#{@endpoint}/places/#{type}.json?#{querystring}"
end

#areas_for_postcode(postcode) ⇒ Object



33
34
35
36
# File 'lib/gds_api/imminence.rb', line 33

def areas_for_postcode(postcode)
  url = "#{@endpoint}/areas/#{ERB::Util.url_encode(postcode)}.json"
  get_json(url)
end

#areas_for_type(type) ⇒ Object



38
39
40
41
# File 'lib/gds_api/imminence.rb', line 38

def areas_for_type(type)
  url = "#{@endpoint}/areas/#{type}.json"
  get_json(url)
end

#places(type, lat, lon, limit = 5) ⇒ Object



10
11
12
13
14
# File 'lib/gds_api/imminence.rb', line 10

def places(type, lat, lon, limit = 5)
  url = api_url(type, lat: lat, lng: lon, limit: limit)
  places = get_json(url) || []
  places.map { |p| self.class.parse_place_hash(p) }
end

#places_for_postcode(type, postcode, limit = 5) ⇒ Object



16
17
18
19
20
# File 'lib/gds_api/imminence.rb', line 16

def places_for_postcode(type, postcode, limit = 5)
  url = api_url(type, postcode: postcode, limit: limit)
  places = get_json(url) || []
  places.map { |p| self.class.parse_place_hash(p) }
end

#places_kml(type) ⇒ Object



29
30
31
# File 'lib/gds_api/imminence.rb', line 29

def places_kml(type)
  get_raw("#{@endpoint}/places/#{type}.kml").body
end