From b981dbbe0cba577651d17f2fc9a540ee7836807b Mon Sep 17 00:00:00 2001 From: Pedro Augusto Date: Fri, 1 Mar 2024 18:52:51 -0300 Subject: [PATCH] Add Rubocop (#8) * fix Rubocop offenses * realease 1.0 and fixed Rubocop offenses * fix style lint workflow job * fix workflow Rubocop job name --- .github/main.yml | 27 ------------ .github/workflows/main.yml | 6 +-- .rubocop.yml | 24 +++++++++++ Gemfile | 11 +++-- Gemfile.lock | 60 ++++++++++++++++++++------- Rakefile.rb | 10 +++-- bin/console | 1 + lib/zspay.rb | 14 +++---- lib/zspay/configuration.rb | 2 +- lib/zspay/resource.rb | 18 ++++---- lib/zspay/resources.rb | 10 ++--- lib/zspay/resources/bank_account.rb | 4 +- lib/zspay/resources/client.rb | 2 +- lib/zspay/resources/establishments.rb | 16 ++++--- lib/zspay/resources/sale.rb | 2 +- lib/zspay/resources/transfer.rb | 2 +- lib/zspay/version.rb | 2 +- zspay.gemspec | 25 ++++++----- 18 files changed, 136 insertions(+), 100 deletions(-) delete mode 100644 .github/main.yml create mode 100644 .rubocop.yml diff --git a/.github/main.yml b/.github/main.yml deleted file mode 100644 index 0b18c4e..0000000 --- a/.github/main.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Ruby - -on: - push: - branches: - - master - - pull_request: - -jobs: - build: - runs-on: ubuntu-latest - name: Ruby ${{ matrix.ruby }} - strategy: - matrix: - ruby: - - '3.2.2' - - steps: - - uses: actions/checkout@v4 - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: ${{ matrix.ruby }} - bundler-cache: true - - name: Run the default task - run: bundle exec rake diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0b18c4e..379d46f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,7 @@ on: jobs: build: runs-on: ubuntu-latest - name: Ruby ${{ matrix.ruby }} + name: Rubocop strategy: matrix: ruby: @@ -23,5 +23,5 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - name: Run the default task - run: bundle exec rake + - name: Run Rubocop + run: bundle exec rake rubocop diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..c9ccc68 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,24 @@ +require: rubocop-rake + +AllCops: + TargetRubyVersion: 3.0 + NewCops: enable + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Layout/LineLength: + Max: 120 + +Naming/FileName: + Exclude: + - Rakefile.rb + +# TODO: replace OpenStruct in version 2 +Style/OpenStructUse: + Enabled: false diff --git a/Gemfile b/Gemfile index 5b719a5..7df91b1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ # frozen_string_literal: true -source 'https://rubygems.org' +source "https://rubygems.org" git_source(:github) { |repo| "https://github.com/#{repo}.git" } # Declare your gem's dependencies in cockpit.gemspec. @@ -8,5 +8,10 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } # development dependencies will be added by default to the :development group. gemspec -gem 'activesupport' -gem 'logger' +gem "activesupport" +gem "byebug", "~> 11.1" +gem "logger" +gem "rake", "~> 13.0" +gem "rubocop", "~> 1.21" + +gem "rubocop-rake", "~> 0.6.0" diff --git a/Gemfile.lock b/Gemfile.lock index 7e2230d..6386840 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - zspay (0.1.8) + zspay (1.0.0) http GEM @@ -13,38 +13,66 @@ GEM minitest (>= 5.1) tzinfo (~> 2.0) zeitwerk (~> 2.3) - addressable (2.8.0) - public_suffix (>= 2.0.2, < 5.0) + addressable (2.8.6) + public_suffix (>= 2.0.2, < 6.0) + ast (2.4.2) + base64 (0.2.0) byebug (11.1.3) concurrent-ruby (1.1.9) - domain_name (0.5.20190701) - unf (>= 0.0.5, < 1.0.0) - ffi (1.15.5) + domain_name (0.6.20240107) + ffi (1.16.3) ffi-compiler (1.0.1) ffi (>= 1.0.0) rake - http (5.1.0) + http (5.2.0) addressable (~> 2.8) + base64 (~> 0.1) http-cookie (~> 1.0) http-form_data (~> 2.2) - llhttp-ffi (~> 0.4.0) + llhttp-ffi (~> 0.5.0) http-cookie (1.0.5) domain_name (~> 0.5) http-form_data (2.3.0) i18n (1.8.11) concurrent-ruby (~> 1.0) - llhttp-ffi (0.4.0) + json (2.7.1) + language_server-protocol (3.17.0.3) + llhttp-ffi (0.5.0) ffi-compiler (~> 1.0) rake (~> 13.0) logger (1.4.3) minitest (5.14.4) - public_suffix (4.0.7) + parallel (1.24.0) + parser (3.3.0.5) + ast (~> 2.4.1) + racc + prism (0.24.0) + public_suffix (5.0.4) + racc (1.7.3) + rainbow (3.1.1) rake (13.0.6) + regexp_parser (2.9.0) + rexml (3.2.6) + rubocop (1.61.0) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.30.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.0) + parser (>= 3.3.0.4) + prism (>= 0.24.0) + rubocop-rake (0.6.0) + rubocop (~> 1.0) + ruby-progressbar (1.13.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) - unf (0.1.4) - unf_ext - unf_ext (0.0.8.2) + unicode-display_width (2.5.0) zeitwerk (2.5.1) PLATFORMS @@ -52,9 +80,11 @@ PLATFORMS DEPENDENCIES activesupport - byebug + byebug (~> 11.1) logger - rake + rake (~> 13.0) + rubocop (~> 1.21) + rubocop-rake (~> 0.6.0) zspay! BUNDLED WITH diff --git a/Rakefile.rb b/Rakefile.rb index c9c085c..a7e4608 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true -task :console do - exec 'irb -r zspay -I ./lib' -end +require "bundler/gem_tasks" + +require "rubocop/rake_task" + +RuboCop::RakeTask.new + +task default: %i[rubocop] diff --git a/bin/console b/bin/console index 22e0672..e1aa746 100755 --- a/bin/console +++ b/bin/console @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require "bundler/setup" require "zspay" diff --git a/lib/zspay.rb b/lib/zspay.rb index a9c312c..2b8ba39 100644 --- a/lib/zspay.rb +++ b/lib/zspay.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require 'http' -require 'byebug' -require 'logger' -require 'active_support/hash_with_indifferent_access' +require "http" +require "byebug" +require "logger" +require "active_support/hash_with_indifferent_access" -require 'zspay/configuration' +require "zspay/configuration" -require 'zspay/resource' -require 'zspay/resources' +require "zspay/resource" +require "zspay/resources" # The Zspay module is the main namespace for the Zspay gem. # It provides methods to configure and access the gem's settings. diff --git a/lib/zspay/configuration.rb b/lib/zspay/configuration.rb index 6f05fbf..7c74189 100644 --- a/lib/zspay/configuration.rb +++ b/lib/zspay/configuration.rb @@ -13,7 +13,7 @@ class << self # # @return [String] the base URL of the Zspay API endpoint. def endpoint - 'https://api.zsystems.com.br' + "https://api.zsystems.com.br" end end end diff --git a/lib/zspay/resource.rb b/lib/zspay/resource.rb index 3672ad8..0e64e7c 100644 --- a/lib/zspay/resource.rb +++ b/lib/zspay/resource.rb @@ -12,7 +12,7 @@ class << self # @param custom_token [String, nil] an optional custom token to use for the request. # @param body [String] the format of the request body, defaults to 'json'. # @return [OpenStruct] the parsed response. - def post(path, payload = {}, custom_token = nil, body: 'json') + def post(path, payload = {}, custom_token = nil, body: "json") req(:post, path, payload, custom_token, body: body) end @@ -64,7 +64,7 @@ def put(path, payload = {}, custom_token = nil) # @param custom_token [String, nil] an optional custom token to use for the request. # @param body [String] the format of the request body, defaults to 'json'. # @return [OpenStruct] the parsed response. - def req(method, path, payload = {}, custom_token = nil, body: 'json') + def req(method, path, payload = {}, custom_token = nil, body: "json") send("req_#{body}", method, path, payload, custom_token) end @@ -94,7 +94,6 @@ def req_form(method, path, payload, custom_token) parse_body(res) end - # Parses the response body and returns it as an OpenStruct. # If the request was successful, returns the JSON parsed body. # Otherwise, logs the error and returns an error message. @@ -110,13 +109,10 @@ def parse_body(response) OpenStruct.new({ success: false, error: body }) else - error_log = Logger.new(STDERR) - error_log.error("Error while making Zspay request" \ - " to: #{response.uri}" \ - " body: #{response.body}" \ - " status: #{response.code}") + error_log = Logger.new($stderr) + error_log.error("Request error to: #{response.uri}\ncode: #{response.code}\nbody: #{response.body}") - OpenStruct.new({ success: false, message: 'An error occurred while making the request' }) + OpenStruct.new({ success: false, message: "An error occurred while making the request" }) end end @@ -135,7 +131,7 @@ def endpoint def headers(custom_token = nil) token = custom_token ? "Bearer #{custom_token}" : "Bearer #{Zspay::Configuration.token}" { - 'Authorization': token + Authorization: token } end @@ -153,7 +149,7 @@ def success_request?(response) # @return [OpenStruct, String] the parsed JSON or the original string if parsing failed. def parse_json(json) JSON.parse(json, object_class: OpenStruct) - rescue JSON::ParserError => e + rescue JSON::ParserError => _e json end end diff --git a/lib/zspay/resources.rb b/lib/zspay/resources.rb index 4368caf..6f8d7a5 100644 --- a/lib/zspay/resources.rb +++ b/lib/zspay/resources.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require 'zspay/resources/bank_account' -require 'zspay/resources/client' -require 'zspay/resources/establishments' -require 'zspay/resources/sale' -require 'zspay/resources/transfer' +require "zspay/resources/bank_account" +require "zspay/resources/client" +require "zspay/resources/establishments" +require "zspay/resources/sale" +require "zspay/resources/transfer" diff --git a/lib/zspay/resources/bank_account.rb b/lib/zspay/resources/bank_account.rb index 818fff2..ed2c6c2 100644 --- a/lib/zspay/resources/bank_account.rb +++ b/lib/zspay/resources/bank_account.rb @@ -9,7 +9,7 @@ class << self # # @return [OpenStruct] A list of bank accounts if the request is successful. def index - get('/estabelecimentos/contas_bancarias') + get("/estabelecimentos/contas_bancarias") end # Creates a new bank account for the current establishment. @@ -17,7 +17,7 @@ def index # @param account [Hash] The bank account details required for creation. # @return [OpenStruct] The created bank account details if the request is successful. def create(account) - post('/estabelecimentos/contas_bancarias', account) + post("/estabelecimentos/contas_bancarias", account) end # Activates a specific bank account for the current establishment. diff --git a/lib/zspay/resources/client.rb b/lib/zspay/resources/client.rb index 56a0988..5295fff 100644 --- a/lib/zspay/resources/client.rb +++ b/lib/zspay/resources/client.rb @@ -13,7 +13,7 @@ class << self # @param client [Hash] A hash containing the client's information, such as name, email, etc. # @return [OpenStruct] A structure containing the newly created client's details if successful. def create(client) - post('/clientes', client) + post("/clientes", client) end # Adds a new card for an existing client on the Zspay platform. diff --git a/lib/zspay/resources/establishments.rb b/lib/zspay/resources/establishments.rb index e067ad6..108d26b 100644 --- a/lib/zspay/resources/establishments.rb +++ b/lib/zspay/resources/establishments.rb @@ -2,28 +2,31 @@ module Zspay # The Establishments class manages establishment-related actions within the Zspay platform. - # It allows for listing, creating, enabling, disabling establishments, and more, extending the functionality from Zspay::Resource. + # It allows for listing, creating, enabling, disabling establishments, and more, extending the functionality + # from Zspay::Resource. class Establishments < Zspay::Resource class << self # Retrieves a list of child establishments associated with the current establishment. # # @return [OpenStruct] A structure containing the list of child establishments if the request is successful. def index - get('/estabelecimentos/filhos') + get("/estabelecimentos/filhos") end # Creates a new establishment on the Zspay platform. # # @param establishment [Hash] A hash containing the establishment's information. - # @return [OpenStruct] A structure containing the newly created establishment's details if the request is successful. + # @return [OpenStruct] A structure containing the newly created establishment's details if the request + # is successful. def create(establishment) - post('/estabelecimentos', establishment, body: 'form') + post("/estabelecimentos", establishment, body: "form") end # Activates an existing establishment on the Zspay platform. # # @param establishment_id [String] The unique identifier of the establishment to be activated. - # @return [OpenStruct] A structure indicating the activation status of the establishment if the request is successful. + # @return [OpenStruct] A structure indicating the activation status of the establishment if the request + # is successful. def active(establishment_id) post("/estabelecimentos/#{establishment_id}/habilitar") end @@ -31,7 +34,8 @@ def active(establishment_id) # Disables an existing establishment on the Zspay platform. # # @param establishment_id [String] The unique identifier of the establishment to be disabled. - # @return [OpenStruct] A structure indicating the disabled status of the establishment if the request is successful. + # @return [OpenStruct] A structure indicating the disabled status of the establishment if the request + # is successful. def disable(establishment_id) delete("/estabelecimentos/#{establishment_id}") end diff --git a/lib/zspay/resources/sale.rb b/lib/zspay/resources/sale.rb index 4e2f226..e87d610 100644 --- a/lib/zspay/resources/sale.rb +++ b/lib/zspay/resources/sale.rb @@ -12,7 +12,7 @@ class << self # @param custom_token [String, nil] An optional custom token to use for the request. # @return [OpenStruct] A structure containing the newly created sale's details if the request is successful. def create(sale, custom_token = nil) - post('/vendas', sale, custom_token) + post("/vendas", sale, custom_token) end # Retrieves details of a specific sale. diff --git a/lib/zspay/resources/transfer.rb b/lib/zspay/resources/transfer.rb index 2078966..330329e 100644 --- a/lib/zspay/resources/transfer.rb +++ b/lib/zspay/resources/transfer.rb @@ -13,7 +13,7 @@ class << self # @param custom_token [String, nil] An optional custom token to use for the request. # @return [OpenStruct] A structure containing the newly created transfer's details if the request is successful. def create(transfer, custom_token = nil) - post('/transferencias', transfer, custom_token) + post("/transferencias", transfer, custom_token) end # Retrieves the details of a scheduled transfer. diff --git a/lib/zspay/version.rb b/lib/zspay/version.rb index 0102c60..12e6f62 100644 --- a/lib/zspay/version.rb +++ b/lib/zspay/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Zspay - VERSION = '0.1.8' + VERSION = "1.0.0" end diff --git a/zspay.gemspec b/zspay.gemspec index ab21e51..d00e8dc 100644 --- a/zspay.gemspec +++ b/zspay.gemspec @@ -1,25 +1,24 @@ # frozen_string_literal: true -$LOAD_PATH.unshift(::File.join(::File.dirname(__FILE__), 'lib')) +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib")) # Maintain your gem's version: -require 'zspay/version' +require "zspay/version" # Describe your gem and declare its dependencies: Gem::Specification.new do |spec| - spec.name = 'zspay' + spec.name = "zspay" spec.version = Zspay::VERSION - spec.authors = ['Pedro Augusto Silva', 'Edson Lima', 'Nullbug'] + spec.authors = ["Pedro Augusto Silva", "Edson Lima", "Nullbug"] spec.email = %w[pedro@nullbug.dev edson@nullbug.dev] - spec.homepage = '' - spec.files = Dir['lib/**/**.rb'] - spec.summary = 'ZSPAY' - spec.description = 'ZSPAY' - spec.license = 'MIT' + spec.homepage = "" + spec.files = Dir["lib/**/**.rb"] + spec.summary = "ZSPAY" + spec.description = "ZSPAY" + spec.license = "MIT" - spec.required_ruby_version = '>= 2.4.0' + spec.required_ruby_version = Gem::Requirement.new("~> 3.0") - spec.add_runtime_dependency 'http' - spec.add_development_dependency 'byebug' - spec.add_development_dependency 'rake' + spec.add_runtime_dependency "http" + spec.metadata["rubygems_mfa_required"] = "true" end