virtuaaliviivakoodi 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: b85d1b0fc4e30a2da977685ca951701d0c476afe
4
+ data.tar.gz: 5cd8c6c11476f693a9eabfcfe405a53dbdcbbbe2
5
+ SHA512:
6
+ metadata.gz: 627cdfd3622a2eeb7e1de580d160dcfb8dcba92e6e16c407fedc5f7ad593c831eba17ce0db35263cda823459d2881a7feaf14657499f71263cd0d7c48b9af287
7
+ data.tar.gz: c39e096ab250576b29c554711f9c0b0ec745dd6766ad3de409048fa4ce322d2334023fe04b466b14871908bdfa6b66a3ea135f726cb14a36c78894caf5943160
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in virtuaaliviivakoodi.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Virtuaaliviivakoodi
2
+
3
+ Virtuaaliviivakoodi is a library for generating Pankkiviivakoodi of The Federation of Finnish Financial Services ("Finnish bank transfer barcode"). Implementation has been done following [the specification](http://www.finanssiala.fi/maksujenvalitys/dokumentit/Pankkiviivakoodi-opas.pdf).
4
+
5
+ The library supports both current versions of Pankkiviivakoodi, version 4 (reference number in national form) and version 5 (reference number in international form, aka Creditor Reference based on ISO 11649).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "virtuaaliviivakoodi"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install virtuaaliviivakoodi
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ Virtuaaliviivakoodi.generate({
27
+ iban: "FI37 1590 3000 0007 76",
28
+ reference: 11112,
29
+ amount: 12.25,
30
+ due_date: "161221"
31
+ }) #=> "437159030000007760000122500000000000000000011112161221"
32
+ ```
33
+
34
+ ## Development
35
+
36
+ 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.
37
+
38
+ 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).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kiskolabs/virtuaaliviivakoodi.
43
+
44
+ ## Thanks
45
+
46
+ Akseli Nurmio for his JavaScript library [virtuaaliviivakoodi](https://github.com/akselinurmio/virtuaaliviivakoodi) from which this library has borrowed bunch of test cases.
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 "virtuaaliviivakoodi"
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(__FILE__)
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
@@ -0,0 +1,28 @@
1
+ module Virtuaaliviivakoodi
2
+ class AmountNormalizer
3
+ def self.call(value)
4
+ amount = value.to_f
5
+
6
+ if amount
7
+ raise(ArgumentError, "Amount is negative") if (amount < 0)
8
+ raise(ArgumentError, "Amount is too large, maximum is 999999.99") if (amount > 999999.99)
9
+
10
+ decimals = digits_after_decimal_point(amount)
11
+ raise(ArgumentError, "There can't be more than two decimals") if decimals > 2
12
+
13
+ euros, cents = sprintf("%0.02f", amount).split(".")
14
+
15
+ Virtuaaliviivakoodi.pad(euros, 6) + cents
16
+ else
17
+ Virtuaaliviivakoodi.pad("", 8)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def self.digits_after_decimal_point(f)
24
+ sign, digits, base, exponent = BigDecimal(f.to_s).split
25
+ digits.length - exponent
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module Virtuaaliviivakoodi
2
+ DUE_DATE_PATTERN = /^[0-9]{6}$/
3
+
4
+ class DueDateNormalizer
5
+ def self.call(due_date)
6
+ if due_date
7
+ if due_date.respond_to?(:strftime)
8
+ due_date.strftime("%y%m%d")
9
+ else
10
+ raise ArgumentError, "Due date is not valid" if (!due_date.to_s.match(DUE_DATE_PATTERN))
11
+
12
+ due_date
13
+ end
14
+ else
15
+ Virtuaaliviivakoodi.pad("", 6)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,53 @@
1
+ require "bigdecimal"
2
+ require "iban-tools"
3
+ require "virtuaaliviivakoodi/iban_normalizer"
4
+ require "virtuaaliviivakoodi/reference_normalizer"
5
+ require "virtuaaliviivakoodi/due_date_normalizer"
6
+ require "virtuaaliviivakoodi/amount_normalizer"
7
+
8
+ module Virtuaaliviivakoodi
9
+ class Generator
10
+ attr_reader :iban, :reference, :amount, :due_date
11
+
12
+ def initialize(iban:, reference:, amount: nil, due_date: nil)
13
+ @iban = iban
14
+ @reference = reference
15
+ @amount = amount
16
+ @due_date = due_date
17
+ end
18
+
19
+ def call
20
+ reference_to_version + normalized_iban + normalized_amount + Virtuaaliviivakoodi.pad(normalized_reference, 23) + normalized_due_date
21
+ end
22
+
23
+ private
24
+
25
+ def normalized_due_date
26
+ Virtuaaliviivakoodi::DueDateNormalizer.call(due_date)
27
+ end
28
+
29
+ def normalized_iban
30
+ Virtuaaliviivakoodi::IBANNormalizer.call(iban)
31
+ end
32
+
33
+ def normalized_reference
34
+ Virtuaaliviivakoodi::ReferenceNormalizer.call(reference)
35
+ end
36
+
37
+ def normalized_amount
38
+ Virtuaaliviivakoodi::AmountNormalizer.call(amount)
39
+ end
40
+
41
+ def reference_to_version
42
+ if normalized_reference.size == 23
43
+ # international
44
+ "5"
45
+ elsif normalized_reference.size == 20
46
+ # national
47
+ "4"
48
+ else
49
+ raise ArgumentError, "Given reference is not of proper length. Should be either 23 or 20 characters long."
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ module Virtuaaliviivakoodi
2
+ class IBANNormalizer
3
+ def self.call(iban)
4
+ iban_validator = IBANTools::IBAN.new(iban)
5
+
6
+ raise(ArgumentError, "IBAN is not valid") unless iban_validator.validation_errors.empty?
7
+ raise(ArgumentError, "IBAN is not Finnish") unless iban_validator.country_code == "FI"
8
+
9
+ # No country code
10
+ iban_validator.code[2..-1]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module Virtuaaliviivakoodi
2
+ class ReferenceNormalizer
3
+ REFERENCE_INTERNATIONAL_PATTERN = /^RF[0-9]{3,23}$/
4
+ REFERENCE_NATIONAL_PATTERN = /^[0-9]{4,20}$/
5
+
6
+ def self.call(reference)
7
+ reference_without_whitespace = reference.to_s.gsub(/\s/, "")
8
+ case reference_without_whitespace
9
+ when REFERENCE_INTERNATIONAL_PATTERN
10
+ # Pad everything after checksum
11
+ reference_without_whitespace[2..3] + Virtuaaliviivakoodi.pad(reference_without_whitespace[4..-1], 21)
12
+ when REFERENCE_NATIONAL_PATTERN
13
+ # Pad everything
14
+ Virtuaaliviivakoodi.pad(reference_without_whitespace, 20)
15
+ else
16
+ raise ArgumentError, "Reference is not valid"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Virtuaaliviivakoodi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ require "virtuaaliviivakoodi/version"
2
+ require "virtuaaliviivakoodi/generator"
3
+
4
+ module Virtuaaliviivakoodi
5
+ # Takes parameters and returns virtuaaliviivakoodi as a String.
6
+ def self.generate(**arguments)
7
+ Generator.new(**arguments).call
8
+ end
9
+
10
+ def self.pad(value, wanted_length)
11
+ value = value.to_s
12
+ raise(ArgumentError, "Value is #{value.size} characters long, maximum being #{wanted_length}") if (value.size > wanted_length)
13
+
14
+ value.rjust(wanted_length, "0")
15
+ end
16
+ end
17
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'virtuaaliviivakoodi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "virtuaaliviivakoodi"
8
+ spec.version = Virtuaaliviivakoodi::VERSION
9
+ spec.authors = ["Vesa Vänskä"]
10
+ spec.email = ["vesa@vesavanska.com"]
11
+
12
+ spec.summary = %q{Creates virtuaaliviivakoodi}
13
+ spec.description = %q{Creates virtuaaliviivakoodi}
14
+ spec.homepage = "https://github.com/kiskolabs/virtuaaliviivakoodi"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
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_dependency "iban-tools", "~> 1.0.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtuaaliviivakoodi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vesa Vänskä
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: iban-tools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.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.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
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
+ description: Creates virtuaaliviivakoodi
70
+ email:
71
+ - vesa@vesavanska.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/virtuaaliviivakoodi.rb
85
+ - lib/virtuaaliviivakoodi/amount_normalizer.rb
86
+ - lib/virtuaaliviivakoodi/due_date_normalizer.rb
87
+ - lib/virtuaaliviivakoodi/generator.rb
88
+ - lib/virtuaaliviivakoodi/iban_normalizer.rb
89
+ - lib/virtuaaliviivakoodi/reference_normalizer.rb
90
+ - lib/virtuaaliviivakoodi/version.rb
91
+ - virtuaaliviivakoodi.gemspec
92
+ homepage: https://github.com/kiskolabs/virtuaaliviivakoodi
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Creates virtuaaliviivakoodi
115
+ test_files: []