minato-utils 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +28 -0
- data/.gitignore +1 -0
- data/.gitlab-ci.yml +40 -0
- data/.rubocop.yml +30 -0
- data/.ruby-version +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +109 -0
- data/README.md +68 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/minato/utils/client.rb +51 -0
- data/lib/minato/utils/configuration.rb +26 -0
- data/lib/minato/utils/exception.rb +45 -0
- data/lib/minato/utils/helpers.rb +19 -0
- data/lib/minato/utils/logger.rb +76 -0
- data/lib/minato/utils/response_handler.rb +47 -0
- data/lib/minato/utils/version.rb +7 -0
- data/lib/minato/utils.rb +12 -0
- data/minato-utils.gemspec +44 -0
- metadata +232 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 01670db07941c0dc68be0ce9c9224f94d88c53cd5c5ec68683da61c583849fcb
|
4
|
+
data.tar.gz: 3583a272ed589e7016bb37028e5b9caeacbbcc333377d9eeb99d07f210f1596c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf8d6b71df86cd3fad67ce1e5023c8bdc3236b2483a9ac4eda01b6ee15cc0cdbaf287cf70083c001be9f30ece77334e7f40a2d48033cdef15ad21aa8978072e0
|
7
|
+
data.tar.gz: 4b534d3b50d1a4ac7d7520642036798bcb75cc90c0676d00968a152152061a8caf7bb8ce4f1c5d5c636b7e3c43de4f67259b775b9d1b8b9e15df19e6cf9f94cc
|
data/.editorconfig
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# EditorConfig is awesome: https://EditorConfig.org
|
2
|
+
|
3
|
+
# top-most EditorConfig file
|
4
|
+
root = true
|
5
|
+
|
6
|
+
# Unix-style newlines with a newline ending every file
|
7
|
+
[*]
|
8
|
+
end_of_line = lf
|
9
|
+
insert_final_newline = true
|
10
|
+
|
11
|
+
# Matches multiple files with brace expansion notation
|
12
|
+
# Set default charset
|
13
|
+
[*.{js,py}]
|
14
|
+
charset = utf-8
|
15
|
+
|
16
|
+
# 4 space indentation
|
17
|
+
[*.rb]
|
18
|
+
indent_style = space
|
19
|
+
indent_size = 2
|
20
|
+
|
21
|
+
# Tab indentation (no size specified)
|
22
|
+
[Makefile]
|
23
|
+
indent_style = tab
|
24
|
+
|
25
|
+
# Matches the exact files either package.json or .travis.yml
|
26
|
+
[{package.json,.travis.yml}]
|
27
|
+
indent_style = space
|
28
|
+
indent_size = 2
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
minato-utils-*.gem
|
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
image: docker:latest
|
2
|
+
|
3
|
+
stages:
|
4
|
+
- test
|
5
|
+
|
6
|
+
variables:
|
7
|
+
IMAGE_REF: "$IMAGE:$CI_COMMIT_SHORT_SHA"
|
8
|
+
KEY_FILE: "gcloud-service-key.json"
|
9
|
+
|
10
|
+
.cmd_google_key: &cmd_google_key |-
|
11
|
+
echo $GCLOUD_SERVICE_KEY | base64 -d > $KEY_FILE
|
12
|
+
|
13
|
+
.cmd_auth_docker: &cmd_auth_docker |-
|
14
|
+
cat $KEY_FILE | docker login -u _json_key --password-stdin https://gcr.io
|
15
|
+
|
16
|
+
.auth_docker: &auth_docker
|
17
|
+
before_script:
|
18
|
+
- *cmd_google_key
|
19
|
+
- *cmd_auth_docker
|
20
|
+
|
21
|
+
rspec:
|
22
|
+
<<: *auth_docker
|
23
|
+
stage: test
|
24
|
+
image: ruby:2.7.3
|
25
|
+
only:
|
26
|
+
- merge_requests
|
27
|
+
- dev
|
28
|
+
- main
|
29
|
+
cache:
|
30
|
+
paths:
|
31
|
+
- vendor/ruby
|
32
|
+
variables:
|
33
|
+
RAILS_ENV: test
|
34
|
+
BASE_PATH: /minato-ruby-utils
|
35
|
+
before_script:
|
36
|
+
- gem install bundler --no-document
|
37
|
+
- bundle config set --local path 'vendor/ruby'
|
38
|
+
- bundle install -j $(nproc)
|
39
|
+
script:
|
40
|
+
- bundle exec rspec
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rails
|
3
|
+
- rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
DisplayCopNames: true
|
7
|
+
DisplayStyleGuide: true
|
8
|
+
ExtraDetails: false
|
9
|
+
NewCops: enable
|
10
|
+
TargetRubyVersion: 2.7.3
|
11
|
+
|
12
|
+
Metrics/BlockLength:
|
13
|
+
IgnoredMethods: ['describe', 'it', 'context']
|
14
|
+
Max: 50
|
15
|
+
|
16
|
+
RSpec/EmptyExampleGroup:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
RSpec/ExampleLength:
|
20
|
+
Max: 50
|
21
|
+
|
22
|
+
RSpec/ImplicitExpect:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/Documentation:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Layout/HashAlignment:
|
29
|
+
EnforcedColonStyle: table
|
30
|
+
EnforcedHashRocketStyle: table
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.3
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
minato-utils (0.1.1)
|
5
|
+
activesupport (~> 6.0)
|
6
|
+
httparty (~> 0.18)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (6.1.4.1)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (>= 1.6, < 2)
|
14
|
+
minitest (>= 5.1)
|
15
|
+
tzinfo (~> 2.0)
|
16
|
+
zeitwerk (~> 2.3)
|
17
|
+
addressable (2.8.0)
|
18
|
+
public_suffix (>= 2.0.2, < 5.0)
|
19
|
+
ast (2.4.2)
|
20
|
+
coderay (1.1.3)
|
21
|
+
concurrent-ruby (1.1.9)
|
22
|
+
crack (0.4.5)
|
23
|
+
rexml
|
24
|
+
diff-lcs (1.4.4)
|
25
|
+
hashdiff (1.0.1)
|
26
|
+
httparty (0.20.0)
|
27
|
+
mime-types (~> 3.0)
|
28
|
+
multi_xml (>= 0.5.2)
|
29
|
+
i18n (1.8.11)
|
30
|
+
concurrent-ruby (~> 1.0)
|
31
|
+
json (2.6.1)
|
32
|
+
method_source (1.0.0)
|
33
|
+
mime-types (3.4.1)
|
34
|
+
mime-types-data (~> 3.2015)
|
35
|
+
mime-types-data (3.2021.1115)
|
36
|
+
minitest (5.14.4)
|
37
|
+
multi_xml (0.6.0)
|
38
|
+
parallel (1.21.0)
|
39
|
+
parser (3.0.2.0)
|
40
|
+
ast (~> 2.4.1)
|
41
|
+
pry (0.14.1)
|
42
|
+
coderay (~> 1.1)
|
43
|
+
method_source (~> 1.0)
|
44
|
+
public_suffix (4.0.6)
|
45
|
+
rack (2.2.3)
|
46
|
+
rainbow (3.0.0)
|
47
|
+
rake (10.5.0)
|
48
|
+
regexp_parser (2.1.1)
|
49
|
+
rexml (3.2.5)
|
50
|
+
rspec (3.10.0)
|
51
|
+
rspec-core (~> 3.10.0)
|
52
|
+
rspec-expectations (~> 3.10.0)
|
53
|
+
rspec-mocks (~> 3.10.0)
|
54
|
+
rspec-core (3.10.1)
|
55
|
+
rspec-support (~> 3.10.0)
|
56
|
+
rspec-expectations (3.10.1)
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
+
rspec-support (~> 3.10.0)
|
59
|
+
rspec-mocks (3.10.2)
|
60
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
61
|
+
rspec-support (~> 3.10.0)
|
62
|
+
rspec-support (3.10.3)
|
63
|
+
rubocop (1.23.0)
|
64
|
+
parallel (~> 1.10)
|
65
|
+
parser (>= 3.0.0.0)
|
66
|
+
rainbow (>= 2.2.2, < 4.0)
|
67
|
+
regexp_parser (>= 1.8, < 3.0)
|
68
|
+
rexml
|
69
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
70
|
+
ruby-progressbar (~> 1.7)
|
71
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
72
|
+
rubocop-ast (1.13.0)
|
73
|
+
parser (>= 3.0.1.1)
|
74
|
+
rubocop-rails (2.12.4)
|
75
|
+
activesupport (>= 4.2.0)
|
76
|
+
rack (>= 1.1)
|
77
|
+
rubocop (>= 1.7.0, < 2.0)
|
78
|
+
rubocop-rake (0.6.0)
|
79
|
+
rubocop (~> 1.0)
|
80
|
+
rubocop-rspec (2.6.0)
|
81
|
+
rubocop (~> 1.19)
|
82
|
+
ruby-progressbar (1.11.0)
|
83
|
+
tzinfo (2.0.4)
|
84
|
+
concurrent-ruby (~> 1.0)
|
85
|
+
unicode-display_width (2.1.0)
|
86
|
+
webmock (3.14.0)
|
87
|
+
addressable (>= 2.8.0)
|
88
|
+
crack (>= 0.3.2)
|
89
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
90
|
+
zeitwerk (2.5.1)
|
91
|
+
|
92
|
+
PLATFORMS
|
93
|
+
x86_64-linux
|
94
|
+
|
95
|
+
DEPENDENCIES
|
96
|
+
bundler (~> 2.2)
|
97
|
+
json (~> 2.6)
|
98
|
+
minato-utils!
|
99
|
+
pry (~> 0.14)
|
100
|
+
rake (~> 10.0)
|
101
|
+
rspec (~> 3.0)
|
102
|
+
rubocop (~> 1.23.0)
|
103
|
+
rubocop-rails (~> 2.12.4)
|
104
|
+
rubocop-rake (~> 0.6.0)
|
105
|
+
rubocop-rspec (~> 2.6.0)
|
106
|
+
webmock (~> 3.14)
|
107
|
+
|
108
|
+
BUNDLED WITH
|
109
|
+
2.2.31
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Minato Ruby Utils
|
2
|
+
|
3
|
+
Lib feita para agrupar os códigos comuns dos serviços da Ferreri.
|
4
|
+
Atualmente possui um cliente HTTP e alguns métodos para ajudar no
|
5
|
+
desenvolvimento das aplicações.
|
6
|
+
|
7
|
+
## Instalação
|
8
|
+
|
9
|
+
O método recomendado para instalação da lib é via `gem`:
|
10
|
+
|
11
|
+
$ gem install minato-utils
|
12
|
+
|
13
|
+
Ou, alternativamente, via `Gemfile`:
|
14
|
+
|
15
|
+
$ gem 'minato-utils', '~> 0.1.0'
|
16
|
+
|
17
|
+
## Uso
|
18
|
+
|
19
|
+
Para usar esta lib você precisa adicionar o seguinte require em um arquivo de
|
20
|
+
inicialização `config/initializers/minato_utils.rb`:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'minato/utils'
|
24
|
+
```
|
25
|
+
|
26
|
+
### Minato::Utils::Helpers
|
27
|
+
|
28
|
+
Join urls com:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
Minato::Utils::Helpers.join_url(*segments)
|
32
|
+
```
|
33
|
+
|
34
|
+
Parse json com:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Minato::Utils::Helpers.parse_json(body)
|
38
|
+
```
|
39
|
+
|
40
|
+
### Minato::Utils::Client
|
41
|
+
|
42
|
+
Para usar o cliente você vai precisar iniciar ele com `base_uri` e
|
43
|
+
`default_options`:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
client = Minato::Utils::Client.new(base_uri: 'http://www.test.com', default_options: headers: { 'Content-type' => 'application/json' })
|
47
|
+
```
|
48
|
+
|
49
|
+
Você pode usar todos os métodos HTTP para fazer requisições.
|
50
|
+
Você também pode adicionar mais opções ou sobrescrever o padrão usando os
|
51
|
+
métodos params:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
client.get('/path', headers: { 'X-Session-Token' => '123' })
|
55
|
+
```
|
56
|
+
|
57
|
+
As falhas de solicitação irão gerar uma exceção do tipo `Minato::Utils::<Exception>`.
|
58
|
+
As possíveis exceções são:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Minato::Utils::BadRequest
|
62
|
+
Minato::Utils::Conflict
|
63
|
+
Minato::Utils::Forbiden
|
64
|
+
Minato::Utils::NotFound
|
65
|
+
Minato::Utils::ServerError
|
66
|
+
Minato::Utils::Unauthorized
|
67
|
+
Minato::Utils::UnprocessableEntity
|
68
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# !/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'common'
|
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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
require_relative 'logger'
|
5
|
+
require_relative 'response_handler'
|
6
|
+
|
7
|
+
module Minato
|
8
|
+
module Utils
|
9
|
+
class Client
|
10
|
+
include HTTParty
|
11
|
+
|
12
|
+
def initialize(base_uri:, default_options: {})
|
13
|
+
@base_uri = base_uri
|
14
|
+
@default_options = default_options
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(path, options = {})
|
18
|
+
request(:get, path, options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def post(path, options = {})
|
22
|
+
request(:post, path, options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(path, options = {})
|
26
|
+
request(:put, path, options)
|
27
|
+
end
|
28
|
+
|
29
|
+
def patch(path, options = {})
|
30
|
+
request(:patch, path, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete(path, options = {})
|
34
|
+
request(:delete, path, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def request(method, path, options)
|
40
|
+
request_options = @default_options.deep_merge(options)
|
41
|
+
url = Minato::Utils::Helpers.join_url(@base_uri, path)
|
42
|
+
|
43
|
+
Minato::Utils.logger.log_request(method, @base_uri, path, request_options)
|
44
|
+
response = self.class.send(method, url, request_options)
|
45
|
+
|
46
|
+
Minato::Utils.logger.log_response(method, @base_uri, path, request_options, response)
|
47
|
+
Minato::Utils::ResponseHandler.handle(response)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minato
|
4
|
+
module Utils
|
5
|
+
class Configuration
|
6
|
+
attr_accessor :logger, :debug
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@logger = nil
|
10
|
+
@debug = false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module_function
|
15
|
+
|
16
|
+
attr_writer :config
|
17
|
+
|
18
|
+
def configure
|
19
|
+
yield(config)
|
20
|
+
end
|
21
|
+
|
22
|
+
def config
|
23
|
+
@config ||= Configuration.new
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minato
|
4
|
+
module Utils
|
5
|
+
class SerializableError < StandardError
|
6
|
+
attr_reader :id, :code, :body
|
7
|
+
|
8
|
+
def initialize(object)
|
9
|
+
@id = self.class.to_s.underscore
|
10
|
+
@body = object.body
|
11
|
+
@code = object.code
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class SerializableException < SerializableError; end
|
17
|
+
|
18
|
+
class NetworkException < SerializableError
|
19
|
+
def initialize(response)
|
20
|
+
super(response)
|
21
|
+
@body = Minato::Utils::Helpers.parse_json(response.body)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class BadGateway < NetworkException; end
|
26
|
+
|
27
|
+
class BadRequest < NetworkException; end
|
28
|
+
|
29
|
+
class Conflict < NetworkException; end
|
30
|
+
|
31
|
+
class Forbidden < NetworkException; end
|
32
|
+
|
33
|
+
class Gone < NetworkException; end
|
34
|
+
|
35
|
+
class NotFound < NetworkException; end
|
36
|
+
|
37
|
+
class ServerError < NetworkException; end
|
38
|
+
|
39
|
+
class ServiceUnavailable < NetworkException; end
|
40
|
+
|
41
|
+
class Unauthorized < NetworkException; end
|
42
|
+
|
43
|
+
class UnprocessableEntity < NetworkException; end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minato
|
4
|
+
module Utils
|
5
|
+
module Helpers
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def join_url(*segments)
|
9
|
+
segments
|
10
|
+
.map { |seg| seg.gsub(%r{/$|^/}, '') }
|
11
|
+
.join('/')
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_json(body)
|
15
|
+
body.present? ? JSON.parse(body, symbolize_names: true) : {}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'configuration'
|
4
|
+
|
5
|
+
module Minato
|
6
|
+
module Utils
|
7
|
+
class LogFormatter < ::Logger::Formatter
|
8
|
+
def call(severity, timestamp, _program_name, data)
|
9
|
+
json_str = {
|
10
|
+
type: severity,
|
11
|
+
time: timestamp,
|
12
|
+
message: data.delete(:message)
|
13
|
+
}.merge(data).to_json
|
14
|
+
|
15
|
+
"#{json_str}\r\n"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class BaseLogger
|
20
|
+
def initialize(logger)
|
21
|
+
@logger = logger
|
22
|
+
@logger.formatter = LogFormatter.new if @logger
|
23
|
+
@debug = Minato::Utils.config.debug
|
24
|
+
end
|
25
|
+
|
26
|
+
def log_request(method, host, path, options)
|
27
|
+
return unless @logger
|
28
|
+
|
29
|
+
call :info,
|
30
|
+
message: 'Minato::Utils::Client starting request',
|
31
|
+
http_request: request_data(method, host, path, options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def log_response(method, host, path, options, response)
|
35
|
+
return unless @logger
|
36
|
+
|
37
|
+
call :info,
|
38
|
+
message: 'Minato::Utils::Client receiving response',
|
39
|
+
http_request: request_data(method, host, path, options),
|
40
|
+
http_response: response_data(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
def call(type, message)
|
44
|
+
@logger.send type, message
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def request_data(method, host, path, options)
|
50
|
+
data = {
|
51
|
+
method: method,
|
52
|
+
host: host,
|
53
|
+
path: path
|
54
|
+
}
|
55
|
+
|
56
|
+
data[:query] = options[:query] if options[:query]
|
57
|
+
@debug ? data.merge({ headers: options[:headers] }) : data
|
58
|
+
end
|
59
|
+
|
60
|
+
def response_data(response)
|
61
|
+
data = {
|
62
|
+
code: response.code,
|
63
|
+
message: response.message
|
64
|
+
}
|
65
|
+
|
66
|
+
@debug ? data.merge({ headers: response.headers, body: response.body }) : data
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module_function
|
71
|
+
|
72
|
+
def logger
|
73
|
+
@logger ||= BaseLogger.new(Minato::Utils.config.logger.dup)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext/enumerable'
|
5
|
+
|
6
|
+
module Minato
|
7
|
+
module Utils
|
8
|
+
class ResponseHandler
|
9
|
+
class << self
|
10
|
+
def handle(response)
|
11
|
+
raise_exception(response) if response_error?(response)
|
12
|
+
|
13
|
+
Minato::Utils::Helpers.parse_json(response.body)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
SUCCESSFUL_RESPONSE_CODE = [200, 201, 204].freeze
|
19
|
+
|
20
|
+
EXCEPTION_HANDLERS_MAP = {
|
21
|
+
400 => 'bad_request',
|
22
|
+
401 => 'unauthorized',
|
23
|
+
403 => 'forbidden',
|
24
|
+
404 => 'not_found',
|
25
|
+
409 => 'conflict',
|
26
|
+
410 => 'gone',
|
27
|
+
422 => 'unprocessable_entity',
|
28
|
+
500 => 'server_error',
|
29
|
+
502 => 'bad_gateway',
|
30
|
+
503 => 'service_unavailable'
|
31
|
+
}.freeze
|
32
|
+
|
33
|
+
def response_error?(response)
|
34
|
+
SUCCESSFUL_RESPONSE_CODE.exclude?(response.code)
|
35
|
+
end
|
36
|
+
|
37
|
+
def raise_exception(response)
|
38
|
+
raise get_exception_class_from(response.code), response
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_exception_class_from(code)
|
42
|
+
"Minato::Utils::#{EXCEPTION_HANDLERS_MAP[code].camelize}".constantize
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/minato/utils.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path('lib', __dir__)
|
4
|
+
require 'minato/utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'minato-utils'
|
8
|
+
spec.version = Minato::Utils::VERSION
|
9
|
+
spec.authors = ['Ferreri']
|
10
|
+
spec.email = ['contato@ferreri.co']
|
11
|
+
spec.homepage = 'https://gitlab.com/ferreri/minato-projects/minato-ruby-utils'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.summary = 'Minato Ruby Utils'
|
14
|
+
spec.description = 'common code across services'
|
15
|
+
|
16
|
+
spec.required_ruby_version = '>= 2.7'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_dependency 'activesupport', '~> 6.0'
|
28
|
+
spec.add_dependency 'httparty', '~> 0.18'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 2.2'
|
31
|
+
spec.add_development_dependency 'json', '~> 2.6'
|
32
|
+
spec.add_development_dependency 'pry', '~> 0.14'
|
33
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
spec.add_development_dependency 'rubocop', '~> 1.23.0'
|
36
|
+
spec.add_development_dependency 'rubocop-rails', '~> 2.12.4'
|
37
|
+
spec.add_development_dependency 'rubocop-rake', '~> 0.6.0'
|
38
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 2.6.0'
|
39
|
+
spec.add_development_dependency 'webmock', '~> 3.14'
|
40
|
+
|
41
|
+
spec.metadata = {
|
42
|
+
'rubygems_mfa_required' => 'true'
|
43
|
+
}
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,232 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minato-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ferreri
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.18'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.18'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.6'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.14'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.14'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.23.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.23.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rails
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 2.12.4
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 2.12.4
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop-rake
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.6.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.6.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rubocop-rspec
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 2.6.0
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 2.6.0
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: webmock
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '3.14'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '3.14'
|
181
|
+
description: common code across services
|
182
|
+
email:
|
183
|
+
- contato@ferreri.co
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".editorconfig"
|
189
|
+
- ".gitignore"
|
190
|
+
- ".gitlab-ci.yml"
|
191
|
+
- ".rubocop.yml"
|
192
|
+
- ".ruby-version"
|
193
|
+
- Gemfile
|
194
|
+
- Gemfile.lock
|
195
|
+
- README.md
|
196
|
+
- Rakefile
|
197
|
+
- bin/console
|
198
|
+
- bin/setup
|
199
|
+
- lib/minato/utils.rb
|
200
|
+
- lib/minato/utils/client.rb
|
201
|
+
- lib/minato/utils/configuration.rb
|
202
|
+
- lib/minato/utils/exception.rb
|
203
|
+
- lib/minato/utils/helpers.rb
|
204
|
+
- lib/minato/utils/logger.rb
|
205
|
+
- lib/minato/utils/response_handler.rb
|
206
|
+
- lib/minato/utils/version.rb
|
207
|
+
- minato-utils.gemspec
|
208
|
+
homepage: https://gitlab.com/ferreri/minato-projects/minato-ruby-utils
|
209
|
+
licenses:
|
210
|
+
- MIT
|
211
|
+
metadata:
|
212
|
+
rubygems_mfa_required: 'true'
|
213
|
+
post_install_message:
|
214
|
+
rdoc_options: []
|
215
|
+
require_paths:
|
216
|
+
- lib
|
217
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
218
|
+
requirements:
|
219
|
+
- - ">="
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: '2.7'
|
222
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
223
|
+
requirements:
|
224
|
+
- - ">="
|
225
|
+
- !ruby/object:Gem::Version
|
226
|
+
version: '0'
|
227
|
+
requirements: []
|
228
|
+
rubygems_version: 3.1.6
|
229
|
+
signing_key:
|
230
|
+
specification_version: 4
|
231
|
+
summary: Minato Ruby Utils
|
232
|
+
test_files: []
|