swiss_qr_bill 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
+ SHA256:
3
+ metadata.gz: f994168de5ab5bb925c716de6341de11fb79c403641858e94552a7576ab20ab1
4
+ data.tar.gz: 57e759c4076e09e3ec2ff556eb1d22c084d1788a1b5154ccf2d3eb390d0f2132
5
+ SHA512:
6
+ metadata.gz: 28c74f4a36ecc862d27305720b4183d8ee065f31858017ecf9831b079d8b6cb72d934ada97b23400626b0858188963452dc9b3a0017d1ff72615f50b957260ca
7
+ data.tar.gz: d42289f1e25ed4d2166ae713af67b6fcea4290d9f22444d48ba64a25e1ed9ca6316df2bbad637bf2837d6e003e81bce5e5f0e8ed832cd83a2b0d4cfc50898d17
data/.node-version ADDED
@@ -0,0 +1 @@
1
+ 22.11.0
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.2
4
+ SuggestExtensions: false
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Style/NumericLiteralPrefix:
18
+ Enabled: false
19
+
20
+ Metrics/AbcSize:
21
+ Enabled: false
22
+
23
+ Metrics/PerceivedComplexity:
24
+ Max: 10
25
+
26
+ Metrics/CyclomaticComplexity:
27
+ Max: 10
28
+
29
+ Metrics/MethodLength:
30
+ Max: 20
31
+ Exclude:
32
+ - "test/**/*"
33
+
34
+ Layout/LineLength:
35
+ Max: 200
36
+
37
+ Lint/ConstantDefinitionInBlock:
38
+ Exclude:
39
+ - lib/swiss_qr_bill/qr_bill.rb
data/.yarnrc ADDED
@@ -0,0 +1 @@
1
+ --ignore-engines true
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-12-24
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at marco.roth@intergga.ch. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # SwissQRBill
2
+
3
+ A Ruby Gem for reading and parsing Swiss QR-Bills from documents like PDFs and images.
4
+
5
+ This gem is meant for reading and parsing Swiss QR-Bill codes out of documents, if you are looking to generate Swiss QR-Bills check out the [`qr-bills` gem](https://github.com/damoiser/qr-bills).
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ ```bash
12
+ bundle add swiss_qr_bill
13
+ ```
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ ```bash
18
+ gem install swiss_qr_bill
19
+ ```
20
+
21
+ Install the required NPM packages:
22
+
23
+ ```bash
24
+ yarn add @andreekeberg/imagedata pdf-to-png-converter jsqr
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require "swiss_qr_bill"
31
+
32
+ qr_bills = SwissQRBill.parse_file("path/to/invoice.pdf")
33
+
34
+ qr_bills.first
35
+ # =>
36
+ # #<data SwissQRBill::QRBill
37
+ # header=#<data SwissQRBill::QRBill::Header qr_type="SPC", version="0200", coding_type="1">,
38
+ # creditor_information=#<data SwissQRBill::QRBill::CreditorInformation iban="CH6431961000004421557">,
39
+ # creditor= #<data SwissQRBill::QRBill::StructuredAddress type="S", name="Health insurance fit&kicking", street="Am Wasser", building_number="1", postal_code="3000", town="Bern", country="CH">,
40
+ # ulimate_creditor=#<data SwissQRBill::QRBill::StructuredAddress type="", name="", street="", building_number="", postal_code="", town="", country="">,
41
+ # debtor=#<data SwissQRBill::QRBill::StructuredAddress type="S", name="Sarah Beispiel", street="Mustergasse", building_number="1", postal_code="3600", town="Thun", country="CH">,
42
+ # payment_amount_information=#<data SwissQRBill::QRBill::PaymentAmountInformation amount=111.0, currency="CHF">,
43
+ # payment_reference=#<data SwissQRBill::QRBill::PaymentReference type="QRR", reference="000008207791225857421286694", additional_information="Premium calculation July 2020">,
44
+ # trailer=#<data SwissQRBill::QRBill::Trailer trailer="EPD">,
45
+ # additional_information=#<data SwissQRBill::QRBill::AdditionalInformation billing_information=nil, av1=nil, av2=nil>,
46
+ # raw=[...]
47
+ # >
48
+
49
+ qr_bills.first.creditor
50
+ # =>
51
+ # #<data SwissQRBill::QRBill::StructuredAddress
52
+ # type="S",
53
+ # name="Health insurance fit&kicking",
54
+ # street="Am Wasser",
55
+ # building_number="1",
56
+ # postal_code="3000",
57
+ # town="Bern",
58
+ # country="CH"
59
+ # >
60
+
61
+ qr_bills.first.to_s
62
+ # => "SPC\n0200\n1\nCH6431961000004421557\nS\nHealth insurance fit&kicking\nAm Wasser\n1\n3000\nBern\nCH\n\n\n\n\n\n\n\n111.0\nCHF\nS\nSarah Beispiel\nMustergasse\n1\n3600\nThun\nCH\nQRR\n000008207791225857421286694\nPremium calculation July 2020\nEPD\n\n\n"
63
+ ```
64
+
65
+ ## Additional Information
66
+
67
+ #### Specification
68
+
69
+ * [Swiss Implementation Guidelines for the QR-bill](https://www.six-group.com/dam/download/banking-services/standardization/qr-bill/ig-qr-bill-v2.2-en.pdf)
70
+ * [Six Download Center (For sample data, specifications, and more)](https://www.six-group.com/en/products-services/banking-services/payment-standardization/downloads-faq/download-center.html)
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` 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 the created tag, 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/marcoroth/swiss_qr_bill. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/marcoroth/swiss_qr_bill/blob/main/CODE_OF_CONDUCT.md).
81
+
82
+ ## Code of Conduct
83
+
84
+ Everyone interacting in the SwissQRBill project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/marcoroth/swiss_qr_bill/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: %i[test]
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwissQRBill
4
+ class QRBill < Data.define(:header, :creditor_information, :creditor, :ulimate_creditor, :debtor, :payment_amount_information, :payment_reference, :trailer, :additional_information, :raw) # rubocop:disable Style/DataInheritance
5
+ Header = Data.define(:qr_type, :version, :coding_type)
6
+ CreditorInformation = Data.define(:iban)
7
+ PaymentReference = Data.define(:type, :reference, :additional_information)
8
+ Trailer = Data.define(:trailer)
9
+
10
+ PaymentAmountInformation = Data.define(:amount, :currency) do
11
+ def initialize(amount:, currency:)
12
+ super(amount: amount.to_f, currency:)
13
+ end
14
+ end
15
+
16
+ CombinedAddress = Data.define(:type, :name, :line1, :line2, :country) do
17
+ def street = line1
18
+ def building_number = line1
19
+ def postal_code = line2
20
+ def town = line2
21
+ end
22
+
23
+ StructuredAddress = Data.define(:type, :name, :street, :building_number, :postal_code, :town, :country) do
24
+ def line1 = "#{street} #{building_number}"
25
+ def line2 = "#{postal_code} #{town}"
26
+ end
27
+
28
+ class Address
29
+ def self.new(*data)
30
+ case data[0]
31
+ when "K"
32
+ CombinedAddress.new(
33
+ type: data[0],
34
+ name: data[1],
35
+ line1: data[2],
36
+ line2: data[3],
37
+ country: data[6]
38
+ )
39
+ when "S"
40
+ StructuredAddress.new(*data)
41
+ else
42
+ raise %(Unknown Address type: "#{data[0]}") unless data.map(&:empty?).reduce(:&)
43
+
44
+ StructuredAddress.new(*data)
45
+ end
46
+ end
47
+ end
48
+
49
+ AdditionalInformation = Data.define(:billing_information, :av1, :av2) do
50
+ def initialize(billing_information: nil, av1: nil, av2: nil)
51
+ super
52
+ end
53
+ end
54
+
55
+ REGIONS = {
56
+ 00..02 => [:header, Header],
57
+ 03..03 => [:creditor_information, CreditorInformation],
58
+ 04..10 => [:creditor, Address],
59
+ 11..17 => [:ulimate_creditor, Address],
60
+ 18..19 => [:payment_amount_information, PaymentAmountInformation],
61
+ 20..26 => [:debtor, Address],
62
+ 27..29 => [:payment_reference, PaymentReference],
63
+ 30..30 => [:trailer, Trailer],
64
+ 31..33 => [:additional_information, AdditionalInformation]
65
+ }.freeze
66
+
67
+ def to_raw_data
68
+ [
69
+ header,
70
+ creditor_information,
71
+ creditor,
72
+ ulimate_creditor,
73
+ payment_amount_information,
74
+ debtor,
75
+ payment_reference,
76
+ trailer,
77
+ additional_information
78
+ ].flat_map(&:deconstruct).map(&:to_s)
79
+ end
80
+
81
+ def to_s
82
+ to_raw_data.join("\n")
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwissQRBill
4
+ class QRBillParser
5
+ def self.parse_file(path)
6
+ detected_codes = QRCodeParser.parse_file(path)
7
+
8
+ return [] if detected_codes.empty?
9
+
10
+ detected_codes.map { |code| parse(code) }
11
+ end
12
+
13
+ def self.parse(data)
14
+ case data
15
+ when String
16
+ data = if data.count("\r").positive?
17
+ data.split("\r\n")
18
+ else
19
+ data.split("\n")
20
+ end
21
+ when Array
22
+ # ok
23
+ else
24
+ raise "Unsupported data argument type. Expected data argument of type Array or String, got: #{data.class}"
25
+ end
26
+
27
+ raise "Unsupported data length. Data object must have at least 31 entries. Found: #{data.length}" if data.length < 31
28
+ raise "Unsupported QRType: #{data[0]}" if data[0] != "SPC"
29
+ raise "Unsupported Version: #{data[1]}" if data[1] != "0200"
30
+ raise "Unsupported Coding: #{data[2]}" if data[2] != "1"
31
+
32
+ regions = QRBill::REGIONS.to_h { |range, (key, klass)| [key, klass.new(*data[range])] }
33
+
34
+ QRBill.new(
35
+ raw: data,
36
+ **regions
37
+ )
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nodo"
4
+
5
+ module SwissQRBill
6
+ class QRCodeParser < Nodo::Core
7
+ require fs: "node:fs"
8
+ require path: "node:path"
9
+
10
+ require jsQR: "jsqr"
11
+ require pdfToPng: "pdf-to-png-converter"
12
+ require imagedata: "@andreekeberg/imagedata"
13
+
14
+ def self.parse_file(path)
15
+ if path.end_with?(".pdf")
16
+ new.parse_pdf(path)
17
+ else
18
+ [new.parse_image(path)]
19
+ end
20
+ end
21
+
22
+ function :parse_image, <<~JS
23
+ (imagePath) => {
24
+ const { data, width, height } = imagedata.getSync(imagePath)
25
+
26
+ return jsQR(data, width, height)?.data
27
+ }
28
+ JS
29
+
30
+ function :parse_pdf, <<~JS
31
+ async (pdfPath) => {
32
+ const filename = path.basename(pdfPath)
33
+
34
+ const pages = await pdfToPng.pdfToPng(pdfPath, { // The function accepts PDF file path or a Buffer
35
+ disableFontFace: false, // When `false`, fonts will be rendered using a built-in font renderer that constructs the glyphs with primitive path commands. Default value is true.
36
+ // useSystemFonts: false, // When `true`, fonts that aren't embedded in the PDF document will fallback to a system font. Default value is false.
37
+ // enableXfa: false, // Render Xfa forms if any. Default value is false.
38
+ viewportScale: 3.0, // The desired scale of PNG viewport. Default value is 1.0.
39
+ outputFolder: 'tmp/swiss_qr_bill', // Folder to write output PNG files. If not specified, PNG output will be available only as a Buffer content, without saving to a file.
40
+ outputFileMaskFunc: (page) => `${filename}_page_${page}.png`, // Function to generate custom output filenames. Example: (pageNum) => `page_${pageNum}.png`
41
+ // pdfFilePassword: 'pa$$word', // Password for encrypted PDF.
42
+ // pagesToProcess: [1, 3, 11], // Subset of pages to convert (first page = 1), other pages will be skipped if specified.
43
+ // strictPagesToProcess: false, // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
44
+ // verbosityLevel: 0 // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.
45
+ })
46
+
47
+ const result = pages.map(page => parse_image(page.path))
48
+
49
+ pages.map(page => fs.unlinkSync(page.path)) // cleanup generated images
50
+
51
+ return result.filter(result => result)
52
+ }
53
+ JS
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwissQRBill
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "swiss_qr_bill/version"
4
+ require_relative "swiss_qr_bill/qr_code_parser"
5
+
6
+ require_relative "swiss_qr_bill/qr_bill"
7
+ require_relative "swiss_qr_bill/qr_bill_parser"
8
+
9
+ module SwissQRBill
10
+ def self.parse_file(...)
11
+ QRBillParser.parse_file(...)
12
+ end
13
+ end
data/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "swiss_qr_bill",
3
+ "private": true,
4
+ "author": "Marco Roth <marco.roth@intergga.ch>",
5
+ "license": "MIT",
6
+ "dependencies": {
7
+ "@andreekeberg/imagedata": "^1.0.2",
8
+ "jsqr": "^1.4.0",
9
+ "pdf-to-png-converter": "^3.6.3"
10
+ }
11
+ }
@@ -0,0 +1,4 @@
1
+ module SwissQRBill
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/swiss_qr_bill/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "swiss_qr_bill"
7
+ spec.version = SwissQRBill::VERSION
8
+ spec.authors = ["Marco Roth"]
9
+ spec.email = ["marco.roth@intergga.ch"]
10
+
11
+ spec.summary = "A Ruby Gem for reading and parsing Swiss QR-Bills from documents."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/marcoroth/swiss_qr_bill"
14
+ spec.required_ruby_version = ">= 3.2.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["rubygems_mfa_required"] = "true"
18
+ spec.metadata["source_code_uri"] = "https://github.com/marcoroth/swiss_qr_bill"
19
+ spec.metadata["changelog_uri"] = "https://github.com/marcoroth/swiss_qr_bill/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "nodo"
34
+ end