zatca 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/zatca/client.rb +40 -2
- data/lib/zatca/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaa15fcf4e0f8bc1ecdd57277871998c22eb1310294aec57ca297324f8ac2e18
|
4
|
+
data.tar.gz: 27e35dae1b305d98573e81e6edcc06eb57013ea145a50614d80889245c302989
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d1fc81fd4799d919c0df244327cdffd74cb30aa62b6d906d4b8e422bfe4dd831577d87e55672e403151563055e2ad99be28d482936d19524951c64df586991a
|
7
|
+
data.tar.gz: 41db16f2a9dde6111a5fd0d6459e0d3c4910d3ca20cc77a7220b62f3b5dc08b7841130c7e3c334b598e914297e20ec9a7038a1a6e9416ef4f958e058229dcc14
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# zatca
|
2
|
-
![](https://img.shields.io/gem/v/zatca) ![](https://img.shields.io/github/workflow/status/mrsool/zatca/
|
2
|
+
![](https://img.shields.io/gem/v/zatca) ![](https://img.shields.io/github/actions/workflow/status/mrsool/zatca/test.yml?branch=main)
|
3
3
|
|
4
4
|
A Ruby library for generating QR Codes and e-invoices according to the standard created by ZATCA in Saudi Arabia.
|
5
5
|
|
@@ -31,7 +31,7 @@ tags = {
|
|
31
31
|
invoice_total: "115",
|
32
32
|
}
|
33
33
|
|
34
|
-
ZATCA.render_qr_code(tags)
|
34
|
+
ZATCA.render_qr_code(tags: tags)
|
35
35
|
# => data:image/png;base64,...
|
36
36
|
# Hint (Try pasting the above into your web browser's address bar)
|
37
37
|
```
|
@@ -49,7 +49,7 @@ tags = ZATCA::Tags.new({
|
|
49
49
|
invoice_total: "115",
|
50
50
|
})
|
51
51
|
|
52
|
-
generator = ZATCA::QRCodeGenerator.new(tags)
|
52
|
+
generator = ZATCA::QRCodeGenerator.new(tags: tags)
|
53
53
|
generator.render(size: 512)
|
54
54
|
```
|
55
55
|
|
data/lib/zatca/client.rb
CHANGED
@@ -4,6 +4,8 @@ require "json"
|
|
4
4
|
# This wraps the API described here:
|
5
5
|
# https://sandbox.zatca.gov.sa/IntegrationSandbox
|
6
6
|
class ZATCA::Client
|
7
|
+
attr_accessor :before_submitting_request, :before_parsing_response
|
8
|
+
|
7
9
|
# API URLs are not present in developer portal, they can only be found in a PDF
|
8
10
|
# called Fatoora Portal User Manual, here:
|
9
11
|
# https://zatca.gov.sa/en/E-Invoicing/Introduction/Guidelines/Documents/Fatoora%20portal%20user%20manual.pdf
|
@@ -20,15 +22,28 @@ class ZATCA::Client
|
|
20
22
|
DEFAULT_API_VERSION = "V2".freeze
|
21
23
|
LANGUAGES = %w[ar en].freeze
|
22
24
|
|
23
|
-
def initialize(
|
25
|
+
def initialize(
|
26
|
+
username:,
|
27
|
+
password:,
|
28
|
+
language: "ar",
|
29
|
+
version: DEFAULT_API_VERSION,
|
30
|
+
environment: :production,
|
31
|
+
verbose: false,
|
32
|
+
before_submitting_request: nil,
|
33
|
+
before_parsing_response: nil
|
34
|
+
)
|
24
35
|
raise "Invalid language: #{language}, Please use one of: #{LANGUAGES}" unless LANGUAGES.include?(language)
|
25
36
|
|
26
37
|
@username = username
|
27
38
|
@password = password
|
28
39
|
@language = language
|
29
40
|
@version = version
|
41
|
+
@verbose = verbose
|
30
42
|
|
31
43
|
@base_url = ENVIRONMENTS_TO_URLS_MAP[environment.to_sym] || PRODUCTION_BASE_URL
|
44
|
+
|
45
|
+
@before_submitting_request = before_submitting_request
|
46
|
+
@before_parsing_response = before_parsing_response
|
32
47
|
end
|
33
48
|
|
34
49
|
# Reporting API
|
@@ -132,6 +147,9 @@ class ZATCA::Client
|
|
132
147
|
url = "#{@base_url}/#{path}"
|
133
148
|
headers = default_headers.merge(headers)
|
134
149
|
|
150
|
+
before_submitting_request&.call(method, url, body, headers)
|
151
|
+
log("Requesting #{method} #{url} with\n\nbody: #{body}\n\nheaders: #{headers}\n")
|
152
|
+
|
135
153
|
client = if authenticated
|
136
154
|
authenticated_request_cilent
|
137
155
|
else
|
@@ -139,14 +157,27 @@ class ZATCA::Client
|
|
139
157
|
end
|
140
158
|
|
141
159
|
response = client.send(method, url, json: body, headers: headers)
|
160
|
+
before_parsing_response&.call(response)
|
161
|
+
log("Raw response: #{response}")
|
162
|
+
|
163
|
+
if response.instance_of?(HTTPX::ErrorResponse)
|
164
|
+
return {
|
165
|
+
message: response.error&.message,
|
166
|
+
details: response.to_s
|
167
|
+
}
|
168
|
+
end
|
142
169
|
|
143
170
|
response_body = response.body.to_s
|
144
171
|
|
145
|
-
if response.headers["Content-Type"] == "application/json"
|
172
|
+
parsed_body = if response.headers["Content-Type"] == "application/json"
|
146
173
|
parse_json_or_return_string(response_body)
|
147
174
|
else
|
148
175
|
response_body
|
149
176
|
end
|
177
|
+
|
178
|
+
log("Response body: #{parsed_body}")
|
179
|
+
|
180
|
+
parsed_body
|
150
181
|
end
|
151
182
|
|
152
183
|
def authenticated_request_cilent
|
@@ -170,4 +201,11 @@ class ZATCA::Client
|
|
170
201
|
rescue JSON::ParserError
|
171
202
|
json
|
172
203
|
end
|
204
|
+
|
205
|
+
def log(message)
|
206
|
+
return unless @verbose
|
207
|
+
message = "\n\n---------------------\n\n#{message}\n\n"
|
208
|
+
|
209
|
+
puts message
|
210
|
+
end
|
173
211
|
end
|
data/lib/zatca/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zatca
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Bahareth
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|