Skip to content

Commit

Permalink
Rubocop setup (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrofurtado committed Jun 26, 2021
1 parent 3cde40e commit 70c6536
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [2.7, 3.0]
ruby-version: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
steps:
Expand Down
28 changes: 28 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
AllCops:
NewCops: enable
SuggestExtensions: false
Exclude:
- spec/*

Layout/LineLength:
Max: 120
Exclude:
- qrcode_pix_ruby.gemspec

Metrics/MethodLength:
Max: 20

Gemspec/RequiredRubyVersion:
Enabled: false

Style/Documentation:
Enabled: false

Metrics/AbcSize:
Enabled: false

Metrics/BlockNesting:
Enabled: false

Style/IfUnlessModifier:
Enabled: false
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## [Unreleased]

## [0.3.3] - 2021-06-25

- Rubocop setup
- Add rubies 2.3, 2.4, 2.5 and 2.6 to support of gem

## [0.3.2] - 2021-06-24

- Setup for run tests/specs locally with Docker
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[![Gem](https://img.shields.io/gem/dt/qrcode_pix_ruby.svg)]()
[![license](https://img.shields.io/github/license/pedrofurtado/qrcode_pix_ruby.svg)]()
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/pedrofurtado/qrcode_pix_ruby)
[![Ruby Style Guide](https://img.shields.io/badge/code_style-rubocop-brightgreen.svg)](https://github.com/rubocop/rubocop)

Ruby gem for Qrcode generation of Pix (Pagamento Instantâneo Brasileiro - Banco Central do Brasil).

Expand Down Expand Up @@ -83,8 +84,8 @@ git clone https://github.com/pedrofurtado/qrcode_pix_ruby
cd qrcode_pix_ruby/
docker build -t qrcode_pix_ruby_specs .

# Then, run this command how many times you want,
# after editing local files, and so on, to get
# Then, run this command how many times you want,
# after editing local files, and so on, to get
# feedback from test suite of gem.
docker run -v $(pwd):/app/ -it qrcode_pix_ruby_specs
```
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: %i[spec]
RuboCop::RakeTask.new
task default: %i[spec rubocop]
75 changes: 39 additions & 36 deletions lib/qrcode_pix_ruby/payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

module QrcodePixRuby
class Payload
ID_PAYLOAD_FORMAT_INDICATOR = '00'.freeze
ID_POINT_OF_INITIATION_METHOD = '01'.freeze
ID_MERCHANT_ACCOUNT_INFORMATION = '26'.freeze
ID_MERCHANT_ACCOUNT_INFORMATION_GUI = '00'.freeze
ID_MERCHANT_ACCOUNT_INFORMATION_KEY = '01'.freeze
ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION = '02'.freeze
ID_MERCHANT_CATEGORY_CODE = '52'.freeze
ID_TRANSACTION_CURRENCY = '53'.freeze
ID_TRANSACTION_AMOUNT = '54'.freeze
ID_COUNTRY_CODE = '58'.freeze
ID_MERCHANT_NAME = '59'.freeze
ID_MERCHANT_CITY = '60'.freeze
ID_POSTAL_CODE = '61'.freeze
ID_ADDITIONAL_DATA_FIELD_TEMPLATE = '62'.freeze
ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID = '05'.freeze
ID_CRC16 = '63'.freeze
ID_PAYLOAD_FORMAT_INDICATOR = '00'
ID_POINT_OF_INITIATION_METHOD = '01'
ID_MERCHANT_ACCOUNT_INFORMATION = '26'
ID_MERCHANT_ACCOUNT_INFORMATION_GUI = '00'
ID_MERCHANT_ACCOUNT_INFORMATION_KEY = '01'
ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION = '02'
ID_MERCHANT_CATEGORY_CODE = '52'
ID_TRANSACTION_CURRENCY = '53'
ID_TRANSACTION_AMOUNT = '54'
ID_COUNTRY_CODE = '58'
ID_MERCHANT_NAME = '59'
ID_MERCHANT_CITY = '60'
ID_POSTAL_CODE = '61'
ID_ADDITIONAL_DATA_FIELD_TEMPLATE = '62'
ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID = '05'
ID_CRC16 = '63'

attr_accessor :pix_key,
:repeatable,
Expand All @@ -33,7 +33,7 @@ class Payload
:amount

def payload
p = ''
p = ''

p += emv(ID_PAYLOAD_FORMAT_INDICATOR, '01')
p += emv_repeatable
Expand All @@ -51,7 +51,17 @@ def payload
end

def base64
::RQRCode::QRCode.new(payload).as_png(bit_depth: 1, border_modules: 0, color_mode: 0, color: 'black', file: nil, fill: 'white', module_px_size: 6, resize_exactly_to: false, resize_gte_to: false).to_data_url
::RQRCode::QRCode.new(payload).as_png(
bit_depth: 1,
border_modules: 0,
color_mode: 0,
color: 'black',
file: nil,
fill: 'white',
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false
).to_data_url
end

private
Expand All @@ -66,41 +76,34 @@ def emv_repeatable
end

def emv_merchant
merchant_gui = emv ID_MERCHANT_ACCOUNT_INFORMATION_GUI, 'BR.GOV.BCB.PIX'
merchant_pix_key = emv ID_MERCHANT_ACCOUNT_INFORMATION_KEY, pix_key
merchant_gui = emv(ID_MERCHANT_ACCOUNT_INFORMATION_GUI, 'BR.GOV.BCB.PIX')
merchant_pix_key = emv(ID_MERCHANT_ACCOUNT_INFORMATION_KEY, pix_key)
merchant_description = emv(ID_MERCHANT_ACCOUNT_INFORMATION_DESCRIPTION, description) if description

emv ID_MERCHANT_ACCOUNT_INFORMATION, "#{merchant_gui}#{merchant_pix_key}#{merchant_description}"
emv(ID_MERCHANT_ACCOUNT_INFORMATION, "#{merchant_gui}#{merchant_pix_key}#{merchant_description}")
end

def emv_additional_data
txid = emv(ID_ADDITIONAL_DATA_FIELD_TEMPLATE_TXID, transaction_id || '***')
emv ID_ADDITIONAL_DATA_FIELD_TEMPLATE, txid
emv(ID_ADDITIONAL_DATA_FIELD_TEMPLATE, txid)
end

def crc16(t)
extended_payload = "#{t}#{ID_CRC16}04"
def crc16(text)
extended_payload = "#{text}#{ID_CRC16}04"
extended_payload_length = extended_payload.length
polynomial = 0x1021
result = 0xFFFF

if extended_payload_length > 0
if extended_payload_length.positive?
offset = 0

while offset < extended_payload_length
result = result ^ (extended_payload[offset].bytes[0] << 8)

result ^= extended_payload[offset].bytes[0] << 8
bitwise = 0

while bitwise < 8
result = result << 1

if result & 0x10000 != 0
result = result ^ polynomial
end

result = result & 0xFFFF

result <<= 1
result ^= polynomial if result & 0x10000 != 0
result &= 0xFFFF
bitwise += 1
end

Expand Down
2 changes: 1 addition & 1 deletion lib/qrcode_pix_ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module QrcodePixRuby
VERSION = '0.3.2'.freeze
VERSION = '0.3.3'
end
26 changes: 14 additions & 12 deletions qrcode_pix_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ require_relative 'lib/qrcode_pix_ruby/version'
Gem::Specification.new do |spec|
spec.name = 'qrcode_pix_ruby'
spec.version = QrcodePixRuby::VERSION
spec.authors = ["Pedro Furtado", "Marcio de Jesus", "William Radi", "Leonardo Comar"]
spec.email = ["pedro.felipe.azevedo.furtado@gmail.com", "marciodejesusrj@gmail.com", "williamw@lbv.org.br", "lfcomar@lbv.org.br"]
spec.summary = "Ruby gem for Qrcode generation of Pix (Pagamento Instantâneo Brasileiro - Banco Central do Brasil)"
spec.description = "Ruby gem for Qrcode generation of Pix (Pagamento Instantâneo Brasileiro - Banco Central do Brasil)"
spec.homepage = "https://github.com/pedrofurtado/qrcode_pix_ruby"
spec.license = "MIT"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
spec.authors = ['Pedro Furtado', 'Marcio de Jesus', 'William Radi', 'Leonardo Comar']
spec.email = ['pedro.felipe.azevedo.furtado@gmail.com', 'marciodejesusrj@gmail.com', 'williamw@lbv.org.br', 'lfcomar@lbv.org.br']
spec.summary = 'Ruby gem for Qrcode generation of Pix (Pagamento Instantâneo Brasileiro - Banco Central do Brasil)'
spec.description = 'Ruby gem for Qrcode generation of Pix (Pagamento Instantâneo Brasileiro - Banco Central do Brasil)'
spec.homepage = 'https://github.com/pedrofurtado/qrcode_pix_ruby'
spec.license = 'MIT'
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "#{spec.homepage}/CHANGELOG.md"
spec.required_ruby_version = '>= 2.3.0'

spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
Expand All @@ -23,7 +24,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'rake', '~> 13.0'
spec.add_dependency 'rspec', '~> 3.0'
spec.add_dependency 'rqrcode', '~> 2.0'
spec.add_dependency 'rqrcode'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop'
end

0 comments on commit 70c6536

Please sign in to comment.