diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13e1c5b..381c93d 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..8625abb --- /dev/null +++ b/.rubocop.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b7ed20..b46e7c2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index e4412b8..60c51cb 100755 --- a/README.md +++ b/README.md @@ -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). @@ -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 ``` diff --git a/Rakefile b/Rakefile index e014861..ee159e5 100755 --- a/Rakefile +++ b/Rakefile @@ -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] diff --git a/lib/qrcode_pix_ruby/payload.rb b/lib/qrcode_pix_ruby/payload.rb index ab5b0cd..5810f36 100755 --- a/lib/qrcode_pix_ruby/payload.rb +++ b/lib/qrcode_pix_ruby/payload.rb @@ -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, @@ -33,7 +33,7 @@ class Payload :amount def payload - p = '' + p = '' p += emv(ID_PAYLOAD_FORMAT_INDICATOR, '01') p += emv_repeatable @@ -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 @@ -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 diff --git a/lib/qrcode_pix_ruby/version.rb b/lib/qrcode_pix_ruby/version.rb index 2faa47b..a2e5202 100755 --- a/lib/qrcode_pix_ruby/version.rb +++ b/lib/qrcode_pix_ruby/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module QrcodePixRuby - VERSION = '0.3.2'.freeze + VERSION = '0.3.3' end diff --git a/qrcode_pix_ruby.gemspec b/qrcode_pix_ruby.gemspec index 179c529..f354edc 100755 --- a/qrcode_pix_ruby.gemspec +++ b/qrcode_pix_ruby.gemspec @@ -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)/}) } @@ -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