mainstreet 0.0.1 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8bd7d3041d913f03949353a816c98876686219a
4
- data.tar.gz: 482de1d807c5500f1df314de5bef2ac073a97057
3
+ metadata.gz: d572831c5d0c80c6580ac885edf6c1ea8137589d
4
+ data.tar.gz: 3f50e8e84d9edad8918aa83e9b87ca6f450bc196
5
5
  SHA512:
6
- metadata.gz: b883a7d67ba49e589243728e6b310cc52541377deafa5a62c2caeba3889a1ed76ae8c1c1efa37d043a47f874401109ff39151ce03971b78fabc30f5e3ece351d
7
- data.tar.gz: f5071479677b0dc600347bba39a19b3c1eab1fb9505e84b5064728ae7ca541571263982665da290f008d679aac6cd1bdff7812499f3d61c602e3e83fa70b7413
6
+ metadata.gz: f9b58c621595ee590c924beb58aa5f4c6402c9df339fc62fd78ea3789ca8ec23accc756f5dabc41a5733c42d98271431db6e1eba007d21ed8dbf2c232fe69134
7
+ data.tar.gz: 954950d8881fd942e1f159e540eb1634780bb36350720402df09f6f25326490d35e1f38d85f9dd98aa4e2ec0fcf0fccc1d34b5a7024d9dc1d9438ada00558e15
data/README.md CHANGED
@@ -4,26 +4,41 @@ A standard US address model for Rails
4
4
 
5
5
  You get:
6
6
 
7
- - geocoding
8
- - standardization
9
7
  - verification
8
+ - standardization
9
+ - geocoding
10
10
 
11
11
  ## How It Works
12
12
 
13
- [todo]
13
+ ```ruby
14
+ Address.create!(street: "1 infinite loop", zip_code: "95014")
15
+ ```
16
+
17
+ This creates an address with:
18
+
19
+ - street - `1 Infinite Loop`
20
+ - city - `Cupertino`
21
+ - state - `CA`
22
+ - zip_code - `95014`
23
+ - latitude - `37.33053`
24
+ - longitude - `-122.02887`
25
+ - original_attributes - `{"street"=>"1 infinite loop", "street2"=>nil, "city"=>nil, "state"=>nil, "zip_code"=>"95014"}`
26
+ - verification_info
14
27
 
15
28
  ### Verification
16
29
 
17
- MainStreet performs ZIP code verification by default.
30
+ By default, MainStreet performs ZIP code verification.
18
31
 
19
32
  ```ruby
20
- address = Address.new(street: "400 bryant st", zip_code: "94108")
33
+ address = Address.new(street:"1 infinite loop", zip_code: "95015")
21
34
  address.valid?
22
35
  # false
23
36
  address.errors.full_messages
24
- # ["Did you mean 94107?"]
37
+ # ["Did you mean 95014?"]
25
38
  ```
26
39
 
40
+ For full verification, including unit number, [see below](#full-verification).
41
+
27
42
  ## Installation
28
43
 
29
44
  Add this line to your application’s Gemfile:
@@ -47,20 +62,18 @@ This creates an `Address` model with:
47
62
  - zip_code
48
63
  - latitude
49
64
  - longitude
50
- - verification_info
51
65
  - original_attributes
66
+ - verification_info
52
67
 
53
68
  To add to an existing model:
54
69
 
55
70
  1. Use `alias_attribute` to map existing field names
56
- 2. Add new fields like `verification_info` and `original_attributes`
71
+ 2. Add new fields like `original_attributes` and `verification_info`
57
72
  3. Add `acts_as_address` to your model
58
73
 
59
- ## Address Verification
60
-
61
- MainStreet does ZIP code verification by default.
74
+ ## Full Verification
62
75
 
63
- For complete address verification, sign up for a [SmartyStreets](https://smartystreets.com/features) account. The free plan supports 250 lookups per month.
76
+ [SmartyStreets](https://smartystreets.com/features) is required for full verification. The free plan supports 250 lookups per month.
64
77
 
65
78
  Set:
66
79
 
@@ -73,7 +86,8 @@ To test it, run:
73
86
 
74
87
  ```ruby
75
88
  address = Address.new(street: "122 Mast Rd", zip_code: "03861")
76
- address.valid? # should get false
89
+ address.valid?
90
+ # should get false
77
91
  ```
78
92
 
79
93
  ## Contributing
@@ -4,7 +4,7 @@ module Mainstreet
4
4
  class AddressGenerator < Rails::Generators::Base
5
5
  def boom
6
6
  invoke "model", ["Address", "street:string", "street2:string", "city:string", "state:string", "zip_code:string", "latitude:decimal{15.10}", "longitude:decimal{15.10}", "verification_info:text", "original_attributes:text"], options
7
- insert_into_file "app/models/address.rb", " acts_as_address\n", after: "ActiveRecord::Base\n"
7
+ insert_into_file "app/models/address.rb", " acts_as_address\n", after: "ApplicationRecord\n"
8
8
  end
9
9
  end
10
10
  end
data/lib/mainstreet.rb CHANGED
@@ -21,29 +21,35 @@ module Mainstreet
21
21
  serialize :original_attributes
22
22
  serialize :verification_info
23
23
 
24
+ validates :street, presence: true
24
25
  validate :verify_address, if: -> { address_fields_changed? }
25
26
  before_save :standardize_address, if: -> { address_fields_changed? }
26
27
 
27
28
  def verify_address
28
- @verification_result = Geocoder.search("#{street} #{street2} #{city}, #{state} #{zip_code}", lookup: Mainstreet.lookup).first
29
- if @verification_result
30
- if @verification_result.respond_to?(:analysis)
31
- case @verification_result.analysis["dpv_match_code"]
32
- when "N"
33
- errors.add(:base, "Address could not be confirmed")
34
- when "S"
35
- errors.add(:base, "Apartment or suite could not be confirmed")
36
- when "D"
37
- errors.add(:base, "Apartment or suite is missing")
29
+ if zip_code.blank? && (city.blank? || state.blank?)
30
+ errors.add(:base, "Address can't be confirmed")
31
+ end
32
+ if errors.empty?
33
+ @verification_result = fetch_verification_info
34
+ if @verification_result
35
+ if @verification_result.respond_to?(:analysis)
36
+ case @verification_result.analysis["dpv_match_code"]
37
+ when "N"
38
+ errors.add(:base, "Address can't be confirmed")
39
+ when "S"
40
+ errors.add(:base, "Apartment or suite can't be confirmed")
41
+ when "D"
42
+ errors.add(:base, "Apartment or suite is missing")
43
+ end
38
44
  end
39
- end
40
45
 
41
- correct_zip_code = @verification_result.postal_code
42
- if zip_code != correct_zip_code
43
- errors.add(:base, "Did you mean #{correct_zip_code}?")
46
+ correct_zip_code = @verification_result.postal_code
47
+ if zip_code != correct_zip_code
48
+ errors.add(:base, "Did you mean #{correct_zip_code}?")
49
+ end
50
+ else
51
+ errors.add(:base, "Address can't be confirmed")
44
52
  end
45
- else
46
- errors.add(:base, "Address could not be confirmed")
47
53
  end
48
54
  errors.full_messages
49
55
  end
@@ -70,6 +76,10 @@ module Mainstreet
70
76
  true
71
77
  end
72
78
 
79
+ def fetch_verification_info
80
+ Geocoder.search("#{street} #{street2} #{city}, #{state} #{zip_code}", lookup: Mainstreet.lookup).first
81
+ end
82
+
73
83
  def address_fields_changed?
74
84
  address_fields.any? { |f| send("#{f}_changed?") }
75
85
  end
@@ -1,3 +1,3 @@
1
1
  module Mainstreet
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
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.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-03-15 00:00:00.000000000 Z
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: geocoder
@@ -172,9 +172,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  version: '0'
173
173
  requirements: []
174
174
  rubyforge_project:
175
- rubygems_version: 2.4.5
175
+ rubygems_version: 2.5.1
176
176
  signing_key:
177
177
  specification_version: 4
178
178
  summary: A standard US address model for Rails
179
179
  test_files: []
180
- has_rdoc: