valabn 0.1.0

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: db647378c9fb65082c6840ec5023b510b71188f0
4
+ data.tar.gz: adfc86a26ce3245596f1fef4fdfffa379f1ae903
5
+ SHA512:
6
+ metadata.gz: 2156818e94a3fa57e2471e4f62ef5a891621fbb051f42ccf7654a80f7d9329737032acb4b2510c894cdf68da230c75531ff2c7b277d1e0a1d29af7466bbc8071
7
+ data.tar.gz: d1a971e9374c1b76e8488c6db4c57843cb3c9281ee07852109196e55f080bf167fcb62ed3818e148c3a86ca26c9ece0fc46852a39ed887fca39ea7423b5f9425
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
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
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 vidadelaempresa@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 valabn.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 xxswingxx
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,73 @@
1
+ # Valabn
2
+
3
+ Valabn (read *valaban*) is a simple Australian Business Number (ABN) validator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'valabn'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install valabn
20
+
21
+ ## Usage
22
+
23
+ If you just need to check the format of the ABN, just use:
24
+
25
+ ```ruby
26
+ Valabn::Utils.validate_format(number) #=> Boolean
27
+ ```
28
+
29
+ In order to lookup real ABN, you'll need to get a GUID (`https://abr.business.gov.au/RegisterAgreement.aspx`). Once you've got one initialize the gem like this:
30
+
31
+ ```ruby
32
+ require 'valabn'
33
+
34
+ Valabn::Lookup.configure do |config|
35
+ config.guid = APP_GUID # Your assigned GUID
36
+ config.cache_host = 'localhost' # (optional) The host of the redis server
37
+ config.cache_port = '6379' # (optional) The port of the redis server
38
+ end
39
+
40
+ ```
41
+
42
+ `cache_host` and `cache_port` are optional configuration attributes in order to keep a cache on a redis database. The current cache configuration lives up to two hours.
43
+
44
+ After that, you invoke
45
+
46
+ ```ruby
47
+ Valabn::Lookup.validate(abn) #=> Hash
48
+
49
+ number = Valabn.new(abn)
50
+ number.exists? #=> Boolean
51
+ ```
52
+
53
+ ## TODO:
54
+
55
+ * Allow custom expiration time on the cache.
56
+ * Integrate with `Activerecord`/`ActiveModel` as validator.
57
+ * Add specs
58
+
59
+ ## Development
60
+
61
+ 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.
62
+
63
+ 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).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/valabn. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
68
+
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "valabn"
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
data/bin/setup ADDED
@@ -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
data/lib/valabn.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'valabn/lookup'
2
+
3
+ class Valabn
4
+ # Instance methods
5
+ def initialize(number)
6
+ @number = number.to_s.gsub(/\W/, '')
7
+ end
8
+
9
+ def exists?(options = {})
10
+ res = Valabn::Lookup.validate(@number, options)
11
+
12
+ if res.any?
13
+ res[:search_by_ab_nv201408_response][:abr_payload_search_results][:response].keys.include?(:business_entity201408)
14
+ else
15
+ false
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,64 @@
1
+ require 'savon'
2
+ require 'json'
3
+ require 'redis-namespace'
4
+ require_relative 'utils'
5
+
6
+ class Valabn
7
+ class Lookup
8
+
9
+ WSDL = 'http://www.abn.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL'
10
+ @@cache_host = nil
11
+ @@cache_port = nil
12
+ @@guid = nil
13
+
14
+ def self.guid=(value)
15
+ @@guid = value
16
+ end
17
+
18
+ def self.cache_host=(value)
19
+ @@cache_host = value
20
+ end
21
+
22
+ def self.cache_port=(value)
23
+ @@cache_port = value
24
+ end
25
+
26
+ def self.configure
27
+ yield self
28
+ end
29
+
30
+ def self.validate(number, options = {})
31
+ number = number.to_s.gsub(/\W/, '')
32
+
33
+ if Valabn::Utils.valid_format?(number)
34
+ cache = if @@cache_port && @@cache_host
35
+ redis_client = Redis.new(host: @@cache_host, port: @@cache_port, db: 15)
36
+ namespaced_redis = Redis::Namespace.new(:valabn_cache, redis: redis_client)
37
+
38
+ JSON.parse(namespaced_redis.get(number) || '{}', symbolize_names: true)
39
+ else
40
+ {}
41
+ end
42
+
43
+ if cache.any? && Time.at(cache[:expires_at]) > Time.now
44
+ cache
45
+ else
46
+ options[:includeHistoricalDetails] ||= 'n'
47
+ options[:searchString] = number
48
+ options[:authenticationGuid] = options[:guid] || @@guid
49
+
50
+ client = Savon.client(wsdl: WSDL)
51
+
52
+ response = JSON.parse(client.call(:search_by_ab_nv201408, message: options).body.to_json, symbolize_names: true)
53
+ response[:expires_at] = (Time.now + 7200).to_i
54
+
55
+ namespaced_redis.set(number, response.to_json) if @@cache_port && @@cache_host
56
+
57
+ response
58
+ end
59
+ else
60
+ {}
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,22 @@
1
+ class Valabn
2
+ class Utils
3
+ def self.valid_format?(number)
4
+ number = number.to_s.gsub(/\W/, '')
5
+
6
+ if !!( number =~ /\A\d+{11}\z/)
7
+ weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
8
+ sum = 0
9
+
10
+ numbers = number.split('').map(&:to_i)
11
+ numbers[0] = numbers[0] - 1
12
+
13
+ numbers = numbers.zip(weights).map { |x, y| x * y }
14
+ sum = numbers.inject(0, :+)
15
+
16
+ (sum % 89).zero?
17
+ else
18
+ false
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class Valabn
2
+ VERSION = "0.1.0"
3
+ end
data/valabn.gemspec ADDED
@@ -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 'valabn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "valabn"
8
+ spec.version = Valabn::VERSION
9
+ spec.authors = ["xxswingxx"]
10
+ spec.email = ["vidadelaempresa@gmail.com"]
11
+
12
+ spec.summary = %q{A wrapper for validating ABN.}
13
+ spec.description = %q{Validate the format and lookup ABN by using the ABN web services.}
14
+ spec.homepage = "https://github.com/xxswingxx/valabn"
15
+ spec.license =
16
+ "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "savon"
24
+ spec.add_development_dependency "redis"
25
+ spec.add_development_dependency "redis-namespace"
26
+ spec.add_development_dependency "bundler", "~> 1.11"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valabn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xxswingxx
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis-namespace
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Validate the format and lookup ABN by using the ABN web services.
98
+ email:
99
+ - vidadelaempresa@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/valabn.rb
115
+ - lib/valabn/lookup.rb
116
+ - lib/valabn/utils.rb
117
+ - lib/valabn/version.rb
118
+ - valabn.gemspec
119
+ homepage: https://github.com/xxswingxx/valabn
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.8
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A wrapper for validating ABN.
143
+ test_files: []