mainstreet 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/LICENSE.txt +1 -1
- data/README.md +30 -12
- data/lib/mainstreet/address_verifier.rb +15 -6
- data/lib/mainstreet/model.rb +3 -2
- data/lib/mainstreet/version.rb +1 -1
- metadata +19 -6
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: d74ee9bd034da5023b1d16a19814f8e921ac542f2cff1b209e2fbdc662d2a1d7
         | 
| 4 | 
            +
              data.tar.gz: 679a9cdfe1d892456372e9e422e0db93fa679fe7298c7a052a1e944a298792c3
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: d70178c18422b36a12febca14d16a1907052bd30923f64625491607c03fb82822de72c980d3e032ccfa3336eae0487f3526e2f0c071f86b073e7ecf2da92fb71
         | 
| 7 | 
            +
              data.tar.gz: 25ba36dd066fc89578ddee0df29d29e70da4884f8eca1657acecec7608a418a40bb9ab34aa6538b2005d1848d7227ed8484994d70dfcc0c502dc3fbdfa66dee1
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/LICENSE.txt
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -4,6 +4,8 @@ Address verification for Ruby and Rails | |
| 4 4 |  | 
| 5 5 | 
             
            :earth_americas: Supports international addresses
         | 
| 6 6 |  | 
| 7 | 
            +
            [](https://travis-ci.org/ankane/mainstreet)
         | 
| 8 | 
            +
             | 
| 7 9 | 
             
            ## Installation
         | 
| 8 10 |  | 
| 9 11 | 
             
            Add this line to your application’s Gemfile:
         | 
| @@ -60,7 +62,15 @@ class User < ApplicationRecord | |
| 60 62 | 
             
            end
         | 
| 61 63 | 
             
            ```
         | 
| 62 64 |  | 
| 63 | 
            -
             | 
| 65 | 
            +
            The order should be the same as if you were to write the address out.
         | 
| 66 | 
            +
             | 
| 67 | 
            +
            For performance, the address is only verified if at least one of the fields changes. Set your own condition with:
         | 
| 68 | 
            +
             | 
| 69 | 
            +
            ```ruby
         | 
| 70 | 
            +
            class User < ApplicationRecord
         | 
| 71 | 
            +
              validates_address if: -> { something_changed? }, ...
         | 
| 72 | 
            +
            end
         | 
| 73 | 
            +
            ```
         | 
| 64 74 |  | 
| 65 75 | 
             
            Geocode the address with:
         | 
| 66 76 |  | 
| @@ -96,17 +106,11 @@ MainStreet::AddressVerifier.new(address, country: "France") | |
| 96 106 |  | 
| 97 107 | 
             
            Here’s the list of [supported countries](https://smartystreets.com/docs/cloud/international-street-api#countries). You can pass the name, ISO-3, ISO-2, or ISO-N code (like `France`, `FRA`, `FR`, or `250`).
         | 
| 98 108 |  | 
| 99 | 
            -
            **Note:** You’ll also need to use a fork of Geocoder until [this PR](https://github.com/alexreisner/geocoder/pull/1367) is merged.
         | 
| 100 | 
            -
             | 
| 101 | 
            -
            ```ruby
         | 
| 102 | 
            -
            gem 'geocoder', github: 'ankane/geocoder', branch: 'smarty_streets_international'
         | 
| 103 | 
            -
            ```
         | 
| 104 | 
            -
             | 
| 105 109 | 
             
            For Active Record, use:
         | 
| 106 110 |  | 
| 107 111 | 
             
            ```ruby
         | 
| 108 112 | 
             
            class User < ApplicationRecord
         | 
| 109 | 
            -
              validates_address country: "France"
         | 
| 113 | 
            +
              validates_address country: "France", ...
         | 
| 110 114 | 
             
            end
         | 
| 111 115 | 
             
            ```
         | 
| 112 116 |  | 
| @@ -114,17 +118,31 @@ Or use a proc to make it dynamic | |
| 114 118 |  | 
| 115 119 | 
             
            ```ruby
         | 
| 116 120 | 
             
            class User < ApplicationRecord
         | 
| 117 | 
            -
              validates_address country: -> { country }
         | 
| 121 | 
            +
              validates_address country: -> { country }, ...
         | 
| 118 122 | 
             
            end
         | 
| 119 123 | 
             
            ```
         | 
| 120 124 |  | 
| 121 | 
            -
            ##  | 
| 125 | 
            +
            ## Internationalization (i18n)
         | 
| 126 | 
            +
             | 
| 127 | 
            +
            You can customize error messages with the [i18n](https://github.com/ruby-i18n/i18n) gem. In Rails, add to the appropriate `config/locales` file:
         | 
| 128 | 
            +
             | 
| 129 | 
            +
            ```yml
         | 
| 130 | 
            +
            en:
         | 
| 131 | 
            +
              mainstreet:
         | 
| 132 | 
            +
                errors:
         | 
| 133 | 
            +
                  messages:
         | 
| 134 | 
            +
                    unconfirmed: Address can't be confirmed
         | 
| 135 | 
            +
                    apt_unconfirmed: Apartment or suite can't be confirmed
         | 
| 136 | 
            +
                    apt_missing: Apartment or suite is missing
         | 
| 137 | 
            +
            ```
         | 
| 138 | 
            +
             | 
| 139 | 
            +
            ## Data Protection
         | 
| 122 140 |  | 
| 123 | 
            -
            We recommend encrypting  | 
| 141 | 
            +
            We recommend encrypting street information and postal code (at the very least) for user addresses. [Lockbox](https://github.com/ankane/lockbox) is great for this. Check out [this article](https://ankane.org/sensitive-data-rails) for more details.
         | 
| 124 142 |  | 
| 125 143 | 
             
            ```ruby
         | 
| 126 144 | 
             
            class User < ApplicationRecord
         | 
| 127 | 
            -
               | 
| 145 | 
            +
              encrypts :street, :postal_code
         | 
| 128 146 | 
             
            end
         | 
| 129 147 | 
             
            ```
         | 
| 130 148 |  | 
| @@ -1,8 +1,9 @@ | |
| 1 1 | 
             
            module MainStreet
         | 
| 2 2 | 
             
              class AddressVerifier
         | 
| 3 | 
            -
                def initialize(address, country: nil)
         | 
| 3 | 
            +
                def initialize(address, country: nil, locale: nil)
         | 
| 4 4 | 
             
                  @address = address
         | 
| 5 5 | 
             
                  @country = country
         | 
| 6 | 
            +
                  @locale = locale
         | 
| 6 7 | 
             
                end
         | 
| 7 8 |  | 
| 8 9 | 
             
                def success?
         | 
| @@ -11,7 +12,7 @@ module MainStreet | |
| 11 12 |  | 
| 12 13 | 
             
                def failure_message
         | 
| 13 14 | 
             
                  if !result
         | 
| 14 | 
            -
                    "Address can't be confirmed"
         | 
| 15 | 
            +
                    message :unconfirmed, "Address can't be confirmed"
         | 
| 15 16 | 
             
                  elsif result.respond_to?(:analysis)
         | 
| 16 17 | 
             
                    analysis = result.analysis
         | 
| 17 18 |  | 
| @@ -20,7 +21,7 @@ module MainStreet | |
| 20 21 | 
             
                      when "Verified"
         | 
| 21 22 | 
             
                        nil # success!!
         | 
| 22 23 | 
             
                      when "Ambiguous", "Partial", "None"
         | 
| 23 | 
            -
                        "Address can't be confirmed"
         | 
| 24 | 
            +
                        message :unconfirmed, "Address can't be confirmed"
         | 
| 24 25 | 
             
                      else
         | 
| 25 26 | 
             
                        raise "Unknown verification_status"
         | 
| 26 27 | 
             
                      end
         | 
| @@ -29,11 +30,11 @@ module MainStreet | |
| 29 30 | 
             
                      when "Y"
         | 
| 30 31 | 
             
                        nil # success!!
         | 
| 31 32 | 
             
                      when "N"
         | 
| 32 | 
            -
                        "Address can't be confirmed"
         | 
| 33 | 
            +
                        message :unconfirmed, "Address can't be confirmed"
         | 
| 33 34 | 
             
                      when "S"
         | 
| 34 | 
            -
                        "Apartment or suite can't be confirmed"
         | 
| 35 | 
            +
                        message :apt_unconfirmed, "Apartment or suite can't be confirmed"
         | 
| 35 36 | 
             
                      when "D"
         | 
| 36 | 
            -
                        "Apartment or suite is missing"
         | 
| 37 | 
            +
                        message :apt_missing, "Apartment or suite is missing"
         | 
| 37 38 | 
             
                      else
         | 
| 38 39 | 
             
                        raise "Unknown dpv_match_code"
         | 
| 39 40 | 
             
                      end
         | 
| @@ -66,5 +67,13 @@ module MainStreet | |
| 66 67 | 
             
                def lookup
         | 
| 67 68 | 
             
                  ENV["SMARTY_STREETS_AUTH_ID"] ? :smarty_streets : nil
         | 
| 68 69 | 
             
                end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                def message(key, default)
         | 
| 72 | 
            +
                  if defined?(I18n)
         | 
| 73 | 
            +
                    I18n.t(key, scope: [:mainstreet, :errors, :messages], locale: @locale, default: default)
         | 
| 74 | 
            +
                  else
         | 
| 75 | 
            +
                    default
         | 
| 76 | 
            +
                  end
         | 
| 77 | 
            +
                end
         | 
| 69 78 | 
             
              end
         | 
| 70 79 | 
             
            end
         | 
    
        data/lib/mainstreet/model.rb
    CHANGED
    
    | @@ -1,12 +1,13 @@ | |
| 1 1 | 
             
            module MainStreet
         | 
| 2 2 | 
             
              module Model
         | 
| 3 | 
            -
                def validates_address(fields:, geocode: false, country: nil)
         | 
| 3 | 
            +
                def validates_address(fields:, geocode: false, country: nil, **options)
         | 
| 4 4 | 
             
                  fields = Array(fields.map(&:to_s))
         | 
| 5 5 | 
             
                  geocode_options = {latitude: :latitude, longitude: :longitude}
         | 
| 6 6 | 
             
                  geocode_options = geocode_options.merge(geocode) if geocode.is_a?(Hash)
         | 
| 7 | 
            +
                  options[:if] ||= -> { fields.any? { |f| changes.key?(f.to_s) } } unless options[:unless]
         | 
| 7 8 |  | 
| 8 9 | 
             
                  class_eval do
         | 
| 9 | 
            -
                    validate :verify_address,  | 
| 10 | 
            +
                    validate :verify_address, **options
         | 
| 10 11 |  | 
| 11 12 | 
             
                    define_method :verify_address do
         | 
| 12 13 | 
             
                      address = fields.map { |v| send(v).presence }.compact.join(", ")
         | 
    
        data/lib/mainstreet/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: mainstreet
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.2. | 
| 4 | 
            +
              version: 0.2.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Andrew Kane
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date:  | 
| 11 | 
            +
            date: 2019-10-04 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: geocoder
         | 
| @@ -16,14 +16,14 @@ dependencies: | |
| 16 16 | 
             
                requirements:
         | 
| 17 17 | 
             
                - - ">="
         | 
| 18 18 | 
             
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            -
                    version:  | 
| 19 | 
            +
                    version: 1.5.1
         | 
| 20 20 | 
             
              type: :runtime
         | 
| 21 21 | 
             
              prerelease: false
         | 
| 22 22 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 23 | 
             
                requirements:
         | 
| 24 24 | 
             
                - - ">="
         | 
| 25 25 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            -
                    version:  | 
| 26 | 
            +
                    version: 1.5.1
         | 
| 27 27 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 28 28 | 
             
              name: bundler
         | 
| 29 29 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -80,6 +80,20 @@ dependencies: | |
| 80 80 | 
             
                - - ">="
         | 
| 81 81 | 
             
                  - !ruby/object:Gem::Version
         | 
| 82 82 | 
             
                    version: '0'
         | 
| 83 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 84 | 
            +
              name: i18n
         | 
| 85 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 86 | 
            +
                requirements:
         | 
| 87 | 
            +
                - - ">="
         | 
| 88 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 89 | 
            +
                    version: '0'
         | 
| 90 | 
            +
              type: :development
         | 
| 91 | 
            +
              prerelease: false
         | 
| 92 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 93 | 
            +
                requirements:
         | 
| 94 | 
            +
                - - ">="
         | 
| 95 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 96 | 
            +
                    version: '0'
         | 
| 83 97 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 84 98 | 
             
              name: sqlite3
         | 
| 85 99 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -154,8 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 154 168 | 
             
                - !ruby/object:Gem::Version
         | 
| 155 169 | 
             
                  version: '0'
         | 
| 156 170 | 
             
            requirements: []
         | 
| 157 | 
            -
             | 
| 158 | 
            -
            rubygems_version: 2.7.6
         | 
| 171 | 
            +
            rubygems_version: 3.0.3
         | 
| 159 172 | 
             
            signing_key: 
         | 
| 160 173 | 
             
            specification_version: 4
         | 
| 161 174 | 
             
            summary: Address verification for Ruby and Rails
         |