File: geocodio.rb

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (78 lines) | stat: -rw-r--r-- 1,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
require 'geocoder/results/base'

module Geocoder::Result
  class Geocodio < Base
    def number
      address_components["number"]
    end

    def street
      address_components["street"]
    end

    def suffix
      address_components["suffix"]
    end

    def street_address
      [number, address_components["formatted_street"]].compact.join(' ')
    end

    def state
      address_components["state"]
    end
    alias_method :state_code, :state

    def zip
      # Postal code is not returned for Canada geocode results
      address_components["zip"] || ""
    end
    alias_method :postal_code, :zip

    def country
      # Geocodio supports US and Canada, however they don't return the full
      # country name.

      if country_code == "CA"
        "Canada"
      else
        "United States"
      end
    end

    def country_code
      address_components['country']
    end

    def city
      address_components["city"]
    end

    def postdirectional
      address_components["postdirectional"]
    end

    def location
      @data['location']
    end

    def coordinates
      ['lat', 'lng'].map{ |i| location[i].to_f } if location
    end

    def accuracy
      @data['accuracy'].to_f if @data.key?('accuracy')
    end

    def formatted_address(format = :full)
      @data['formatted_address']
    end
    alias_method :address, :formatted_address

    private

    def address_components
      @data['address_components'] || {}
    end
  end
end