address_validate 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 934d4c95f0c0ff0a57eb87e4423c1d704454f4de
4
+ data.tar.gz: 475de09953d7c2f267aa8a1acc0f695844d3b0ed
5
+ SHA512:
6
+ metadata.gz: 1b6be3b23a2f87bcd02651216615d33bdc0771bb387b3f93aad72791b207f184240add006883f7286a79d7fc8a6945c5ea6c4cdd6326f1696d1dac60a19d97fd
7
+ data.tar.gz: 80c7fb6ca1c94dadb4982cd1cf429fc0bbb3752cdf5f0de4c4f87e5f7e422360de19550cbfc606097ed9392ab9ad8712cace61cbf1e08840788387b610dc41ee
@@ -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
+ *.DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at marybethlee11@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in address_validate.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 marybethlee
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.
@@ -0,0 +1,85 @@
1
+ # AddressValidate
2
+
3
+ A ruby gem for integrating with the USPS address validation API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'address_validate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install address_validate
20
+
21
+ ## Configuration
22
+
23
+ The USPS address validation api requires a valid username. It will need to be set in order for calls to be made:
24
+
25
+ ```ruby
26
+ # config/initializers/address_validate.rb
27
+ AddressValidate.configure do |config|
28
+ config.environment = :test # or :production
29
+ config.username = 'MY_USPS_USERNAME'
30
+ end
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```ruby
36
+ address_data = { street_address: '34 Berry', zip_5: '11249' }
37
+
38
+ response = AddressValidate::API.validate_address(address_data)
39
+
40
+ response.success? # => true
41
+
42
+ response.data # => { street_address: '34 BERRY ST', city: 'BROOKLYN', state: 'NY', zip_5: '11249' }
43
+
44
+ response.error # => nil
45
+
46
+ response.message # => 'Default address: The address you entered was found but more information is needed...'
47
+ ```
48
+
49
+ If you want to override the default fields for address data, you can do that during configuration
50
+
51
+ ```ruby
52
+ # config/initializers/address_validate.rb
53
+ AddressValidate.configure do |config|
54
+ config.environment = :test # or :production
55
+ config.username = 'MY_USPS_USERNAME'
56
+ config.street_address = :address_1
57
+ config.apartment_number = :address_2
58
+ config.zip_5 = :postal_code
59
+ end
60
+ ```
61
+
62
+ You can then send your address data with your field names to the API, and will receive matching data back.
63
+
64
+ ```ruby
65
+ address_data = { address_1: '34 Berry', postal_code: '11249' }
66
+
67
+ response = AddressValidate::API.validate_address(address_data)
68
+
69
+ response.data # => { address_1: '34 BERRY ST', city: 'BROOKLYN', state: 'NY', postal_code: '11249' }
70
+ ```
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marybethlee/address_validate.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'address_validate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "address_validate"
8
+ spec.version = AddressValidate::VERSION
9
+ spec.authors = ["Mary Lee"]
10
+ spec.email = ["marybethlee11@gmail.com"]
11
+
12
+ spec.summary = %q{USPS address validation api integration}
13
+ spec.description = %q{AddressValidate is a ruby wrapper for integrating with the USPS address validation api.}
14
+ spec.homepage = "https://github.com/marybethlee/address_validate"
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
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_dependency "ox", "~> 2.4.1"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "address_validate"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ require 'address_validate/version'
2
+ require 'address_validate/configuration'
3
+ require 'address_validate/api'
4
+ require 'address_validate/api/request'
5
+ require 'address_validate/api/response'
6
+ require 'address_validate/api/error'
7
+ require 'ox'
8
+
9
+ module AddressValidate
10
+ extend Configuration
11
+
12
+ def self.address_fields_map
13
+ {
14
+ 'FirmName' => AddressValidate.firm_name,
15
+ 'Address1' => AddressValidate.apartment_number,
16
+ 'Address2' => AddressValidate.street_address,
17
+ 'City' => AddressValidate.city,
18
+ 'State' => AddressValidate.state,
19
+ 'Zip5' => AddressValidate.zip_5,
20
+ 'Zip4' => AddressValidate.zip_4
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module AddressValidate
5
+ module API
6
+ def self.validate_address(data)
7
+ response = Net::HTTP.post_form(URI(AddressValidate.verify_url),
8
+ { API: 'Verify',
9
+ XML: Request.build_xml(data) })
10
+ Response.new(response.body)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module AddressValidate
2
+ module API
3
+ class Error
4
+ attr_reader :error
5
+
6
+ def initialize(error)
7
+ @error = error
8
+ end
9
+
10
+ def message
11
+ error.Description.text
12
+ end
13
+
14
+ def code
15
+ error.Number.text
16
+ end
17
+
18
+ def source
19
+ error.Source.text
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ module AddressValidate
2
+ module API
3
+ class Request
4
+ attr_reader :data
5
+
6
+ def self.build_xml(data)
7
+ new(data).build_xml
8
+ end
9
+
10
+ def initialize(data)
11
+ @data = data
12
+ end
13
+
14
+ def build_xml
15
+ request_xml = build_node('AddressValidateRequest', nil,
16
+ { 'USERID' => AddressValidate.username })
17
+ request_xml << build_address
18
+ Ox.dump(request_xml)
19
+ end
20
+
21
+ def build_node(name, text, attrs={})
22
+ node = Ox::Element.new(name)
23
+ attrs.each { |key, value| node[key] = value }
24
+ node << text if text
25
+ node
26
+ end
27
+
28
+ def build_address
29
+ address = build_node('Address', nil, { 'ID' => 0 })
30
+ AddressValidate.address_fields_map.each do |field, name|
31
+ address << build_node(field, data[name])
32
+ end
33
+ address
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ module AddressValidate
2
+ module API
3
+ class Response
4
+ attr_reader :response_body
5
+
6
+ def initialize(response_body)
7
+ @response_body = Ox.parse(response_body)
8
+ end
9
+
10
+ def success?
11
+ errors.nil?
12
+ end
13
+
14
+ def error
15
+ AddressValidate::API::Error.new(errors) if errors
16
+ end
17
+
18
+ def data
19
+ @data ||= errors ? {} : parse_data
20
+ end
21
+
22
+ def warning
23
+ get_field('ReturnText')
24
+ end
25
+
26
+ private
27
+
28
+ def errors
29
+ @errors ||= address_data.locate('Error').first
30
+ end
31
+
32
+ def address_data
33
+ @address_data ||= response_body.AddressValidateResponse.Address
34
+ end
35
+
36
+ def parse_data
37
+ {}.tap do |data|
38
+ AddressValidate.address_fields_map.each do |field, name|
39
+ data[name] = get_field(field)
40
+ end
41
+ end
42
+ end
43
+
44
+ def get_field(field_name)
45
+ field = address_data.locate(field_name).first
46
+ field.text if field
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ module AddressValidate
2
+ module Configuration
3
+ API_URLS = {
4
+ test: 'http://production.shippingapis.com/ShippingAPITest.dll',
5
+ production: 'http://production.shippingapis.com/ShippingAPI.dll'
6
+ }
7
+
8
+ attr_accessor :environment, :username, :password,
9
+ :firm_name, :street_address, :apartment_number,
10
+ :city, :state, :zip_5, :zip_4
11
+
12
+ def configure
13
+ yield self
14
+ end
15
+
16
+ def self.extended(base)
17
+ base.set_default_configuration
18
+ end
19
+
20
+ def verify_url
21
+ API_URLS[environment]
22
+ end
23
+
24
+ def set_default_configuration
25
+ self.environment = :production
26
+ self.firm_name = :firm_name
27
+ self.street_address = :street_address
28
+ self.apartment_number = :apartment_number
29
+ self.city = :city
30
+ self.state = :state
31
+ self.zip_5 = :zip_5
32
+ self.zip_4 = :zip_4
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module AddressValidate
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: address_validate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mary Lee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.4.1
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.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ description: AddressValidate is a ruby wrapper for integrating with the USPS address
84
+ validation api.
85
+ email:
86
+ - marybethlee11@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - address_validate.gemspec
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/address_validate.rb
103
+ - lib/address_validate/api.rb
104
+ - lib/address_validate/api/error.rb
105
+ - lib/address_validate/api/request.rb
106
+ - lib/address_validate/api/response.rb
107
+ - lib/address_validate/configuration.rb
108
+ - lib/address_validate/version.rb
109
+ homepage: https://github.com/marybethlee/address_validate
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 2.0.0
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: USPS address validation api integration
133
+ test_files: []