interage-request 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f3158ec69ab8db993365e3d5da65aff8b2e3276ceef11f641eedf6cdb675227
4
- data.tar.gz: a673d489c9cab26eddc199209f0cc1e1168ad17905f3107806e5cc17e16e547d
3
+ metadata.gz: 15230f74201e69eb0c6bff7855b00da6a604e3b4a67936e86e710aed76d16c24
4
+ data.tar.gz: 1b7720990fc7eebe2969ae87849b7c104b7f036e6da3762772a5676ec4c1abd4
5
5
  SHA512:
6
- metadata.gz: 2d816277fa807a02aba6df711c27dd23734fadf0136c0a1dcd542173bb1171b8dda2e6b945a22b9d337cd2ab346ffc393224650a27f9b024051f53b474219f32
7
- data.tar.gz: 12ffc905dcd220155fa052ba7715664dca7b1577262f4104e062e4423f3094a32f050bbddfdf0607676da81471e3ccc7b3c4323ee062a64a71f5fe718ec9385a
6
+ metadata.gz: bc3e902e3885513ee2d108d2c53c8aa661febd3e5a180592cf9566c21bded357a043d0b4cb4871efa8f94a7605e428e9967a89bee1e3233ce8922c7acd91e95e
7
+ data.tar.gz: 1a0b8b1e2f0292eefe40b50b707c426c50af2f55dd42a9214a2088a2ddebb5a5d6ea14d3cfcfec83812059969141393def99fb496e451bea859e7b9aa9346676
data/.gitignore CHANGED
@@ -9,3 +9,6 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ gemfiles/*.lock
13
+ interage-request-*.gem
14
+ README-BKP.md
@@ -2,7 +2,7 @@ require:
2
2
  - rubocop-rspec
3
3
  - rubocop-performance
4
4
 
5
- Documentation:
5
+ Style/Documentation:
6
6
  Enabled: false
7
7
 
8
8
  Style/EmptyMethod:
@@ -14,6 +14,27 @@ Style/SymbolArray:
14
14
  Style/WordArray:
15
15
  Enabled: false
16
16
 
17
+ Layout/SpaceAroundMethodCallOperator:
18
+ Enabled: true
19
+
20
+ Lint/RaiseException:
21
+ Enabled: true
22
+
23
+ Lint/StructNewOverride:
24
+ Enabled: true
25
+
26
+ Style/ExponentialNotation:
27
+ Enabled: true
28
+
29
+ Style/HashEachMethods:
30
+ Enabled: true
31
+
32
+ Style/HashTransformKeys:
33
+ Enabled: true
34
+
35
+ Style/HashTransformValues:
36
+ Enabled: true
37
+
17
38
  AllCops:
18
39
  TargetRubyVersion: 2.6
19
40
  Exclude:
data/CHANGES CHANGED
@@ -1,3 +1,33 @@
1
+ Version 0.2.0:
2
+ - Cria binário de publicar gem
3
+ - Atualiza Documentação
4
+ - Ajusta binário do CI
5
+ - Altera url do repositório git
6
+ - Ajusta README
7
+ - Atualiza documentação
8
+ - Ajusta geradores e adiciona form
9
+ - Adiciona gem empacotadas no gitignore
10
+ - Refatora a classe ApplicationRequest
11
+ - Atualiza arquivo de versão
12
+ - Atualiza dependencias de desenvolvimento
13
+
14
+ Version 0.1.3:
15
+ - Altera arquivos VERSION e CHANGES para 0.1.3
16
+ - Empacota nova versão da gem
17
+ - Corrige arquivo de versões
18
+ - Adiciona parametro 'uri' que estava faltando
19
+ - Gera nova versão da gem
20
+
21
+ Version 0.1.2:
22
+ - Altera arquivos VERSION e CHANGES para 0.1.2
23
+ - Atualiza version.rb
24
+ - Corrige atributo no método update
25
+ - Adiciona verificação de SSL como parametro opciona
26
+
27
+ Version 0.1.1:
28
+ - Empacota a versão 0.1.1
29
+ - Verifica se a resposta do find não é nula
30
+
1
31
  Version 0.1.0
2
32
  - Renomea e reimplmenta gem para application request
3
33
  - Adiciona comando dos geradores na documentação
data/Gemfile CHANGED
@@ -6,17 +6,17 @@ gemspec
6
6
 
7
7
  group :development, :test do
8
8
  # https://github.com/rubocop-hq/rubocop#quickstart
9
- gem 'rubocop', '~> 0.67.2'
9
+ gem 'rubocop', '~> 0.82.0'
10
10
  # https://github.com/backus/rubocop-rspec
11
- gem 'rubocop-rspec', '~> 1.32'
11
+ gem 'rubocop-rspec', '~> 1.39'
12
12
  # https://github.com/rubocop-hq/rubocop-performance/#usage
13
- gem 'rubocop-performance', '~> 1.1'
13
+ gem 'rubocop-performance', '~> 1.5', '>= 1.5.2'
14
14
  # https://github.com/rspec/rspec#install
15
- gem 'rspec', '~> 3.8'
15
+ gem 'rspec', '~> 3.9'
16
16
  # https://github.com/pry/pry#installation
17
- gem 'pry', '~> 0.12.2'
17
+ gem 'pry', '~> 0.13.1'
18
18
  # https://github.com/presidentbeef/brakeman
19
- gem 'brakeman', '~> 4.3', '>= 4.3.1', require: false
19
+ gem 'brakeman', '~> 4.8', '>= 4.8.1', require: false
20
20
  # https://github.com/rails/rails/tree/master/activesupport#download-and-installation
21
- gem 'activesupport', '~> 5.2', '>= 5.2.3'
21
+ gem 'activesupport', '~> 6.0'
22
22
  end
@@ -1,80 +1,82 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- interage-request (0.1.0)
4
+ interage-request (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activesupport (5.2.3)
9
+ activesupport (6.0.3.1)
10
10
  concurrent-ruby (~> 1.0, >= 1.0.2)
11
11
  i18n (>= 0.7, < 2)
12
12
  minitest (~> 5.1)
13
13
  tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.2, >= 2.2.2)
14
15
  ast (2.4.0)
15
- brakeman (4.5.0)
16
+ brakeman (4.8.1)
16
17
  coderay (1.1.2)
17
- concurrent-ruby (1.1.5)
18
+ concurrent-ruby (1.1.6)
18
19
  diff-lcs (1.3)
19
- i18n (1.6.0)
20
+ i18n (1.8.2)
20
21
  concurrent-ruby (~> 1.0)
21
- jaro_winkler (1.5.2)
22
- method_source (0.9.2)
23
- minitest (5.11.3)
24
- parallel (1.17.0)
25
- parser (2.6.2.1)
22
+ jaro_winkler (1.5.4)
23
+ method_source (1.0.0)
24
+ minitest (5.14.1)
25
+ parallel (1.19.1)
26
+ parser (2.7.1.2)
26
27
  ast (~> 2.4.0)
27
- pry (0.12.2)
28
- coderay (~> 1.1.0)
29
- method_source (~> 0.9.0)
30
- psych (3.1.0)
28
+ pry (0.13.1)
29
+ coderay (~> 1.1)
30
+ method_source (~> 1.0)
31
31
  rainbow (3.0.0)
32
- rake (10.5.0)
33
- rspec (3.8.0)
34
- rspec-core (~> 3.8.0)
35
- rspec-expectations (~> 3.8.0)
36
- rspec-mocks (~> 3.8.0)
37
- rspec-core (3.8.0)
38
- rspec-support (~> 3.8.0)
39
- rspec-expectations (3.8.2)
32
+ rake (13.0.1)
33
+ rexml (3.2.4)
34
+ rspec (3.9.0)
35
+ rspec-core (~> 3.9.0)
36
+ rspec-expectations (~> 3.9.0)
37
+ rspec-mocks (~> 3.9.0)
38
+ rspec-core (3.9.2)
39
+ rspec-support (~> 3.9.3)
40
+ rspec-expectations (3.9.1)
40
41
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.8.0)
42
- rspec-mocks (3.8.0)
42
+ rspec-support (~> 3.9.0)
43
+ rspec-mocks (3.9.1)
43
44
  diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.8.0)
45
- rspec-support (3.8.0)
46
- rubocop (0.67.2)
45
+ rspec-support (~> 3.9.0)
46
+ rspec-support (3.9.3)
47
+ rubocop (0.82.0)
47
48
  jaro_winkler (~> 1.5.1)
48
49
  parallel (~> 1.10)
49
- parser (>= 2.5, != 2.5.1.1)
50
- psych (>= 3.1.0)
50
+ parser (>= 2.7.0.1)
51
51
  rainbow (>= 2.2.2, < 4.0)
52
+ rexml
52
53
  ruby-progressbar (~> 1.7)
53
- unicode-display_width (>= 1.4.0, < 1.6)
54
- rubocop-performance (1.1.0)
55
- rubocop (>= 0.67.0)
56
- rubocop-rspec (1.32.0)
57
- rubocop (>= 0.60.0)
58
- ruby-progressbar (1.10.0)
54
+ unicode-display_width (>= 1.4.0, < 2.0)
55
+ rubocop-performance (1.5.2)
56
+ rubocop (>= 0.71.0)
57
+ rubocop-rspec (1.39.0)
58
+ rubocop (>= 0.68.1)
59
+ ruby-progressbar (1.10.1)
59
60
  thread_safe (0.3.6)
60
- tzinfo (1.2.5)
61
+ tzinfo (1.2.7)
61
62
  thread_safe (~> 0.1)
62
- unicode-display_width (1.5.0)
63
+ unicode-display_width (1.7.0)
64
+ zeitwerk (2.3.0)
63
65
 
64
66
  PLATFORMS
65
67
  ruby
66
68
 
67
69
  DEPENDENCIES
68
- activesupport (~> 5.2, >= 5.2.3)
69
- brakeman (~> 4.3, >= 4.3.1)
70
+ activesupport (~> 6.0)
71
+ brakeman (~> 4.8, >= 4.8.1)
70
72
  bundler (~> 2.0)
71
73
  interage-request!
72
- pry (~> 0.12.2)
73
- rake (~> 10.0)
74
- rspec (~> 3.8)
75
- rubocop (~> 0.67.2)
76
- rubocop-performance (~> 1.1)
77
- rubocop-rspec (~> 1.32)
74
+ pry (~> 0.13.1)
75
+ rake (~> 13.0)
76
+ rspec (~> 3.9)
77
+ rubocop (~> 0.82.0)
78
+ rubocop-performance (~> 1.5, >= 1.5.2)
79
+ rubocop-rspec (~> 1.39)
78
80
 
79
81
  BUNDLED WITH
80
- 2.0.1
82
+ 2.1.4
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Add this line to your application's Gemfile:
6
6
 
7
7
  ```ruby
8
- gem 'interage-request', '~> 0.1'
8
+ gem 'interage-request', '~> 0.2'
9
9
  ```
10
10
 
11
11
  And then execute:
@@ -22,19 +22,150 @@ gem install interage-request
22
22
 
23
23
  ## Usage
24
24
 
25
- You can use a Rails generator to create `ApplicationRequest`:
25
+ You can use a Rails generator to install:
26
26
 
27
27
  ```bash
28
28
  rails g interage:request:install
29
29
  ```
30
30
 
31
- And you can use a Rails generator to create request classes:
31
+ Or you can create the `ApplicationRequest`:
32
+
33
+ ```ruby
34
+ # app/requests/application_request.rb
35
+ class ApplicationRequest < ::Interage::ApplicationRequest
36
+ end
37
+ ```
38
+
39
+ And `ApplicationBuilder`
40
+
41
+
42
+ ```ruby
43
+ # app/builders/application_builder.rb
44
+ class ApplicationBuilder < ::Interage::ApplicationBuilder
45
+ end
46
+ ```
47
+
48
+ To create a request and builder classes you also can use a Rails generator:
32
49
 
33
50
 
34
51
  ```bash
35
- rails g interage:request:create CustomersByDocument Customer
52
+ rails g interage:request:create store/order client_name payment_form
53
+ ```
54
+
55
+ This will create this classes:
56
+
57
+ ```ruby
58
+ # app/requests/store/base_request.rb
59
+ module Store
60
+ class BaseRequest < ::ApplicationRequest
61
+ private
62
+
63
+ def api_base_url
64
+ "#{ENV.fetch('STORE_BASE_URL')}/v1/"
65
+ end
66
+
67
+ def headers
68
+ { 'Authorization-Token': ENV.fetch('STORE_AUTHORIZATION_TOKEN') }
69
+ end
70
+ end
71
+ end
72
+
73
+ # app/requests/store/orders_request.rb
74
+ module Store
75
+ class OrdersRequest < ::Store::BaseRequest
76
+ private
77
+
78
+ def key_name
79
+ :order
80
+ end
81
+
82
+ def klass
83
+ ::Store::Order
84
+ end
85
+ end
86
+ end
87
+
88
+ # app/builders/store/order.rb
89
+ module Store
90
+ class Order < ApplicationBuilder
91
+ attr_accessor :client_name,
92
+ :payment_form
93
+
94
+ def requester
95
+ @requester ||= ::Store::OrdersRequest.new
96
+ end
97
+
98
+ private
99
+
100
+ def changeable_attributes
101
+ { client_name: client_name,
102
+ payment_form: payment_form }
103
+ end
104
+ end
105
+ end
106
+ ```
107
+
108
+ ## Controller usage
109
+
110
+ ```ruby
111
+ # frozen_string_literal: true
112
+
113
+ class OrdersController < ApplicationController
114
+ before_action :set_new_order, only: [:new, :create]
115
+ before_action :set_order, only: [:show, :edit, :update, :destroy]
116
+
117
+ def index
118
+ @orders = Order.paginate(params[:page])
119
+ end
120
+
121
+ def new
122
+ end
123
+
124
+ def create
125
+ if @order.create(order_params)
126
+ redirect_to orders_path
127
+ else
128
+ render :new
129
+ end
130
+ end
131
+
132
+ def edit
133
+ end
134
+
135
+ def update
136
+ if @order.update(order_params)
137
+ redirect_to orders_path
138
+ else
139
+ render :edit
140
+ end
141
+ end
142
+
143
+ def show
144
+ end
145
+
146
+ def destroy
147
+ @order.destroy
148
+
149
+ redirect_to orders_path
150
+ end
151
+
152
+ private
153
+
154
+ def order_params
155
+ params.require(:order).permit(:name, :age)
156
+ end
157
+
158
+ def set_new_order
159
+ @order = Order.new
160
+ end
161
+
162
+ def set_order
163
+ @order = Order.find(params[:id])
164
+ end
165
+ end
36
166
  ```
37
167
 
38
168
  ## Contributing
39
169
 
40
- Bug reports and merge requests are welcome on GitLab at https://gitlab.com/[USERNAME]/interage-request.
170
+ Bug reports and merge requests are welcome on GitLab at
171
+ https://github.com/[USERNAME]/interage-request.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/bin/ci CHANGED
@@ -2,11 +2,12 @@
2
2
 
3
3
  set -e
4
4
 
5
- echo -e "\e[1;36m$ bin/brakeman --force-scan \e[0m"
6
- bin/brakeman --force-scan
5
+ source config;
7
6
 
8
- echo -e "\e[1;36m$ bin/rubocop \e[0m"
9
- bin/rubocop
7
+ execute bin/brakeman --force-scan;
10
8
 
11
- echo -e "\e[1;36m$ bin/rspec \e[0m"
12
- bin/rspec
9
+ execute bin/rubocop;
10
+
11
+ execute bin/rspec;
12
+
13
+ success_message CI completed successfully.;
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ source config;
6
+
7
+ execute bin/ci;
8
+
9
+ execute release_start;
10
+
11
+ CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`;
12
+ RELEASE_VERSION=${CURRENT_BRANCH##*/v};
13
+
14
+ execute gem build interage-request.gemspec;
15
+
16
+ execute gem push interage-request-${RELEASE_VERSION}.gem;
17
+
18
+ execute release_finish;
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = 'Application Request for Interage'
14
14
  spec.description = 'Use Net::HTTP to make requests'
15
- spec.homepage = 'https://gitlab.com/interage/patterns/request'
15
+ spec.homepage = 'https://github.com/InterageASH/application-request'
16
16
  spec.licenses = ['MIT']
17
17
 
18
18
  if spec.respond_to?(:metadata)
@@ -37,6 +37,6 @@ Gem::Specification.new do |spec|
37
37
  spec.require_paths = ['lib']
38
38
 
39
39
  spec.add_development_dependency 'bundler', '~> 2.0'
40
- spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rake', '~> 13.0'
41
41
  spec.add_development_dependency 'rspec', '~> 3.0'
42
42
  end
@@ -2,7 +2,9 @@ Description:
2
2
  Explain the generator
3
3
 
4
4
  Example:
5
- rails g interage:request:create ClientsByDocument Client
5
+ rails g interage:request:create store/order client_name payment_form
6
6
 
7
7
  This will create:
8
- /app/requests/clients_by_document_request.rb
8
+ /app/requests/store/base_request.rb
9
+ /app/requests/store/orders_request.rb
10
+ /app/builders/order.rb
@@ -6,29 +6,119 @@ module Interage
6
6
  class CreateGenerator < Rails::Generators::NamedBase
7
7
  source_root File.expand_path('templates', __dir__)
8
8
 
9
- desc 'Generates a request.'
9
+ desc 'Generates a request and builder.'
10
10
 
11
11
  def initialize(args, *_options)
12
12
  super
13
13
 
14
- @request_name = args[0].camelize
15
- @klass_name = args[1].camelize
16
- @base_path = args[0].underscore.pluralize
14
+ @tab = ' '
15
+ @arguments = args
16
+ @request_param = arguments.shift.to_s.underscore
17
17
  end
18
18
 
19
19
  def create_request
20
- create_file(Rails.root.join("app/requests/#{file_name}_request.rb"),
21
- sample_file.gsub('{{REQUEST_NAME}}', request_name)
22
- .gsub('{{KLASS_NAME}}', klass_name)
23
- .gsub('{{BASE_PATH}}', base_path))
20
+ if create_base_request?
21
+ template('base_request.rb', new_base_request_file)
22
+ end
23
+
24
+ template('request.rb', new_request_file)
25
+
26
+ template('builder.rb', new_builder_file)
24
27
  end
25
28
 
26
29
  private
27
30
 
28
- attr_reader :request_name, :klass_name, :base_path
31
+ attr_reader :tab, :arguments, :request_param
32
+
33
+ def create_base_request?
34
+ !File.exist?(new_base_request_file) && modulu_exists?
35
+ end
36
+
37
+ def new_base_request_file
38
+ Rails.root.join("app/requests/#{base_request_path}.rb")
39
+ end
40
+
41
+ def base_request_path
42
+ "#{main_modulu.underscore}/base_request"
43
+ end
44
+
45
+ def new_request_file
46
+ Rails.root.join("app/requests/#{request_path}.rb")
47
+ end
48
+
49
+ def request_path
50
+ @request_path ||= begin
51
+ path =
52
+ request_param.gsub(prefix_class_name, prefix_class_name.pluralize)
53
+ "#{path}_request"
54
+ end
55
+ end
56
+
57
+ def request_class
58
+ @request_class ||= request_path.split('/').last.camelize
59
+ end
60
+
61
+ def request_full_class
62
+ (modulus + [request_class]).join('::')
63
+ end
64
+
65
+ def new_builder_file
66
+ Rails.root.join("app/builders/#{prefix_class_name}.rb")
67
+ end
68
+
69
+ def builder_class
70
+ @builder_class ||= builder_full_class.split('::').last
71
+ end
72
+
73
+ def builder_full_class
74
+ @builder_full_class ||= prefix_class_name.camelize
75
+ end
76
+
77
+ def builder_accessors
78
+ ":#{arguments.join(",\n#{tabs(modulus.count + 8)}:")}"
79
+ end
80
+
81
+ def builder_changeable_attributes
82
+ arguments.map { |argument| "#{argument}: #{argument}" }
83
+ .join(",\n#{tabs(modulus.count + 3)}")
84
+ end
85
+
86
+ def request_extend_class
87
+ modulu_exists? ? base_request_path.camelize : 'ApplicationRequest'
88
+ end
89
+
90
+ def modulu_exists?
91
+ main_modulu.present?
92
+ end
93
+
94
+ def main_modulu
95
+ modulus.first.to_s
96
+ end
97
+
98
+ def modulus_init
99
+ 0.upto(modulus.count - 1).map do |i|
100
+ "#{tabs(i)}module #{modulus[i]}"
101
+ end
102
+ end
103
+
104
+ def modules_end
105
+ modulus.count.downto(1).map { |i| "#{tabs(i - 1)}end" }
106
+ end
107
+
108
+ def modulus
109
+ @modulus ||= begin
110
+ names = builder_full_class.split('::')
111
+
112
+ names.take(names.count - 1)
113
+ end
114
+ end
115
+
116
+ def tabs(size = nil)
117
+ tab * (size || modulus.count).to_i
118
+ end
29
119
 
30
- def sample_file
31
- File.read(File.expand_path('templates/sample_request.rb', __dir__))
120
+ def prefix_class_name
121
+ @prefix_class_name ||= request_param.split('_by_').first
32
122
  end
33
123
  end
34
124
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= main_modulu %>
4
+ class BaseRequest < ::ApplicationRequest
5
+ private
6
+
7
+ def api_base_url
8
+ "#{ENV.fetch('<%= main_modulu.upcase %>_BASE_URL')}/v1/"
9
+ end
10
+
11
+ def headers
12
+ { 'Authorization-Token': ENV.fetch('<%= main_modulu.upcase %>_AUTHORIZATION_TOKEN') }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ <%= "\n#{modulus_init.join("\n")}" if modulu_exists? %>
3
+ <%= tabs %>class <%= builder_class %> < ApplicationBuilder
4
+ <%= tabs %> attr_accessor <%= builder_accessors %>
5
+
6
+ <%= tabs %> def requester
7
+ <%= tabs %> @requester ||= ::<%= request_full_class %>.new
8
+ <%= tabs %> end
9
+
10
+ <%= tabs %> private
11
+
12
+ <%= tabs %> def changeable_attributes
13
+ <%= tabs %> { <%= builder_changeable_attributes %> }
14
+ <%= tabs %> end
15
+ <%= tabs %>end<%= "\n#{modules_end.join("\n")}" if modulu_exists? %>
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ <%= "\n#{modulus_init.join("\n")}" if modulu_exists? %>
3
+ <%= tabs %>class <%= request_class %> < ::<%= request_extend_class %>
4
+ <%= tabs %> private
5
+
6
+ <%= tabs %> def key_name
7
+ <%= tabs %> :<%= builder_class.underscore %>
8
+ <%= tabs %> end
9
+
10
+ <%= tabs %> def klass
11
+ <%= tabs %> ::<%= builder_full_class %>
12
+ <%= tabs %> end
13
+ <%= tabs %>end<%= "\n#{modules_end.join("\n")}" if modulu_exists? %>
@@ -6,3 +6,4 @@ Example:
6
6
 
7
7
  This will create:
8
8
  /app/requests/application_request.rb
9
+ /app/builders/application_builder.rb
@@ -9,6 +9,8 @@ module Interage
9
9
 
10
10
  def create_application_request
11
11
  copy_file('application_request.rb', application_request_path)
12
+
13
+ copy_file('application_builder.rb', application_builder_path)
12
14
  end
13
15
 
14
16
  private
@@ -16,6 +18,10 @@ module Interage
16
18
  def application_request_path
17
19
  Rails.root.join('app/requests/application_request.rb')
18
20
  end
21
+
22
+ def application_builder_path
23
+ Rails.root.join('app/builders/application_builder.rb')
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationBuilder < ::Interage::ApplicationBuilder
4
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ class ApplicationBuilder < ::Interage::Request::Builder
5
+ def self.paginate(*args)
6
+ new.requester.paginate(*args)
7
+ end
8
+
9
+ def self.all(*args)
10
+ new.requester.all(*args)
11
+ end
12
+
13
+ def self.find(*args)
14
+ new.requester.find(*args)
15
+ end
16
+
17
+ def create(params = {})
18
+ self.attributes = params
19
+
20
+ return false if invalid?
21
+
22
+ response = requester.create(changeable_attributes)
23
+
24
+ mount_errors(response.errors)
25
+
26
+ response.success?
27
+ end
28
+
29
+ def update(params = {})
30
+ self.attributes = params
31
+
32
+ return false if invalid?
33
+
34
+ response = requester.update(id, changeable_attributes)
35
+
36
+ mount_errors(response.errors)
37
+
38
+ response.success?
39
+ end
40
+
41
+ def destroy
42
+ response = requester.destroy(id)
43
+
44
+ mount_errors(response.errors)
45
+
46
+ response.success?
47
+ end
48
+
49
+ def requester
50
+ raise ArgumentError,
51
+ "requester is missing for #{self.class.superclass.name}"
52
+ end
53
+
54
+ private
55
+
56
+ def changeable_attributes
57
+ raise ArgumentError,
58
+ "changeable_attributes is missing for #{self.class.superclass.name}"
59
+ end
60
+ end
61
+ end
@@ -1,86 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'net/http'
4
-
5
3
  module Interage
6
- class ApplicationRequest
7
- def all(params = {})
8
- response = get(path: base_path, params: params)
9
-
10
- response[plural_key_name].map { |attribute| klass.new(attribute) }
11
- end
12
-
13
- def find(id)
14
- response = get(path: member_path(id))
15
-
16
- klass.new(response[key_name])
17
- end
18
-
19
- def create(body)
20
- post(base_path, body)
21
- end
22
-
23
- def update(id)
24
- put(member_path(id), body)
25
- end
26
-
27
- def destroy(id)
28
- delete(path: member_path(id))
29
- end
30
-
31
- protected
32
-
33
- def base_path
34
- end
35
-
36
- def member_path(id)
37
- "#{base_path}/#{id}"
4
+ class ApplicationRequest < ::Interage::Request::Base
5
+ def paginate(page, params: {})
6
+ all(params.merge(page: page))
38
7
  end
39
8
 
40
- def base_url(path: nil, params: {})
41
- "#{api_base_url}#{path}?#{params.to_query}"
42
- end
43
-
44
- def key_name
45
- klass.model_name.to_s.underscore.to_sym
46
- end
47
-
48
- def get(path: nil, params: {})
49
- response = start_request(Net::HTTP::Get, path, params)
50
-
51
- JSON.parse(response.body, symbolize_names: true)
52
- end
53
-
54
- def post(path, body)
55
- start_request(Net::HTTP::Post, path, body)
56
- end
9
+ def all(params = {})
10
+ @response = get(path: base_path, params: params)
57
11
 
58
- def put(path, body)
59
- start_request(Net::HTTP::Put, path, body)
12
+ collection_response.map { |attributes| klass.new(attributes) }
60
13
  end
61
14
 
62
- def delete(path)
63
- start_request(Net::HTTP::Delete, path)
15
+ def create(params)
16
+ post(path: base_path, params: params)
64
17
  end
65
18
 
66
- def start_request(klass, path, body = {})
67
- uri = URI(base_url(path: path))
68
- request = klass.new(uri)
69
- request.body = body.to_json
70
- request.content_type = 'application/json'
71
- headers.map { |key, value| request[key] = value }
19
+ def find(id, params = {})
20
+ @response = get(path: member_path(id), params: params)
72
21
 
73
- Net::HTTP.start(uri.hostname, uri.port) do |http|
74
- http.request(request)
75
- end
22
+ klass.new(member_response) if member_response.present?
76
23
  end
77
24
 
78
- def headers
79
- {}
25
+ def update(id, params = {})
26
+ put(path: member_path(id), params: params)
80
27
  end
81
28
 
82
- def plural_key_name
83
- key_name.to_s.pluralize.to_sym
29
+ def destroy(id, params = {})
30
+ delete(path: member_path(id), params: params)
84
31
  end
85
32
  end
86
33
  end
@@ -7,7 +7,12 @@ module Interage
7
7
  module Request
8
8
  class Error < StandardError
9
9
  end
10
+
11
+ autoload :Base, 'interage/request/base'
12
+ autoload :Builder, 'interage/request/builder'
13
+ autoload :Start, 'interage/request/start'
10
14
  end
11
15
 
16
+ autoload :ApplicationBuilder, 'interage/application_builder'
12
17
  autoload :ApplicationRequest, 'interage/application_request'
13
18
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module Request
5
+ class Base
6
+ delegate :code, :body, :success?, :errors, to: :response
7
+
8
+ protected
9
+
10
+ attr_reader :response
11
+
12
+ def base_path
13
+ plural_key_name
14
+ end
15
+
16
+ def member_path(id)
17
+ "#{base_path}/#{id}"
18
+ end
19
+
20
+ def base_url(path: nil)
21
+ "#{api_base_url}#{path}"
22
+ end
23
+
24
+ def api_base_url
25
+ raise ArgumentError,
26
+ "api_base_url is missing for #{self.class.superclass.name}"
27
+ end
28
+
29
+ def collection_response
30
+ body[plural_key_name.to_sym] || body || []
31
+ end
32
+
33
+ def plural_key_name
34
+ key_name.to_s.pluralize
35
+ end
36
+
37
+ def member_response
38
+ body[key_name.to_sym] || body || {}
39
+ end
40
+
41
+ def key_name
42
+ klass.model_name.to_s.underscore
43
+ end
44
+
45
+ def klass
46
+ Interage::Request::Builder
47
+ end
48
+
49
+ def get(*args)
50
+ start(Net::HTTP::Get, *args)
51
+ end
52
+
53
+ def patch(*args)
54
+ start(Net::HTTP::Patch, *args)
55
+ end
56
+
57
+ def post(*args)
58
+ start(Net::HTTP::Post, *args)
59
+ end
60
+
61
+ def put(*args)
62
+ start(Net::HTTP::Put, *args)
63
+ end
64
+
65
+ def delete(*args)
66
+ start(Net::HTTP::Delete, *args)
67
+ end
68
+
69
+ def start(klass, path:, params: {})
70
+ ::Interage::Request::Start.call(klass,
71
+ base_url(path: path),
72
+ params,
73
+ headers)
74
+ end
75
+
76
+ def headers
77
+ {}
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ module Request
5
+ class Builder
6
+ include ActiveModel::Model
7
+
8
+ attr_accessor :id, :created_at, :updated_at
9
+
10
+ def initialize(attributes = {})
11
+ mount_errors(attributes.delete(:errors))
12
+
13
+ super(assigned_attributes(attributes))
14
+ end
15
+
16
+ def persisted?
17
+ id.present?
18
+ end
19
+
20
+ protected
21
+
22
+ def assigned_attributes(attributes)
23
+ attributes.select { |attr_name, _| respond_to?(attr_name) }
24
+ end
25
+
26
+ def mount_errors(attr_errors)
27
+ return if attr_errors.blank?
28
+
29
+ attr_errors.each do |attr_name, errors|
30
+ Array.wrap(errors).each { |error| self.errors.add(attr_name, error) }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ module Interage
6
+ module Request
7
+ class Start
8
+ def self.call(*args)
9
+ new(*args).perform
10
+ end
11
+
12
+ def initialize(klass, uri, params = {}, headers = {}, ssl = false)
13
+ @klass = klass
14
+ @uri = URI(uri.to_s)
15
+ @params = params
16
+ @headers = headers
17
+ @ssl = ssl
18
+ end
19
+
20
+ def perform
21
+ @response = Net::HTTP.start(uri.host, uri.port) do |http|
22
+ http.use_ssl = true if ssl?
23
+
24
+ http.request(request)
25
+ end
26
+
27
+ self
28
+ end
29
+
30
+ def body
31
+ @body ||= JSON.parse(response_body, symbolize_names: true)
32
+ rescue JSON::ParserError
33
+ {}
34
+ end
35
+
36
+ def code
37
+ response_code.to_i
38
+ end
39
+
40
+ def success?
41
+ response_message == 'OK' && errors.blank?
42
+ end
43
+
44
+ def errors
45
+ body[:errors] || {}
46
+ end
47
+
48
+ protected
49
+
50
+ attr_reader :response, :klass, :uri, :params, :headers, :ssl
51
+
52
+ delegate :code, :body, :message,
53
+ to: :response, allow_nil: true, prefix: true
54
+
55
+ alias ssl? ssl
56
+
57
+ def request
58
+ @request ||= begin
59
+ request = klass.new(uri)
60
+ request.body = params.to_json
61
+ request.content_type = 'application/json'
62
+ headers.map { |key, value| request[key] = value }
63
+
64
+ request
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Interage
4
4
  module Request
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interage-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walmir Neto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-22 00:00:00.000000000 Z
11
+ date: 2020-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -73,27 +73,35 @@ files:
73
73
  - bin/bundle
74
74
  - bin/ci
75
75
  - bin/console
76
+ - bin/publish
76
77
  - bin/rspec
77
78
  - bin/rubocop
78
79
  - bin/setup
79
80
  - interage-request.gemspec
80
81
  - lib/generators/interage/request/create/USAGE
81
82
  - lib/generators/interage/request/create/create_generator.rb
82
- - lib/generators/interage/request/create/templates/sample_request.rb
83
+ - lib/generators/interage/request/create/templates/base_request.rb
84
+ - lib/generators/interage/request/create/templates/builder.rb
85
+ - lib/generators/interage/request/create/templates/request.rb
83
86
  - lib/generators/interage/request/install/USAGE
84
87
  - lib/generators/interage/request/install/install_generator.rb
88
+ - lib/generators/interage/request/install/templates/application_builder.rb
85
89
  - lib/generators/interage/request/install/templates/application_request.rb
90
+ - lib/interage/application_builder.rb
86
91
  - lib/interage/application_request.rb
87
92
  - lib/interage/request.rb
93
+ - lib/interage/request/base.rb
94
+ - lib/interage/request/builder.rb
95
+ - lib/interage/request/start.rb
88
96
  - lib/interage/request/version.rb
89
- homepage: https://gitlab.com/interage/patterns/request
97
+ homepage: https://github.com/InterageASH/application-request
90
98
  licenses:
91
99
  - MIT
92
100
  metadata:
93
101
  allowed_push_host: https://rubygems.org
94
- homepage_uri: https://gitlab.com/interage/patterns/request
95
- source_code_uri: https://gitlab.com/interage/patterns/request
96
- changelog_uri: https://gitlab.com/interage/patterns/request/blob/master/CHANGES
102
+ homepage_uri: https://github.com/InterageASH/application-request
103
+ source_code_uri: https://github.com/InterageASH/application-request
104
+ changelog_uri: https://github.com/InterageASH/application-request/blob/master/CHANGES
97
105
  post_install_message:
98
106
  rdoc_options: []
99
107
  require_paths:
@@ -109,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
117
  - !ruby/object:Gem::Version
110
118
  version: '0'
111
119
  requirements: []
112
- rubygems_version: 3.0.3
120
+ rubygems_version: 3.0.8
113
121
  signing_key:
114
122
  specification_version: 4
115
123
  summary: Application Request for Interage
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class {{REQUEST_NAME}}Request < ApplicationRequest
4
- private
5
-
6
- def klass
7
- {{KLASS_NAME}}
8
- end
9
-
10
- def base_path
11
- '/{{BASE_PATH}}'
12
- end
13
- end