File: smarty_streets.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 (142 lines) | stat: -rw-r--r-- 2,808 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'geocoder/lookups/base'

module Geocoder::Result
  class SmartyStreets < Base
    def coordinates
      result = %w(latitude longitude).map do |i|
        zipcode_endpoint? ? zipcodes.first[i] : metadata[i]
      end

      if result.compact.empty?
        nil
      else
        result
      end
    end

    def address
      parts =
        if international_endpoint?
          (1..12).map { |i| @data["address#{i}"] }
        else
          [
            delivery_line_1,
            delivery_line_2,
            last_line
          ]
        end
      parts.select{ |i| i.to_s != "" }.join(" ")
    end

    def state
      if international_endpoint?
        components['administrative_area']
      elsif zipcode_endpoint?
        city_states.first['state']
      else
        components['state_abbreviation']
      end
    end

    def state_code
      if international_endpoint?
        components['administrative_area']
      elsif zipcode_endpoint?
        city_states.first['state_abbreviation']
      else
        components['state_abbreviation']
      end
    end

    def country
      international_endpoint? ?
        components['country_iso_3'] :
        "United States"
    end

    def country_code
      international_endpoint? ?
        components['country_iso_3'] :
        "US"
    end

    ## Extra methods not in base.rb ------------------------

    def street
      international_endpoint? ?
        components['thoroughfare_name'] :
        components['street_name']
    end

    def city
      if international_endpoint?
        components['locality']
      elsif zipcode_endpoint?
        city_states.first['city']
      else
        components['city_name']
      end
    end

    def zipcode
      if international_endpoint?
        components['postal_code']
      elsif zipcode_endpoint?
        zipcodes.first['zipcode']
      else
        components['zipcode']
      end
    end
    alias_method :postal_code, :zipcode

    def zip4
      components['plus4_code']
    end
    alias_method :postal_code_extended, :zip4

    def fips
      zipcode_endpoint? ?
        zipcodes.first['county_fips'] :
        metadata['county_fips']
    end

    def zipcode_endpoint?
      zipcodes.any?
    end

    def international_endpoint?
      !@data['address1'].nil?
    end

    [
      :delivery_line_1,
      :delivery_line_2,
      :last_line,
      :delivery_point_barcode,
      :addressee
    ].each do |m|
      define_method(m) do
        @data[m.to_s] || ''
      end
    end

    [
      :components,
      :metadata,
      :analysis
    ].each do |m|
      define_method(m) do
        @data[m.to_s] || {}
      end
    end

    [
      :city_states,
      :zipcodes
    ].each do |m|
      define_method(m) do
        @data[m.to_s] || []
      end
    end
  end
end