├── pix_key ├── .ruby-version ├── .tool-versions ├── .rspec ├── lib │ └── pix_key.rb ├── Rakefile ├── Gemfile ├── bin │ ├── setup │ ├── console │ └── rake ├── spec │ ├── spec_helper.rb │ └── pix_key_spec.rb └── LICENSE.txt ├── me.json ├── .gitignore ├── prepare_to_submit.rb └── README.md /pix_key/.ruby-version: -------------------------------------------------------------------------------- 1 | 3.1.2 2 | -------------------------------------------------------------------------------- /pix_key/.tool-versions: -------------------------------------------------------------------------------- 1 | ruby 3.1.2 2 | -------------------------------------------------------------------------------- /pix_key/.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /pix_key/lib/pix_key.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | class PixKey 4 | end 5 | -------------------------------------------------------------------------------- /me.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"", 3 | "email":"", 4 | "github_url":"", 5 | "linkedin_url":"" 6 | } 7 | -------------------------------------------------------------------------------- /pix_key/Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task default: :spec 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /pix_key/coverage/ 2 | /pix_key/spec/reports/ 3 | /pix_key/spec/examples.txt 4 | /pix_key/tmp/ 5 | /pix_key/.rspec_status 6 | /pix_key/Gemfile.lock 7 | -------------------------------------------------------------------------------- /pix_key/Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | ruby "3.1.2" 6 | 7 | gem "rake", "~> 13.0" 8 | 9 | gem "rspec", "~> 3.0" 10 | -------------------------------------------------------------------------------- /pix_key/bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /pix_key/bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "pix_key" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require "irb" 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /pix_key/spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "pix_key" 4 | 5 | RSpec.configure do |config| 6 | # Enable flags like --only-failures and --next-failure 7 | config.example_status_persistence_file_path = ".rspec_status" 8 | 9 | # Disable RSpec exposing methods globally on `Module` and `main` 10 | config.disable_monkey_patching! 11 | 12 | config.expect_with :rspec do |c| 13 | c.syntax = :expect 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /pix_key/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'rake' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 12 | 13 | bundle_binstub = File.expand_path("bundle", __dir__) 14 | 15 | if File.file?(bundle_binstub) 16 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 17 | load(bundle_binstub) 18 | else 19 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 20 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 21 | end 22 | end 23 | 24 | require "rubygems" 25 | require "bundler/setup" 26 | 27 | load Gem.bin_path("rake", "rake") 28 | -------------------------------------------------------------------------------- /prepare_to_submit.rb: -------------------------------------------------------------------------------- 1 | require 'uri' 2 | require 'json' 3 | 4 | me_json = nil 5 | 6 | begin 7 | me_json = JSON.parse(File.read('./me.json')) 8 | rescue JSON::ParserError => exception 9 | message = 'O arquivo me.json contém um JSON inválido.' 10 | 11 | puts '-' * message.size 12 | puts message 13 | puts '-' * message.size 14 | puts 15 | puts "erro: #{exception.message}" 16 | end 17 | 18 | email = me_json.fetch("email") 19 | 20 | if email.match?(::URI::MailTo::EMAIL_REGEXP) 21 | bundle_name = email.tr('@', '').tr('.', '').downcase 22 | bundle_file = "#{bundle_name}.bundle" 23 | 24 | system("rm #{bundle_file}; git bundle create #{bundle_file} main") 25 | puts 26 | puts "Parabéns, faça upload do arquivo #{bundle_file} "\ 27 | "nesse form: https://forms.gle/2Bwqm9G3Mzv3nUaD7.\nBoa sorte!" 28 | else 29 | puts "Detectado email inválido no me.json\n" \ 30 | 'Por favor corrija-o e garanta que as outras informações estejam válidas.' 31 | end 32 | -------------------------------------------------------------------------------- /pix_key/LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Husky Tecnologia LTDA. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Husky Academy 2 | 3 | Olá seja bem-vindo(a) ao nosso teste técnico. 4 | 5 | ## Escopo 6 | 7 | O desafio consiste em fazer a implementação de um objeto capaz de representar uma chave PIX usando a [linguagem Ruby](https://www.ruby-lang.org/en/about/). 8 | 9 | Para isso você precisará ler a especificação oficial sobre os diferentes tipos de chaves no [DICT API](https://www.bcb.gov.br/content/estabilidadefinanceira/pix/API-DICT.html#tag/Key) e usar esse conhecimento para fazer os testes do arquivo `pix_key/spec/pix_key_spec.rb` passarem. 10 | 11 | ### Critérios de avaliação 12 | 13 | O teste foi pensando para avaliar os seguintes atributos: clareza, qualidade e eficiência do código. Por conta disso, tente entregar algo que atenda a esses critérios. 14 | 15 | ## Preparando ambiente de dev 16 | 17 | 1. Instale o [Ruby `3.1.2`](https://www.ruby-lang.org/en/documentation/installation/). 18 | 2. Faça um clone deste repositório: `git clone git@github.com:husky-misc/husky-academy.git` 19 | 3. Acesse a pasta `pix_key`. 20 | 4. Execute `bin/setup`. 21 | 22 | ## Executando os testes unitários 23 | 24 | 1. Acesse a pasta `pix_key`. 25 | 2. Execute `bin/rake`. 26 | 27 | ## Sobre a suite de testes 28 | 29 | Os testes foram escritos usando `Rspec`. Caso não conheça a ferramenta, acesse a [documentação oficial](https://relishapp.com/rspec/) ou procure por outros tutorias. 30 | 31 | **Observação:** O `Rspec` tem quatro módulos: `rspec-core`, `rspec-expectations`, `rspec-mocks` e `rspec-rails`. Apenas os dois primeiros são necessários para entender os testes. 32 | 33 | ## Submetendo o seu teste para avaliação 34 | 35 | 1. Preencha o `me.json` com os seus dados pessoais e faça o commit deste arquivo. 36 | 2. Garanta que a sua branch local `main` contenha seu último commit. 37 | 3. Execute o comando `ruby prepare_to_submit.rb` 38 | 4. Faça upload do arquivo com final `.bundle` nesse form: [https://forms.gle/2Bwqm9G3Mzv3nUaD7](https://forms.gle/2Bwqm9G3Mzv3nUaD7). 39 | 5. Boa sorte! o/ 40 | 41 | ### Atenção: 42 | 43 | 1. Não faça fork, nem torne o seu repositório público. 44 | > O __*Husky Academy*__ tem como objetivo capacitar as pessoas durante o processo seletivo, então, por favor, **não compartilhe seu código**. Permita que cada participante viva sua própria experiência de crescimento e aprendizagem. 45 | 2. Tenha certeza que o código está executável (sem erros de sintaxe) e que todos os testes estão passando. 46 | 3. Altere apenas os arquivos `pix_key/lib/pix_key.rb`, `me.json` e `Gemfile` caso precise adicionar uma dependência. 47 | > Todos esses arquivos precisam estar no git, caso contrário você poderá ser desclassificado(a). 48 | 4. **Apenas um upload será permitido**. Caso contrário, há o risco de avaliarmos a versão incorreta do seu código. 49 | -------------------------------------------------------------------------------- /pix_key/spec/pix_key_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | RSpec.describe PixKey do 6 | subject(:pix_key) { described_class.new(key) } 7 | 8 | let(:key) { 'email@husky.io' } 9 | 10 | describe '.new' do 11 | context 'with a valid key' do 12 | it { is_expected.to be_a(described_class) } 13 | 14 | it { is_expected.to be_valid } 15 | end 16 | 17 | context 'with an invalid key' do 18 | [1, nil, [], {}].each do |key| 19 | context "when key is #{key}" do 20 | let(:key) { key } 21 | 22 | it { is_expected.to be_a(described_class) } 23 | 24 | it { is_expected.to be_invalid } 25 | 26 | describe '#value' do 27 | subject { pix_key.value } 28 | 29 | it { is_expected.to be_a String } 30 | it { is_expected.to be_empty } 31 | it { is_expected.to be_frozen } 32 | end 33 | end 34 | end 35 | end 36 | end 37 | 38 | describe '#valid?' do 39 | context 'with valid keys' do 40 | [ 41 | 'email@husky.io', 42 | '12312312334', #cpf 43 | '12345678901234', # cnpj 44 | '+5510998765432', # phone 45 | '123e4567-e89b-12d3-a456-426655440000', #evp 46 | ] 47 | .map { |key| "#{['', ' '].sample}#{key}#{['', ' '].sample}" } 48 | .each do |key| 49 | context "when the key is #{key.inspect}" do 50 | it { is_expected.to be_valid } 51 | end 52 | end 53 | end 54 | 55 | context 'with invalid keys' do 56 | [ 57 | 'emailhusky.io', 58 | '1231231123122334', 59 | 'invalid', 60 | '551099876543299', # wrong phone 61 | '11123e45670sdsd00a-e21289b-12d3-a456-426655440000', # invalid evp 62 | ].each do |key| 63 | context "when the key is #{key}" do 64 | let(:key) { key } 65 | 66 | it { is_expected.to be_invalid } 67 | end 68 | end 69 | end 70 | end 71 | 72 | describe '#value' do 73 | let(:cpf_value) { "#{['', ' '].sample}01201201202#{['', ' '].sample}" } 74 | let(:cnpj_value) { "#{['', ' '].sample}75928551000119#{['', ' '].sample}" } 75 | let(:phone_value) { "#{['', ' '].sample}+55998822334#{['', ' '].sample}" } 76 | let(:email_value) { "#{['', ' '].sample}other.email@husky.io#{['', ' '].sample}" } 77 | let(:evp_value) { "#{['', ' '].sample}123e4567-e89b-12d3-a456-426655440000#{['', ' '].sample}" } 78 | 79 | let(:cpf) { described_class.new(cpf_value) } 80 | let(:cnpj) { described_class.new(cnpj_value) } 81 | let(:phone) { described_class.new(phone_value) } 82 | let(:email) { described_class.new(email_value) } 83 | let(:evp) { described_class.new(evp_value) } 84 | 85 | it 'returns the key' do 86 | [cpf, cnpj, phone, email, evp] 87 | .each { |pix_key| expect(pix_key.value) } 88 | 89 | expect(cpf.value).to eq '01201201202' 90 | expect(cnpj.value).to eq '75928551000119' 91 | expect(phone.value).to eq '+55998822334' 92 | expect(email.value).to eq 'other.email@husky.io' 93 | expect(evp.value).to eq '123e4567-e89b-12d3-a456-426655440000' 94 | 95 | expect(cpf.value.object_id).not_to eq cpf_value.object_id 96 | expect(cnpj.value.object_id).not_to eq cpf_value.object_id 97 | expect(phone.value.object_id).not_to eq cpf_value.object_id 98 | expect(email.value.object_id).not_to eq cpf_value.object_id 99 | expect(evp.value.object_id).not_to eq cpf_value.object_id 100 | end 101 | end 102 | 103 | describe '#key' do 104 | it 'is an alias to #value' do 105 | expect(pix_key.key).to be_frozen 106 | expect(pix_key.key).to eql(pix_key.value) 107 | end 108 | end 109 | 110 | describe '#to_s' do 111 | it 'returns the key' do 112 | expect(pix_key.value).to be_frozen 113 | expect(pix_key.to_s).to eql(key) 114 | end 115 | end 116 | 117 | describe '#phone?' do 118 | context 'when receive a valid phone' do 119 | [ 120 | '+55861107350', 121 | ].each do |key| 122 | context "when key is #{key}" do 123 | let(:key) { key } 124 | 125 | it { is_expected.to be_a_phone } 126 | end 127 | end 128 | end 129 | 130 | context 'when receive an invalid phone' do 131 | [ 132 | '55861107350', 133 | 'fernando@husky', 134 | '123e4567-e89b-12d3-a456-426655440000' 135 | ].each do |key| 136 | context "when key is #{key}" do 137 | it { is_expected.not_to be_a_phone } 138 | end 139 | end 140 | end 141 | end 142 | 143 | describe '#cpf?' do 144 | context 'when receive a valid cpf' do 145 | [ 146 | '86110735094', 147 | ].each do |key| 148 | context "when key is #{key}" do 149 | let(:key) { key } 150 | 151 | it { is_expected.to be_a_cpf } 152 | end 153 | end 154 | end 155 | 156 | context 'when receive an invalid cpf' do 157 | [ 158 | '86110735099', 159 | '861.107.350-94', 160 | '+5582998899889', 161 | 'fernando@husky', 162 | '123e4567-e89b-12d3-a456-426655440000' 163 | ].each do |key| 164 | context "when key is #{key}" do 165 | it { is_expected.not_to be_a_cpf } 166 | end 167 | end 168 | end 169 | end 170 | 171 | describe '#email?' do 172 | context 'when receive a valid email' do 173 | it { is_expected.to be_an_email } 174 | end 175 | 176 | context 'when receive an invalid email' do 177 | [ 178 | 'emailhusky.io', 179 | '75.928.551/0001-19', 180 | '861.107.350-94', 181 | '+5582998899889', 182 | '123e4567-e89b-12d3-a456-426655440000' 183 | ].each do |key| 184 | context "when email is #{key}" do 185 | let(:key) { key } 186 | 187 | it { is_expected.not_to be_an_email } 188 | end 189 | end 190 | end 191 | end 192 | 193 | describe '#cnpj?' do 194 | context 'when receive a valid cnpj' do 195 | [ 196 | '75928551000119' 197 | ].each do |key| 198 | context "when key is #{key}" do 199 | let(:key) { key } 200 | 201 | it { is_expected.to be_a_cnpj } 202 | end 203 | end 204 | end 205 | 206 | context 'when receive an invalid cnpj' do 207 | [ 208 | '75.928.551/0001-19', 209 | '75928551000118', 210 | '861.107.350-94', 211 | '+5582998899889', 212 | 'fernando@husky', 213 | '123e4567-e89b-12d3-a456-426655440000' 214 | ].each do |key| 215 | context "when key is #{key}" do 216 | it { is_expected.not_to be_a_cnpj } 217 | end 218 | end 219 | end 220 | end 221 | 222 | describe '#evp?' do 223 | context 'when receive a valid evp' do 224 | [ 225 | '123e4567-e89b-12d3-a456-426655440000' 226 | ].each do |key| 227 | context "when key is #{key}" do 228 | let(:key) { key } 229 | 230 | it { is_expected.to be_an_evp } 231 | end 232 | end 233 | end 234 | 235 | context 'when receive an invalid evp' do 236 | [ 237 | '75.928.551/0001-19', 238 | '861.107.350-94', 239 | '+5582998899889', 240 | 'fernando@husky', 241 | ].each do |key| 242 | context "when key is #{key}" do 243 | it { is_expected.not_to be_an_evp } 244 | end 245 | end 246 | end 247 | end 248 | 249 | describe '#type' do 250 | { 251 | 'cpf' => '86110735094', 252 | 'cnpj' => '75928551000119', 253 | 'phone' => '+55998822334', 254 | 'email' => 'email@husky.io', 255 | 'evp' => '123e4567-e89b-12d3-a456-426655440000' 256 | }.each do |type, key| 257 | context "when key is #{key}" do 258 | let(:key) { key } 259 | 260 | it "returns #{type}" do 261 | expect(pix_key.type).to eq(type) 262 | expect(pix_key.type).to be_frozen 263 | end 264 | end 265 | end 266 | end 267 | 268 | describe '#==' do 269 | let(:cpf) { described_class.new('01201201202') } 270 | let(:cnpj) { described_class.new('75928551000119') } 271 | let(:phone) { described_class.new('+55998822334') } 272 | let(:email) { described_class.new('other.email@husky.io') } 273 | let(:evp) { described_class.new('123e4567-e89b-12d3-a456-426655440000') } 274 | 275 | it 'verifies the key equality' do 276 | expect(cpf).to eq described_class.new('01201201202') 277 | expect(cnpj).to eq described_class.new('75928551000119') 278 | expect(phone).to eq described_class.new('+55998822334') 279 | expect(email).to eq described_class.new('other.email@husky.io') 280 | expect(evp).to eq described_class.new('123e4567-e89b-12d3-a456-426655440000') 281 | 282 | expect(email).not_to eq described_class.new('email@husky.io') 283 | 284 | expect(cpf).not_to eq cnpj 285 | expect(cnpj).not_to eq phone 286 | expect(phone).not_to eq email 287 | expect(email).not_to eq evp 288 | expect(evp).not_to eq cpf 289 | 290 | expect(cpf).not_to eq '01201201202' 291 | expect(cnpj).not_to eq '75928551000119' 292 | expect(phone).not_to eq '+55998822334' 293 | expect(email).not_to eq 'other.email@husky.io' 294 | expect(evp).not_to eq '123e4567-e89b-12d3-a456-426655440000' 295 | end 296 | end 297 | end 298 | --------------------------------------------------------------------------------