mainstreet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8bd7d3041d913f03949353a816c98876686219a
4
+ data.tar.gz: 482de1d807c5500f1df314de5bef2ac073a97057
5
+ SHA512:
6
+ metadata.gz: b883a7d67ba49e589243728e6b310cc52541377deafa5a62c2caeba3889a1ed76ae8c1c1efa37d043a47f874401109ff39151ce03971b78fabc30f5e3ece351d
7
+ data.tar.gz: f5071479677b0dc600347bba39a19b3c1eab1fb9505e84b5064728ae7ca541571263982665da290f008d679aac6cd1bdff7812499f3d61c602e3e83fa70b7413
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /test/fixtures/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mainstreet.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrew Kane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # MainStreet
2
+
3
+ A standard US address model for Rails
4
+
5
+ You get:
6
+
7
+ - geocoding
8
+ - standardization
9
+ - verification
10
+
11
+ ## How It Works
12
+
13
+ [todo]
14
+
15
+ ### Verification
16
+
17
+ MainStreet performs ZIP code verification by default.
18
+
19
+ ```ruby
20
+ address = Address.new(street: "400 bryant st", zip_code: "94108")
21
+ address.valid?
22
+ # false
23
+ address.errors.full_messages
24
+ # ["Did you mean 94107?"]
25
+ ```
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application’s Gemfile:
30
+
31
+ ```ruby
32
+ gem 'mainstreet'
33
+ ```
34
+
35
+ To create a new address model, run:
36
+
37
+ ```sh
38
+ rails g mainstreet:address
39
+ ```
40
+
41
+ This creates an `Address` model with:
42
+
43
+ - street
44
+ - street2
45
+ - city
46
+ - state
47
+ - zip_code
48
+ - latitude
49
+ - longitude
50
+ - verification_info
51
+ - original_attributes
52
+
53
+ To add to an existing model:
54
+
55
+ 1. Use `alias_attribute` to map existing field names
56
+ 2. Add new fields like `verification_info` and `original_attributes`
57
+ 3. Add `acts_as_address` to your model
58
+
59
+ ## Address Verification
60
+
61
+ MainStreet does ZIP code verification by default.
62
+
63
+ For complete address verification, sign up for a [SmartyStreets](https://smartystreets.com/features) account. The free plan supports 250 lookups per month.
64
+
65
+ Set:
66
+
67
+ ```ruby
68
+ ENV["SMARTY_STREETS_AUTH_ID"] = "auth-id"
69
+ ENV["SMARTY_STREETS_AUTH_TOKEN"] = "auth-token"
70
+ ```
71
+
72
+ To test it, run:
73
+
74
+ ```ruby
75
+ address = Address.new(street: "122 Mast Rd", zip_code: "03861")
76
+ address.valid? # should get false
77
+ ```
78
+
79
+ ## Contributing
80
+
81
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
82
+
83
+ - [Report bugs](https://github.com/ankane/mainstreet/issues)
84
+ - Fix bugs and [submit pull requests](https://github.com/ankane/mainstreet/pulls)
85
+ - Write, clarify, or fix documentation
86
+ - Suggest or add new features
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task default: :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -0,0 +1,10 @@
1
+ require "rails/generators"
2
+
3
+ module Mainstreet
4
+ class AddressGenerator < Rails::Generators::Base
5
+ def boom
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"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Mainstreet
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mainstreet.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "mainstreet/version"
2
+ require "geocoder"
3
+
4
+ module Mainstreet
5
+ class << self
6
+ attr_writer :lookup
7
+
8
+ def lookup
9
+ @lookup ||= begin
10
+ if ENV["SMARTY_STREETS_AUTH_ID"] && ENV["SMARTY_STREETS_AUTH_TOKEN"]
11
+ Geocoder.config[:smarty_streets] ||= {api_key: [ENV["SMARTY_STREETS_AUTH_ID"], ENV["SMARTY_STREETS_AUTH_TOKEN"]]}
12
+ end
13
+ Geocoder.config[:smarty_streets] ? :smarty_streets : nil
14
+ end
15
+ end
16
+ end
17
+
18
+ module Model
19
+ def acts_as_address(_options = {})
20
+ class_eval do
21
+ serialize :original_attributes
22
+ serialize :verification_info
23
+
24
+ validate :verify_address, if: -> { address_fields_changed? }
25
+ before_save :standardize_address, if: -> { address_fields_changed? }
26
+
27
+ 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")
38
+ end
39
+ end
40
+
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}?")
44
+ end
45
+ else
46
+ errors.add(:base, "Address could not be confirmed")
47
+ end
48
+ errors.full_messages
49
+ end
50
+
51
+ def standardize_address
52
+ result = @verification_result
53
+ if result
54
+ info = result.data
55
+ self.original_attributes = attributes.slice(*address_fields)
56
+ self.verification_info = result.data.to_hash
57
+ self.street =
58
+ if result.respond_to?(:delivery_line_1)
59
+ result.delivery_line_1
60
+ else
61
+ result.formatted_address.split(",").first
62
+ end
63
+ self.street2 = nil
64
+ self.city = result.city
65
+ self.state = result.state_code
66
+ self.zip_code = result.postal_code
67
+ self.latitude = result.latitude
68
+ self.longitude = result.longitude
69
+ end
70
+ true
71
+ end
72
+
73
+ def address_fields_changed?
74
+ address_fields.any? { |f| send("#{f}_changed?") }
75
+ end
76
+
77
+ def address_fields
78
+ attributes.keys & %w(street street2 city state zip_code)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ ActiveRecord::Base.send(:extend, Mainstreet::Model) if defined?(ActiveRecord)
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mainstreet/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mainstreet"
8
+ spec.version = Mainstreet::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["andrew@chartkick.com"]
11
+
12
+ spec.summary = "A standard US address model for Rails"
13
+ spec.description = "A standard US address model for Rails"
14
+ spec.homepage = "https://github.com/ankane/mainstreet"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "geocoder"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest"
27
+ spec.add_development_dependency "activerecord"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "smartystreets"
30
+ spec.add_development_dependency "vcr"
31
+ spec.add_development_dependency "webmock"
32
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mainstreet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: geocoder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
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'
97
+ - !ruby/object:Gem::Dependency
98
+ name: smartystreets
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: A standard US address model for Rails
140
+ email:
141
+ - andrew@chartkick.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - LICENSE.txt
149
+ - README.md
150
+ - Rakefile
151
+ - lib/generators/mainstreet/address_generator.rb
152
+ - lib/mainstreet.rb
153
+ - lib/mainstreet/version.rb
154
+ - mainstreet.gemspec
155
+ homepage: https://github.com/ankane/mainstreet
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.4.5
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: A standard US address model for Rails
179
+ test_files: []
180
+ has_rdoc: