zatca 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 31fc90251ebaaa59adb0c0e56c685d7a0328621d4135ec652c1b755ffaba9dcb
4
+ data.tar.gz: dfe4cf0d45af7b39910b74f960f9eac6396baa4d023633f8a14dd45f23f0fb36
5
+ SHA512:
6
+ metadata.gz: 509c63d4567ad7a299feb239f52138f89d5085785584f12a8b2b2f2ef907cdcbbfe84a902b301eb04e5b13fac42811f9c7932e84606c861427da66da07024755
7
+ data.tar.gz: 531d7fe55112f3c5f50765895a9d0276fd9dcef4807fba574c901fd73fd2be8c7adf2bd304469744dc0fb1adb41da73e7991c7cb0020abfc95988a3b9aa889cb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ -I lib/zatca
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in zatca-qr-code.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,100 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zatca-qr-code (0.1.0)
5
+ dry-schema (~> 1.5)
6
+ rqrcode (~> 2.1.0)
7
+ zeitwerk (~> 2.4)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ byebug (9.1.0)
14
+ chunky_png (1.4.0)
15
+ concurrent-ruby (1.1.9)
16
+ diff-lcs (1.4.4)
17
+ dry-configurable (0.13.0)
18
+ concurrent-ruby (~> 1.0)
19
+ dry-core (~> 0.6)
20
+ dry-container (0.9.0)
21
+ concurrent-ruby (~> 1.0)
22
+ dry-configurable (~> 0.13, >= 0.13.0)
23
+ dry-core (0.7.1)
24
+ concurrent-ruby (~> 1.0)
25
+ dry-inflector (0.2.1)
26
+ dry-initializer (3.0.4)
27
+ dry-logic (1.2.0)
28
+ concurrent-ruby (~> 1.0)
29
+ dry-core (~> 0.5, >= 0.5)
30
+ dry-schema (1.8.0)
31
+ concurrent-ruby (~> 1.0)
32
+ dry-configurable (~> 0.13, >= 0.13.0)
33
+ dry-core (~> 0.5, >= 0.5)
34
+ dry-initializer (~> 3.0)
35
+ dry-logic (~> 1.0)
36
+ dry-types (~> 1.5)
37
+ dry-types (1.5.1)
38
+ concurrent-ruby (~> 1.0)
39
+ dry-container (~> 0.3)
40
+ dry-core (~> 0.5, >= 0.5)
41
+ dry-inflector (~> 0.1, >= 0.1.2)
42
+ dry-logic (~> 1.0, >= 1.0.2)
43
+ parallel (1.21.0)
44
+ parser (3.0.2.0)
45
+ ast (~> 2.4.1)
46
+ rainbow (3.0.0)
47
+ rake (13.0.6)
48
+ regexp_parser (2.1.1)
49
+ rexml (3.2.5)
50
+ rqrcode (2.1.0)
51
+ chunky_png (~> 1.0)
52
+ rqrcode_core (~> 1.0)
53
+ rqrcode_core (1.2.0)
54
+ rspec (3.10.0)
55
+ rspec-core (~> 3.10.0)
56
+ rspec-expectations (~> 3.10.0)
57
+ rspec-mocks (~> 3.10.0)
58
+ rspec-core (3.10.1)
59
+ rspec-support (~> 3.10.0)
60
+ rspec-expectations (3.10.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.10.0)
63
+ rspec-mocks (3.10.2)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.10.0)
66
+ rspec-support (3.10.2)
67
+ rubocop (1.20.0)
68
+ parallel (~> 1.10)
69
+ parser (>= 3.0.0.0)
70
+ rainbow (>= 2.2.2, < 4.0)
71
+ regexp_parser (>= 1.8, < 3.0)
72
+ rexml
73
+ rubocop-ast (>= 1.9.1, < 2.0)
74
+ ruby-progressbar (~> 1.7)
75
+ unicode-display_width (>= 1.4.0, < 3.0)
76
+ rubocop-ast (1.12.0)
77
+ parser (>= 3.0.1.1)
78
+ rubocop-performance (1.11.5)
79
+ rubocop (>= 1.7.0, < 2.0)
80
+ rubocop-ast (>= 0.4.0)
81
+ ruby-progressbar (1.11.0)
82
+ standard (1.3.0)
83
+ rubocop (= 1.20.0)
84
+ rubocop-performance (= 1.11.5)
85
+ unicode-display_width (2.1.0)
86
+ zeitwerk (2.4.2)
87
+
88
+ PLATFORMS
89
+ x86_64-darwin-20
90
+ x86_64-linux
91
+
92
+ DEPENDENCIES
93
+ byebug (~> 9.0)
94
+ rake (~> 13.0)
95
+ rspec (~> 3.10)
96
+ standard (~> 1.3)
97
+ zatca-qr-code!
98
+
99
+ BUNDLED WITH
100
+ 2.2.27
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Mrsool
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # zatca-qr-code
2
+ ![](https://img.shields.io/gem/v/zatca-qr-code) ![](https://img.shields.io/github/workflow/status/mrsool/zatca-qr-code/Ruby)
3
+
4
+ [🚧 Still being validated by ZATCA](https://github.com/mrsool/zatca-qr-code/issues/3).
5
+
6
+ A Ruby library for generating QR Codes for the e-invoice standard by ZATCA in Saudi Arabia.
7
+
8
+ # Installation
9
+
10
+ ## Rubygems
11
+ ```sh
12
+ gem install zatca-qr-code
13
+ ```
14
+
15
+ ## Bundler
16
+ ```sh
17
+ bundle add zatca-qr-code
18
+ ```
19
+
20
+ # Usage
21
+
22
+ ```rb
23
+ require "zatca-qr-code"
24
+
25
+ tags = {
26
+ seller_name: "Mrsool",
27
+ vat_registration_number: "310228833400003",
28
+ timestamp: "2021-10-20T19:29:32+03:00",
29
+ vat_total: "15",
30
+ invoice_total: "115",
31
+ }
32
+
33
+ ZATCA.render_qr_code(tags)
34
+ # => data:image/png;base64,...
35
+ # Hint (Try pasting the above into your web browser's address bar)
36
+ ```
37
+
38
+ If you'd like to customize the size of the QR Code you can manually use the generator like so:
39
+
40
+ ```rb
41
+ require "zatca-qr-code"
42
+
43
+ tags = ZATCA::Tags.new({
44
+ seller_name: "Mrsool",
45
+ vat_registration_number: "310228833400003",
46
+ timestamp: "2021-10-20T19:29:32+03:00",
47
+ vat_total: "15",
48
+ invoice_total: "115",
49
+ })
50
+
51
+ generator = ZATCA::QRCodeGenerator.new(tags)
52
+ generator.render(size: 512)
53
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require_relative "../lib/zatca.rb"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,21 @@
1
+ require "rqrcode"
2
+
3
+ module ZATCA
4
+ class QRCodeGenerator
5
+ def initialize(tags)
6
+ @tags = tags
7
+ end
8
+
9
+ def render(size: 256)
10
+ qr_code = generate
11
+
12
+ qr_code.as_png(size: size, border_modules: 2)&.to_data_url
13
+ end
14
+
15
+ private
16
+
17
+ def generate
18
+ RQRCode::QRCode.new(@tags.to_base64)
19
+ end
20
+ end
21
+ end
data/lib/zatca/tag.rb ADDED
@@ -0,0 +1,31 @@
1
+ module ZATCA
2
+ class Tag
3
+ TAG_IDS = {
4
+ seller_name: 1,
5
+ vat_registration_number: 2,
6
+ timestamp: 3,
7
+ invoice_total: 4,
8
+ vat_total: 5
9
+ }.freeze
10
+
11
+ attr_accessor :id, :key, :value
12
+
13
+ def initialize(key:, value:)
14
+ @id = TAG_IDS[key]
15
+ @key = key
16
+ @value = value.to_s
17
+ end
18
+
19
+ def to_h
20
+ {id: @id, key: @key, value: @value}
21
+ end
22
+
23
+ def to_tlv
24
+ # TLV should be concatenated together without any separator in the following
25
+ # format: hex_value_of_id hex_value_of_length value_itself
26
+ # where hex_values should be padded with a 0 if containing only a single digit.
27
+
28
+ sprintf("%02X%02X%s", @id, @value.length, @value)
29
+ end
30
+ end
31
+ end
data/lib/zatca/tags.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "base64"
2
+
3
+ module ZATCA
4
+ class Tags
5
+ def initialize(tags)
6
+ keys_to_coerce_to_string = tags.except(:timestamp).keys
7
+ stringified_tags = tags.map do |key, value|
8
+ if keys_to_coerce_to_string.include?(key)
9
+ [key, value.to_s]
10
+ else
11
+ [key, value]
12
+ end
13
+ end.to_h
14
+
15
+ schema_result = TagsSchema.call(stringified_tags)
16
+ if schema_result.failure?
17
+ raise(Error, "Parsing tags failed due to:\n#{schema_result.errors(full: true).to_h}")
18
+ end
19
+
20
+ stringified_tags[:timestamp] = tags[:timestamp].to_s
21
+
22
+ # Create tags, then sort them by ID
23
+ @tags = stringified_tags.map do |key, value|
24
+ Tag.new(key: key, value: value)
25
+ end.sort_by(&:id)
26
+ end
27
+
28
+ def to_base64
29
+ Base64.strict_encode64(to_tlv)
30
+ end
31
+
32
+ private
33
+
34
+ def to_tlv
35
+ @tags.map(&:to_tlv).join("")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,22 @@
1
+ require "dry-schema"
2
+
3
+ module ZATCA
4
+ TagsSchema = Dry::Schema.Params do
5
+ required(:seller_name).filled(:string)
6
+ required(:vat_registration_number).filled(:string)
7
+
8
+ required(:timestamp).filled([:date_time, :string])
9
+
10
+ # Using strings to avoid any floating point approximations, we're not doing
11
+ # any calculations with these values, it's for display purposes only. A
12
+ # string is a good fit.
13
+ required(:invoice_total).filled(:string)
14
+ required(:vat_total).filled(:string)
15
+
16
+ # TODO: Data types required by 1 January 2023
17
+ # - Hash of XML Invoice
18
+ # - ECDSA signature
19
+ # - ECDSA public key
20
+ # - ECDSA signature of the cryptographic stamp’s public key by ZATCA’s technical CA
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ZATCA
4
+ VERSION = "0.1.0"
5
+ end
data/lib/zatca.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "zatca/version"
4
+
5
+ require "zeitwerk"
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+ loader.inflector.inflect(
9
+ "zatca" => "ZATCA",
10
+ "qr_code_generator" => "QRCodeGenerator"
11
+ )
12
+ loader.setup
13
+
14
+ module ZATCA
15
+ extend self
16
+
17
+ class Error < StandardError; end
18
+
19
+ def self.render_qr_code(tags)
20
+ QRCodeGenerator.new(ZATCA::Tags.new(tags)).render
21
+ end
22
+ end
23
+
24
+ loader.eager_load
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zatca
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Omar Bahareth
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-schema
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rqrcode
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '9.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '9.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.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.10'
97
+ description: A library for generating QR Codes for the e-invoice standard by ZATCA
98
+ in Saudi Arabia.
99
+ email:
100
+ - obahareth@mrsool.co
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".rspec"
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/zatca.rb
114
+ - lib/zatca/qr_code_generator.rb
115
+ - lib/zatca/tag.rb
116
+ - lib/zatca/tags.rb
117
+ - lib/zatca/tags_schema.rb
118
+ - lib/zatca/version.rb
119
+ homepage: https://github.com/mrsool/zatca-qr-code
120
+ licenses: []
121
+ metadata:
122
+ allowed_push_host: https://rubygems.org
123
+ homepage_uri: https://github.com/mrsool/zatca-qr-code
124
+ source_code_uri: https://github.com/mrsool/zatca-qr-code
125
+ changelog_uri: https://github.com/mrsool/zatca-qr-code/releases
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 2.4.0
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.2.22
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: A library for generating QR Codes for the e-invoice standard by ZATCA in
145
+ Saudi Arabia.
146
+ test_files: []