interage-request 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef769569ed4c32d07be9e4385cf2c229c5960f6d0fd58a26877539b102822d34
4
- data.tar.gz: ce7bff4cdbaa538602caed4b539e8a566eece0d7e2fef30a3c8e76a7d85b8b26
3
+ metadata.gz: 83e4804dec76967fdc3a51551d2d7bbed163464544acacea44d74adedcb2f9f4
4
+ data.tar.gz: cf05912afeeef17680c418babf304c47455b22617eca91b4c39759ba7ddfcf97
5
5
  SHA512:
6
- metadata.gz: cb0c5709c01391b8c85ab844a4c4e4526d57f9f3b94d59a28b088f78a7694e4a062ea2ac6abcd6783aa0350ca94383bcacca9aafc6cb5e76fe56a72c99ea5380
7
- data.tar.gz: ae23527b5f6343e5ca758915ecd3dbaa400a86a061d787a1cb52ecb2852458fad768e4f170c74004a359bb3824a5f46bb2f6ba3f9ef3775aae18422df7bf7524
6
+ metadata.gz: 8c28d57ab9fa9c4135fa0020abc8c5afcca732d53a5745f39a2f192fd2d82ea5a1cc7fa34fb4f9922fb6e612c066206da64b86707db7dc7a57ca012454000d7a
7
+ data.tar.gz: 7fb7f13ed9f00f46ba954b603d399525c149e1515f87da99712aa2562e80dd1cda7322c3cf60d97a9aff4825a30a13391bafe29cfc1462b94d0a73b63d749a99
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
data/.rubocop.yml CHANGED
@@ -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,16 @@
1
+ Version 0.1.3:
2
+ - Altera arquivos VERSION e CHANGES para 0.1.3
3
+ - Empacota nova versão da gem
4
+ - Corrige arquivo de versões
5
+ - Adiciona parametro 'uri' que estava faltando
6
+ - Gera nova versão da gem
7
+
8
+ Version 0.1.2:
9
+ - Altera arquivos VERSION e CHANGES para 0.1.2
10
+ - Atualiza version.rb
11
+ - Corrige atributo no método update
12
+ - Adiciona verificação de SSL como parametro opciona
13
+
1
14
  Version 0.1.1:
2
15
  - Empacota a versão 0.1.1
3
16
  - Verifica se a resposta do find não é nula
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', '~> 5.2', '>= 5.2.4.2'
22
22
  end
data/Gemfile.lock CHANGED
@@ -1,80 +1,80 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- interage-request (0.1.0)
4
+ interage-request (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activesupport (5.2.3)
9
+ activesupport (5.2.4.2)
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
14
  ast (2.4.0)
15
- brakeman (4.5.0)
15
+ brakeman (4.8.1)
16
16
  coderay (1.1.2)
17
- concurrent-ruby (1.1.5)
17
+ concurrent-ruby (1.1.6)
18
18
  diff-lcs (1.3)
19
- i18n (1.6.0)
19
+ i18n (1.8.2)
20
20
  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)
21
+ jaro_winkler (1.5.4)
22
+ method_source (1.0.0)
23
+ minitest (5.14.0)
24
+ parallel (1.19.1)
25
+ parser (2.7.1.2)
26
26
  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)
27
+ pry (0.13.1)
28
+ coderay (~> 1.1)
29
+ method_source (~> 1.0)
31
30
  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)
31
+ rake (13.0.1)
32
+ rexml (3.2.4)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.2)
38
+ rspec-support (~> 3.9.3)
39
+ rspec-expectations (3.9.1)
40
40
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.8.0)
42
- rspec-mocks (3.8.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.1)
43
43
  diff-lcs (>= 1.2.0, < 2.0)
44
- rspec-support (~> 3.8.0)
45
- rspec-support (3.8.0)
46
- rubocop (0.67.2)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.3)
46
+ rubocop (0.82.0)
47
47
  jaro_winkler (~> 1.5.1)
48
48
  parallel (~> 1.10)
49
- parser (>= 2.5, != 2.5.1.1)
50
- psych (>= 3.1.0)
49
+ parser (>= 2.7.0.1)
51
50
  rainbow (>= 2.2.2, < 4.0)
51
+ rexml
52
52
  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)
53
+ unicode-display_width (>= 1.4.0, < 2.0)
54
+ rubocop-performance (1.5.2)
55
+ rubocop (>= 0.71.0)
56
+ rubocop-rspec (1.39.0)
57
+ rubocop (>= 0.68.1)
58
+ ruby-progressbar (1.10.1)
59
59
  thread_safe (0.3.6)
60
- tzinfo (1.2.5)
60
+ tzinfo (1.2.7)
61
61
  thread_safe (~> 0.1)
62
- unicode-display_width (1.5.0)
62
+ unicode-display_width (1.7.0)
63
63
 
64
64
  PLATFORMS
65
65
  ruby
66
66
 
67
67
  DEPENDENCIES
68
- activesupport (~> 5.2, >= 5.2.3)
69
- brakeman (~> 4.3, >= 4.3.1)
68
+ activesupport (~> 5.2, >= 5.2.4.2)
69
+ brakeman (~> 4.8, >= 4.8.1)
70
70
  bundler (~> 2.0)
71
71
  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)
72
+ pry (~> 0.13.1)
73
+ rake (~> 13.0)
74
+ rspec (~> 3.9)
75
+ rubocop (~> 0.82.0)
76
+ rubocop-performance (~> 1.5, >= 1.5.2)
77
+ rubocop-rspec (~> 1.39)
78
78
 
79
79
  BUNDLED WITH
80
- 2.0.1
80
+ 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,127 @@ 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 `ApplicationForm`
40
+
41
+
42
+ ```ruby
43
+ # app/models/application_form.rb
44
+ class ApplicationForm < ::Interage::ApplicationForm
45
+ end
46
+ ```
47
+
48
+ To create a request and form 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/orders_request.rb
59
+ module Store
60
+ class OrdersRequest < ApplicationRequest
61
+ private
62
+
63
+ def klass
64
+ ::Store::Order
65
+ end
66
+ end
67
+ end
68
+
69
+ # app/models/order.rb
70
+ class Order < ApplicationForm
71
+ attr_accessor :client_name, :payment_form
72
+
73
+ def requester
74
+ @requester ||= ::Store::OrderRequest.new
75
+ end
76
+
77
+ private
78
+
79
+ def changeable_attributes
80
+ { client_name: client_name, payment_form: payment_form }
81
+ end
82
+ end
83
+ ```
84
+
85
+ ## Controller usage
86
+
87
+ ```ruby
88
+ # frozen_string_literal: true
89
+
90
+ class OrdersController < ApplicationController
91
+ before_action :set_new_order, only: [:new, :create]
92
+ before_action :set_order, only: [:show, :edit, :update, :destroy]
93
+
94
+ def index
95
+ @orders = Order.paginate(params[:page])
96
+ end
97
+
98
+ def new
99
+ end
100
+
101
+ def create
102
+ if @order.create(order_params)
103
+ redirect_to orders_path
104
+ else
105
+ render :new
106
+ end
107
+ end
108
+
109
+ def edit
110
+ end
111
+
112
+ def update
113
+ if @order.update(order_params)
114
+ redirect_to orders_path
115
+ else
116
+ render :edit
117
+ end
118
+ end
119
+
120
+ def show
121
+ end
122
+
123
+ def destroy
124
+ @order.destroy
125
+
126
+ redirect_to orders_path
127
+ end
128
+
129
+ private
130
+
131
+ def order_params
132
+ params.require(:order).permit(:name, :age)
133
+ end
134
+
135
+ def set_new_order
136
+ @order = Order.new
137
+ end
138
+
139
+ def set_order
140
+ @order = Order.find(params[:id])
141
+ end
142
+ end
36
143
  ```
37
144
 
38
145
  ## Contributing
39
146
 
40
- Bug reports and merge requests are welcome on GitLab at https://gitlab.com/[USERNAME]/interage-request.
147
+ Bug reports and merge requests are welcome on GitLab at
148
+ https://github.com/[USERNAME]/interage-request.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
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.;
@@ -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
@@ -6,29 +6,58 @@ 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 form.'
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
+ @form_filename = args.first
15
+ first_arg = form_filename.split('/')
16
+ @modulu = first_arg.shift.camelize if first_arg.size > 1
17
+ @form_name = first_arg.join('/').camelize
18
+ @form_fullname = "/#{form_filename}".camelize
19
+ @request_filename = "#{form_filename}_request"
17
20
  end
18
21
 
19
22
  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))
23
+ create_file(new_request_file, sample_request_file)
24
+
25
+ create_file(new_form_file, sample_form_file)
24
26
  end
25
27
 
26
28
  private
27
29
 
28
- attr_reader :request_name, :klass_name, :base_path
30
+ attr_reader :modulu, :form_filename, :form_name, :form_fullname,
31
+ :request_filename, :request_name
32
+
33
+ def new_request_file
34
+ Rails.root.join("app/requests/#{request_filename}.rb")
35
+ end
36
+
37
+ def sample_request_file
38
+ file = modulu.blank? ? 'request' : 'request_namespace'
39
+
40
+ File.read(File.expand_path("templates/#{file}.rb", __dir__))
41
+ .gsub('{{REQUEST_NAME}}', "#{form_name}Request".camelize)
42
+ .gsub('{{FORM_NAME}}', form_fullname)
43
+ .gsub('{{MODULU}}', modulu)
44
+ end
45
+
46
+ def new_form_file
47
+ Rails.root.join("app/forms/#{form_filename}.rb")
48
+ end
49
+
50
+ def sample_form_file
51
+ file = modulu.blank? ? 'form' : 'form_namespace'
52
+
53
+ File.read(File.expand_path("templates/#{file}.rb", __dir__))
54
+ .gsub('{{FORM_ATTRIBUTES}}', form_attributes)
55
+ .gsub('{{FORM_NAME}}', form_name)
56
+ .gsub('{{MODULU}}', modulu)
57
+ end
29
58
 
30
- def sample_file
31
- File.read(File.expand_path('templates/sample_request.rb', __dir__))
59
+ def form_attributes
60
+ ":#{@args.join(', :')}"
32
61
  end
33
62
  end
34
63
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class {{FORM_NAME}} < ApplicationForm
4
+ attr_accessor {{FORM_ATTRIBUTES}}
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module {{MODULU}}
4
+ class {{FORM_NAME}} < ApplicationForm
5
+ attr_accessor {{FORM_ATTRIBUTES}}
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class {{REQUEST_NAME}} < ApplicationRequest
4
+ private
5
+
6
+ def klass
7
+ {{FORM_NAME}}
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module {{MODULU}}
4
+ class {{REQUEST_NAME}} < ApplicationRequest
5
+ private
6
+
7
+ def klass
8
+ {{FORM_NAME}}
9
+ end
10
+ end
11
+ end
@@ -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_form.rb', application_form_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_form_path
23
+ Rails.root.join('app/forms/application_form.rb')
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ApplicationForm < ::Interage::ApplicationForm
4
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Interage
4
+ class ApplicationForm < ::Interage::Request::Form
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,110 +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]) if response.present?
17
- end
18
-
19
- def create(body)
20
- post(base_path, body)
21
- end
22
-
23
- def update(id, body)
24
- put(member_path(id), body)
25
- end
26
-
27
- def destroy(id)
28
- delete(path: member_path(id))
4
+ class ApplicationRequest < ::Interage::Request::Base
5
+ def paginate(page, params: {})
6
+ all(params.merge(page: page))
29
7
  end
30
8
 
31
- protected
32
-
33
- def base_path
34
- end
35
-
36
- def member_path(id)
37
- "#{base_path}/#{id}"
38
- end
39
-
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
57
-
58
- def put(path, body)
59
- start_request(Net::HTTP::Put, path, body)
60
- end
61
-
62
- def delete(path)
63
- start_request(Net::HTTP::Delete, path)
64
- end
9
+ def all(params = {})
10
+ @response = get(path: base_path, params: params)
65
11
 
66
- def start_request(klass, path, body = {}, use_ssl=false, verify_ssl=true)
67
- return start_https_request(klass, path, body, verify_ssl) if use_ssl
68
- return start_http_request(klass, path, body)
12
+ collection_response.map { |attributes| klass.new(attributes) }
69
13
  end
70
14
 
71
- def start_http_request(klass, path, body)
72
- uri = URI(base_url(path: path))
73
- request = http_request(klass, path, body, uri)
74
-
75
- Net::HTTP.start(uri.hostname, uri.port) do |http|
76
- http.request(request)
77
- end
15
+ def create(params)
16
+ post(path: base_path, params: params)
78
17
  end
79
18
 
80
- def start_https_request(klass, path, body, verify_ssl)
81
- uri = URI(base_url(path: path))
82
- request = http_request(klass, path, body, uri)
19
+ def find(id, params = {})
20
+ @response = get(path: member_path(id), params: params)
83
21
 
84
- http = Net::HTTP.new uri.hostname, 443
85
- http.use_ssl = true
86
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless verify_ssl
87
-
88
- http.start do |http|
89
- http.request(request)
90
- end
91
- end
92
-
93
- def headers
94
- {}
22
+ klass.new(member_response) if member_response.present?
95
23
  end
96
24
 
97
- def plural_key_name
98
- key_name.to_s.pluralize.to_sym
25
+ def update(id, params = {})
26
+ put(path: member_path(id), params: params)
99
27
  end
100
28
 
101
- def http_request(klass, path, body, uri)
102
- request = klass.new(uri)
103
- request.body = body.to_json
104
- request.content_type = 'application/json'
105
- headers.map { |key, value| request[key] = value }
106
-
107
- request
29
+ def destroy(id, params = {})
30
+ delete(path: member_path(id), params: params)
108
31
  end
109
32
  end
110
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 :Form, 'interage/request/form'
13
+ autoload :Start, 'interage/request/start'
10
14
  end
11
15
 
16
+ autoload :ApplicationForm, 'interage/application_form'
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::Form
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 Form
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 = http.start { |h| h.request(request) }
22
+
23
+ self
24
+ end
25
+
26
+ def body
27
+ @body ||= JSON.parse(response_body, symbolize_names: true)
28
+ rescue JSON::ParserError
29
+ {}
30
+ end
31
+
32
+ def code
33
+ response_code.to_i
34
+ end
35
+
36
+ def success?
37
+ response_message == 'OK' && errors.blank?
38
+ end
39
+
40
+ def errors
41
+ body[:errors] || {}
42
+ end
43
+
44
+ protected
45
+
46
+ attr_reader :response, :klass, :uri, :params, :headers, :ssl
47
+
48
+ delegate :code, :body, :message,
49
+ to: :response, allow_nil: true, prefix: true
50
+
51
+ alias ssl? ssl
52
+
53
+ def http
54
+ @http ||= Net::HTTP.new(uri.hostname, uri.port, use_ssl: ssl?)
55
+ end
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.3'
5
+ VERSION = '0.2.0'
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.3
4
+ version: 0.2.0
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-08-28 00:00:00.000000000 Z
11
+ date: 2020-05-17 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
@@ -79,21 +79,29 @@ files:
79
79
  - interage-request.gemspec
80
80
  - lib/generators/interage/request/create/USAGE
81
81
  - lib/generators/interage/request/create/create_generator.rb
82
- - lib/generators/interage/request/create/templates/sample_request.rb
82
+ - lib/generators/interage/request/create/templates/form.rb
83
+ - lib/generators/interage/request/create/templates/form_namespace.rb
84
+ - lib/generators/interage/request/create/templates/request.rb
85
+ - lib/generators/interage/request/create/templates/request_namespace.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_form.rb
85
89
  - lib/generators/interage/request/install/templates/application_request.rb
90
+ - lib/interage/application_form.rb
86
91
  - lib/interage/application_request.rb
87
92
  - lib/interage/request.rb
93
+ - lib/interage/request/base.rb
94
+ - lib/interage/request/form.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.6
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