qrcode_pix_ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4286f32be5f071b29e6f504834595f8f5dbc0ef1fdced824b76cfef429763c15
4
- data.tar.gz: 24c12240f5df73f9dff448f1521401e990b0b2f7d3b78b4131a6e5294e14f61e
3
+ metadata.gz: a7d931df9ece2ebd9af528f72a1dda1efefda84c10524b3896194d0d34d96c22
4
+ data.tar.gz: dfb9495a14d4a1607f8451465bb009e8bff5a12816cb79f314e4b7572c60023b
5
5
  SHA512:
6
- metadata.gz: 71a8669277a170358742ffbb6bbfbb7dc6c0a4d6050640050ae793d329e6af5b535756672f27b156bd9dc22c0c1a465e371cdd0393097e09fdb2074101221f5c
7
- data.tar.gz: 93e4f118bd2df45b4ed2f56a012808a0d63e40e40e9c5283ad34baf4550684997d434694f14b7227b52f0bdfda42933f5e862763a66be2dd355013d3d154d5c7
6
+ metadata.gz: 5b58df6d23aa1f1c884218da52bacf1c0450e7534eef254522f32d021fda4be283b4d001b5a0b04488f208de62512e1e60ea1baccb54e13f98171f97825eae11
7
+ data.tar.gz: 7608914b8ef248a0f6705bc4d14483ffbb5a716009de911302fef9822136b5f51f5213999922df89a8053ba1cb32745887243f6c8f11df2ef5c84c9a8b1f1ed0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2021-07-13
4
+
5
+ - Add option to pass Pix informations directly in initializer
6
+ - CI speedup
7
+ - Specs reorganization
8
+
3
9
  ## [0.4.0] - 2021-07-10
4
10
 
5
11
  - Add URL option for PIX, in order to generate dynamic QR Codes
data/README.md CHANGED
@@ -36,22 +36,27 @@ Or install it yourself as:
36
36
  ```ruby
37
37
  require 'qrcode_pix_ruby'
38
38
 
39
- pix = QrcodePixRuby::Payload.new
40
- pix.pix_key = 'minhachavedopix'
41
- pix.description = 'Pagamento do pedido 123456'
42
- pix.merchant_name = 'Fulano de Tal'
43
- pix.merchant_city = 'SAO PAULO'
44
- pix.transaction_id = 'TID12345'
45
- pix.amount = '100.00'
46
- pix.currency = '986'
47
- pix.country_code = 'BR'
48
- pix.postal_code = '01131010'
49
- pix.repeatable = false
39
+ pix = QrcodePixRuby::Payload.new(
40
+ pix_key: 'minhachavedopix',
41
+ description: 'Pagamento do pedido 123456',
42
+ merchant_name: 'Fulano de Tal',
43
+ merchant_city: 'SAO PAULO',
44
+ transaction_id: 'TID12345',
45
+ amount: '100.00',
46
+ currency: '986',
47
+ country_code: 'BR',
48
+ postal_code: '01131010',
49
+ repeatable: false
50
+ )
51
+
52
+ # If needed, change the attributes value later
53
+ pix.pix_key = 'minhaoutrachavepix'
54
+ pix.merchant_city = 'BRASILIA'
50
55
 
51
56
  # QRCode copia-e-cola
52
57
  puts pix.payload
53
58
 
54
- # QRCode para uso em imagens
59
+ # QRCode for images
55
60
  puts pix.base64
56
61
  ```
57
62
 
@@ -60,13 +65,18 @@ puts pix.base64
60
65
  ```ruby
61
66
  require 'qrcode_pix_ruby'
62
67
 
63
- pix = QrcodePixRuby::Payload.new
64
- pix.url = 'https://example.com'
65
- pix.merchant_name = 'Fulano de Tal'
66
- pix.merchant_city = 'SAO PAULO'
67
- pix.amount = '100.00'
68
- pix.transaction_id = 'TID12345'
69
- pix.repeatable = false
68
+ pix = QrcodePixRuby::Payload.new(
69
+ url: 'https://example.com',
70
+ merchant_name: 'Fulano de Tal',
71
+ merchant_city: 'SAO PAULO',
72
+ amount: '100.00',
73
+ transaction_id: 'TID12345',
74
+ repeatable: false
75
+ )
76
+
77
+ # If needed, change the attributes value later
78
+ pix.url = 'https://another-example.com'
79
+ pix.amount = 1.50
70
80
 
71
81
  # QRCode copia-e-cola
72
82
  puts pix.payload
data/Rakefile CHANGED
@@ -3,6 +3,6 @@
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
5
  require 'rubocop/rake_task'
6
- RSpec::Core::RakeTask.new(:spec)
7
6
  RuboCop::RakeTask.new
8
- task default: %i[spec rubocop]
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task default: %i[rubocop spec]
data/demo/config.ru CHANGED
@@ -4,22 +4,22 @@ require 'qrcode_pix_ruby'
4
4
 
5
5
  def generate_html_with(env)
6
6
  qrcode_data = Rack::Request.new(env).params
7
- payload = ''
8
- data_uri = ''
9
- pix = QrcodePixRuby::Payload.new
7
+ payload = ''
8
+ data_uri = ''
9
+ pix = QrcodePixRuby::Payload.new
10
10
 
11
11
  unless qrcode_data.empty?
12
- pix.pix_key = qrcode_data['pix_key'] unless qrcode_data['pix_key'].nil?
13
- pix.url = qrcode_data['url'] unless qrcode_data['url'].nil?
14
- pix.description = qrcode_data['description'] unless qrcode_data['description'].nil?
15
- pix.merchant_name = qrcode_data['merchant_name'] unless qrcode_data['merchant_name'].nil?
16
- pix.merchant_city = qrcode_data['merchant_city'] unless qrcode_data['merchant_city'].nil?
17
- pix.transaction_id = qrcode_data['transaction_id'] unless qrcode_data['transaction_id'].nil?
18
- pix.amount = qrcode_data['amount'] unless qrcode_data['amount'].nil?
19
- pix.currency = qrcode_data['currency'] unless qrcode_data['currency'].nil?
20
- pix.country_code = qrcode_data['country_code'] unless qrcode_data['country_code'].nil?
21
- pix.postal_code = qrcode_data['postal_code'] unless qrcode_data['postal_code'].nil?
22
- pix.repeatable = qrcode_data['repeatable'] == 't' unless qrcode_data['repeatable'].nil?
12
+ pix.pix_key = qrcode_data['pix_key'] if !qrcode_data['pix_key'].nil? && !qrcode_data['pix_key'].empty?
13
+ pix.url = qrcode_data['url'] if !qrcode_data['url'].nil? && !qrcode_data['url'].empty?
14
+ pix.description = qrcode_data['description'] if !qrcode_data['description'].nil? && !qrcode_data['description'].empty?
15
+ pix.merchant_name = qrcode_data['merchant_name'] if !qrcode_data['merchant_name'].nil? && !qrcode_data['merchant_name'].empty?
16
+ pix.merchant_city = qrcode_data['merchant_city'] if !qrcode_data['merchant_city'].nil? && !qrcode_data['merchant_city'].empty?
17
+ pix.transaction_id = qrcode_data['transaction_id'] if !qrcode_data['transaction_id'].nil? && !qrcode_data['transaction_id'].empty?
18
+ pix.amount = qrcode_data['amount'] if !qrcode_data['amount'].nil? && !qrcode_data['amount'].empty?
19
+ pix.currency = qrcode_data['currency'] if !qrcode_data['currency'].nil? && !qrcode_data['currency'].empty?
20
+ pix.country_code = qrcode_data['country_code'] if !qrcode_data['country_code'].nil? && !qrcode_data['country_code'].empty?
21
+ pix.postal_code = qrcode_data['postal_code'] if !qrcode_data['postal_code'].nil? && !qrcode_data['postal_code'].empty?
22
+ pix.repeatable = qrcode_data['repeatable'] == 't' if !qrcode_data['repeatable'].nil? && !qrcode_data['repeatable'].empty?
23
23
 
24
24
  payload = <<-HTML
25
25
  <label for='payload'>Payload</label>
@@ -29,7 +29,7 @@ def generate_html_with(env)
29
29
  </div>
30
30
  HTML
31
31
 
32
- data_uri = "<img style='max-width: 100%;' src='#{pix.base64}'>"
32
+ data_uri = "<img style='max-width: 100%; display: block; margin: 0 auto;' src='#{pix.base64}'>"
33
33
  end
34
34
 
35
35
  StringIO.new <<-HTML
@@ -63,58 +63,79 @@ def generate_html_with(env)
63
63
  <br>
64
64
  <form action='https://qrcode-pix-ruby.herokuapp.com' method='post'>
65
65
  <div class='mb-3'>
66
- <label for='pix_key'>Pix key</label>
66
+ <div class='form-text'>
67
+ For static Pix, please fill the 'Pix key' field.<br>
68
+ For dynamic Pix, fill the 'URL' field.<br>
69
+ Do not fill both fields.
70
+ </div>
71
+ </div>
72
+ <div class='mb-3'>
73
+ <label for='pix_key'>* Pix key</label>
67
74
  <input type='text' class='form-control' id='pix_key' value='#{qrcode_data["pix_key"]}' name='pix_key'>
75
+ <div class='form-text'>
76
+ Formats of Pix keys:<br>
77
+ CPF: 12345678910 (only numbers)<br>
78
+ Phone: +5511912345678 (+55 + DDD + phone, only numbers)<br>
79
+ Email: example@mail.com<br>
80
+ Random: a6hf7jdk3nc8iK (generated by bank)
81
+ </div>
68
82
  </div>
83
+ <div class='mb-3 text-center'> or</div>
69
84
  <div class='mb-3'>
70
- <label for='url'>URL</label>
71
- <input type='text' class='form-control' id='url' value='#{qrcode_data["url"]}' name='url'>
85
+ <label for='url'>* URL</label>
86
+ <input type='url' pattern='https?:\/\/.+' class='form-control' id='url' value='#{qrcode_data["url"]}' name='url'>
72
87
  </div>
88
+ <hr>
73
89
  <div class='mb-3'>
74
- <label for='description'>Description</label>
75
- <input required type='text' class='form-control' id='description' value='#{qrcode_data["description"]}' name='description'>
90
+ <label for='merchant_name'>* Merchant name</label>
91
+ <input required maxlength='10' type='text' class='form-control' id='merchant_name' value='#{qrcode_data["merchant_name"]}' name='merchant_name'>
76
92
  </div>
77
93
  <div class='mb-3'>
78
- <label for='merchant_name'>Merchant name</label>
79
- <input required type='text' class='form-control' id='merchant_name' value='#{qrcode_data["merchant_name"]}' name='merchant_name'>
94
+ <label for='merchant_city'>* Merchant city</label>
95
+ <input required maxlength='10' type='text' class='form-control' id='merchant_city' value='#{qrcode_data["merchant_city"]}' name='merchant_city'>
80
96
  </div>
81
97
  <div class='mb-3'>
82
- <label for='merchant_city'>Merchant city</label>
83
- <input required type='text' class='form-control' id='merchant_city' value='#{qrcode_data["merchant_city"]}' name='merchant_city'>
98
+ <label for='country_code'>* Country</label>
99
+ <select required id='country_code' name='country_code' class='form-select'>
100
+ <option></option>
101
+ <option value='BR' #{qrcode_data['country_code'] == 'BR' ? 'selected' : ''}>Brazil</option>
102
+ </select>
84
103
  </div>
85
104
  <div class='mb-3'>
86
- <label for='transaction_id'>Transaction ID</label>
87
- <input required type='text' class='form-control' id='transaction_id' value='#{qrcode_data["transaction_id"]}' name='transaction_id'>
105
+ <label for='currency'>* Currency</label>
106
+ <select required id='currency' name='currency' class='form-select'>
107
+ <option></option>
108
+ <option value='986' #{qrcode_data['currency'] == '986' ? 'selected' : ''}>Brazilian Real (R$)</option>
109
+ </select>
88
110
  </div>
89
111
  <div class='mb-3'>
90
- <label for='amount'>Amount</label>
91
- <input required type='text' class='form-control' id='amount' value='#{qrcode_data["amount"]}' name='amount'>
112
+ <label for='description'>Description</label>
113
+ <input maxlength='10' type='text' class='form-control' id='description' value='#{qrcode_data["description"]}' name='description'>
92
114
  </div>
93
115
  <div class='mb-3'>
94
- <label for='currency'>Currency</label>
95
- <select required id='currency' name='currency' class='form-select'>
96
- <option value='986' selected>Brazilian Real (R$)</option>
97
- </select>
116
+ <label for='transaction_id'>Transaction ID</label>
117
+ <input maxlength='10' pattern='[a-zA-z0-9]*' type='text' class='form-control' id='transaction_id' value='#{qrcode_data["transaction_id"]}' name='transaction_id'>
118
+ <div class='form-text'>Only numbers and simple characters, without whitespaces.</div>
98
119
  </div>
99
120
  <div class='mb-3'>
100
- <label for='country_code'>Country</label>
101
- <select required id='country_code' name='country_code' class='form-select'>
102
- <option value='BR' selected>Brazil</option>
103
- </select>
121
+ <label for='amount'>Amount</label>
122
+ <input type='number' step='0.01' pattern='[0-9]{1,}\.[0-9][0-9]' class='form-control' id='amount' value='#{qrcode_data["amount"]}' name='amount'>
123
+ <div class='form-text'>Only numbers with 2 decimal places.</div>
104
124
  </div>
105
125
  <div class='mb-3'>
106
126
  <label for='postal_code'>Postal code</label>
107
- <input type='text' class='form-control' id='postal_code' value='#{qrcode_data["postal_code"]}' name='postal_code'>
127
+ <input maxlength='8' pattern='[0-9]{8}' type='tel' class='form-control' id='postal_code' value='#{qrcode_data["postal_code"]}' name='postal_code'>
128
+ <div class='form-text'>Only numbers.</div>
108
129
  </div>
109
130
  <div class='mb-3'>
110
- <label for='repeatable'>Repeatable?</label>
131
+ <label for='repeatable'>Can be paid multiple times?</label>
111
132
  <select required id='repeatable' name='repeatable' class='form-select'>
112
- <option selected></option>
113
- <option value='t'>Yes</option>
114
- <option value='f'>No</option>
133
+ <option></option>
134
+ <option value='t' #{qrcode_data['repeatable'] == 't' ? 'selected' : ''}>Yes</option>
135
+ <option value='f' #{qrcode_data['repeatable'] == 'f' ? 'selected' : ''}>No</option>
115
136
  </select>
116
137
  </div>
117
- <div class='d-grid gap-2'>
138
+ <div class='d-grid gap-2' style='padding-bottom: 20px;'>
118
139
  <button type='submit' class='btn btn-lg btn-primary'>Generate</button>
119
140
  </div>
120
141
  </form>
@@ -124,8 +145,6 @@ def generate_html_with(env)
124
145
  <br>
125
146
  <br>
126
147
  #{payload}
127
- <br>
128
- <br>
129
148
  #{data_uri}
130
149
  </div>
131
150
  </div>
@@ -3,5 +3,4 @@
3
3
  require_relative 'qrcode_pix_ruby/version'
4
4
  require_relative 'qrcode_pix_ruby/payload'
5
5
 
6
- module QrcodePixRuby
7
- end
6
+ module QrcodePixRuby end
@@ -3,6 +3,8 @@
3
3
  require 'rqrcode'
4
4
 
5
5
  module QrcodePixRuby
6
+ class PayloadArgumentError < ArgumentError; end
7
+
6
8
  class Payload
7
9
  ID_PAYLOAD_FORMAT_INDICATOR = '00'
8
10
  ID_POINT_OF_INITIATION_METHOD = '01'
@@ -22,17 +24,17 @@ module QrcodePixRuby
22
24
  ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID = '05'
23
25
  ID_CRC16 = '63'
24
26
 
25
- attr_accessor :pix_key,
26
- :url,
27
- :repeatable,
28
- :currency,
29
- :country_code,
30
- :description,
31
- :postal_code,
32
- :merchant_name,
33
- :merchant_city,
34
- :transaction_id,
35
- :amount
27
+ ATTRIBUTES = %i[pix_key url repeatable currency country_code description postal_code
28
+ merchant_name merchant_city transaction_id amount].freeze
29
+
30
+ def initialize(**kwargs)
31
+ verify_kwargs(kwargs.keys)
32
+
33
+ ATTRIBUTES.each do |attribute|
34
+ singleton_class.class_eval { attr_accessor attribute }
35
+ instance_variable_set("@#{attribute}", kwargs[attribute])
36
+ end
37
+ end
36
38
 
37
39
  def payload
38
40
  p = ''
@@ -68,6 +70,14 @@ module QrcodePixRuby
68
70
 
69
71
  private
70
72
 
73
+ def verify_kwargs(keys)
74
+ unknowns = keys - ATTRIBUTES
75
+
76
+ return unless unknowns.any?
77
+
78
+ raise QrcodePixRuby::PayloadArgumentError, "Unknown attributes: #{unknowns.join(', ')}"
79
+ end
80
+
71
81
  def emv(id, value)
72
82
  size = value.to_s.length.to_s.rjust(2, '0')
73
83
  "#{id}#{size}#{value}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QrcodePixRuby
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qrcode_pix_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Furtado
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2021-07-10 00:00:00.000000000 Z
14
+ date: 2021-07-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rqrcode