active_shrine 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.rspec +1 -0
  4. data/CHANGELOG.md +5 -0
  5. data/CODE_OF_CONDUCT.md +84 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +42 -14
  8. data/Rakefile +2 -8
  9. data/config.ru +9 -0
  10. data/lib/.DS_Store +0 -0
  11. data/lib/active_shrine/attached/base.rb +24 -0
  12. data/lib/active_shrine/attached/changes/create_many.rb +45 -0
  13. data/lib/active_shrine/attached/changes/create_one.rb +51 -0
  14. data/lib/active_shrine/attached/changes/create_one_of_many.rb +17 -0
  15. data/lib/active_shrine/attached/changes/delete_many.rb +28 -0
  16. data/lib/active_shrine/attached/changes/delete_one.rb +24 -0
  17. data/lib/active_shrine/attached/changes/detach_many.rb +24 -0
  18. data/lib/active_shrine/attached/changes/detach_one.rb +31 -0
  19. data/lib/active_shrine/attached/changes/purge_many.rb +34 -0
  20. data/lib/active_shrine/attached/changes/purge_one.rb +34 -0
  21. data/lib/active_shrine/attached/changes.rb +24 -0
  22. data/lib/active_shrine/attached/many.rb +78 -0
  23. data/lib/active_shrine/attached/one.rb +90 -0
  24. data/lib/active_shrine/attached.rb +14 -0
  25. data/lib/active_shrine/attachment.rb +111 -0
  26. data/lib/active_shrine/job/destroy_shrine_attachment.rb +18 -0
  27. data/lib/active_shrine/job/promote_shrine_attachment.rb +21 -0
  28. data/lib/active_shrine/job.rb +12 -0
  29. data/lib/active_shrine/model.rb +200 -0
  30. data/lib/active_shrine/railtie.rb +11 -0
  31. data/lib/active_shrine/reflection.rb +75 -0
  32. data/lib/active_shrine/version.rb +5 -0
  33. data/lib/active_shrine.rb +18 -0
  34. data/lib/generators/.DS_Store +0 -0
  35. data/lib/generators/active_shrine/install/install_generator.rb +29 -0
  36. data/lib/generators/active_shrine/install/templates/app/jobs/destroy_shrine_attachment_job.rb +5 -0
  37. data/lib/generators/active_shrine/install/templates/app/jobs/promote_shrine_attachment_job.rb +5 -0
  38. data/lib/generators/active_shrine/install/templates/config/initializers/shrine.rb +51 -0
  39. data/lib/generators/active_shrine/install/templates/db/migrate/create_active_shrine_attachments.rb +28 -0
  40. data/sig/active_shrine.rbs +4 -0
  41. metadata +120 -47
  42. data/MIT-LICENSE +0 -20
  43. data/app/assets/config/api_base_manifest.js +0 -1
  44. data/app/assets/stylesheets/api_base/application.css +0 -15
  45. data/app/controllers/api_base/application_controller.rb +0 -6
  46. data/app/helpers/api_base/application_helper.rb +0 -6
  47. data/app/jobs/api_base/application_job.rb +0 -6
  48. data/app/mailers/api_base/application_mailer.rb +0 -8
  49. data/app/models/api_base/api_log.rb +0 -108
  50. data/app/models/api_base/application_record.rb +0 -7
  51. data/app/views/layouts/api_base/application.html.erb +0 -15
  52. data/config/routes.rb +0 -4
  53. data/db/migrate/20220612165032_create_api_logs.rb +0 -22
  54. data/lib/api_base/behaviours/get_json.rb +0 -26
  55. data/lib/api_base/behaviours/post_json.rb +0 -27
  56. data/lib/api_base/behaviours/shared.rb +0 -51
  57. data/lib/api_base/concerns/filterer.rb +0 -47
  58. data/lib/api_base/concerns/traceable.rb +0 -25
  59. data/lib/api_base/connection.rb +0 -24
  60. data/lib/api_base/endpoint.rb +0 -65
  61. data/lib/api_base/engine.rb +0 -7
  62. data/lib/api_base/errors/api_error.rb +0 -8
  63. data/lib/api_base/errors/processing_error.rb +0 -8
  64. data/lib/api_base/service.rb +0 -17
  65. data/lib/api_base/version.rb +0 -5
  66. data/lib/api_base.rb +0 -14
  67. data/lib/tasks/api_base_tasks.rake +0 -5
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateActiveShrineAttachments < ActiveRecord::Migration[7.0]
4
+ def change
5
+ create_table :active_shrine_attachments do |t|
6
+ t.belongs_to :record, polymorphic: true, null: true
7
+ t.string :name, null: false
8
+ t.string :type, null: false, default: "ActiveShrine::Attachment"
9
+ if ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
10
+ t.jsonb :file_data, null: false
11
+ t.jsonb :metadata, default: {}, null: false
12
+ else
13
+ t.json :file_data, null: false
14
+ t.json :metadata, default: {}, null: false
15
+ end
16
+
17
+ t.timestamps
18
+ end
19
+ add_index :active_shrine_attachments, :name
20
+ if ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql")
21
+ add_index :active_shrine_attachments, :file_data, using: :gin
22
+ add_index :active_shrine_attachments, :metadata, using: :gin
23
+ else
24
+ add_index :active_shrine_attachments, :file_data
25
+ add_index :active_shrine_attachments, :metadata
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module ActiveShrine
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,100 +1,172 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_shrine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Stefan Froelich
7
+ - Radioactive Labs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-11 00:00:00.000000000 Z
11
+ date: 2024-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: faraday
14
+ name: railties
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.0'
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: shrine
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
20
40
  type: :runtime
21
41
  prerelease: false
22
42
  version_requirements: !ruby/object:Gem::Requirement
23
43
  requirements:
24
44
  - - ">="
25
45
  - !ruby/object:Gem::Version
26
- version: '1.0'
46
+ version: '0'
27
47
  - !ruby/object:Gem::Dependency
28
- name: rails
48
+ name: activesupport
29
49
  requirement: !ruby/object:Gem::Requirement
30
50
  requirements:
31
51
  - - ">="
32
52
  - !ruby/object:Gem::Version
33
- version: 6.0.3
53
+ version: '0'
34
54
  type: :runtime
35
55
  prerelease: false
36
56
  version_requirements: !ruby/object:Gem::Requirement
37
57
  requirements:
38
58
  - - ">="
39
59
  - !ruby/object:Gem::Version
40
- version: 6.0.3
60
+ version: '0'
41
61
  - !ruby/object:Gem::Dependency
42
- name: stoplight
62
+ name: activestorage
43
63
  requirement: !ruby/object:Gem::Requirement
44
64
  requirements:
45
65
  - - ">="
46
66
  - !ruby/object:Gem::Version
47
- version: 3.0.0
67
+ version: '0'
48
68
  type: :runtime
49
69
  prerelease: false
50
70
  version_requirements: !ruby/object:Gem::Requirement
51
71
  requirements:
52
72
  - - ">="
53
73
  - !ruby/object:Gem::Version
54
- version: 3.0.0
55
- description: Building blocks for building API Clients
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: combustion
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: appraisal
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Write a longer description or delete this line.
56
118
  email:
57
- - sfroelich01@gmail.com
58
119
  executables: []
59
120
  extensions: []
60
121
  extra_rdoc_files: []
61
122
  files:
62
- - MIT-LICENSE
123
+ - ".DS_Store"
124
+ - ".rspec"
125
+ - CHANGELOG.md
126
+ - CODE_OF_CONDUCT.md
127
+ - LICENSE.txt
63
128
  - README.md
64
129
  - Rakefile
65
- - app/assets/config/api_base_manifest.js
66
- - app/assets/stylesheets/api_base/application.css
67
- - app/controllers/api_base/application_controller.rb
68
- - app/helpers/api_base/application_helper.rb
69
- - app/jobs/api_base/application_job.rb
70
- - app/mailers/api_base/application_mailer.rb
71
- - app/models/api_base/api_log.rb
72
- - app/models/api_base/application_record.rb
73
- - app/views/layouts/api_base/application.html.erb
74
- - config/routes.rb
75
- - db/migrate/20220612165032_create_api_logs.rb
76
- - lib/api_base.rb
77
- - lib/api_base/behaviours/get_json.rb
78
- - lib/api_base/behaviours/post_json.rb
79
- - lib/api_base/behaviours/shared.rb
80
- - lib/api_base/concerns/filterer.rb
81
- - lib/api_base/concerns/traceable.rb
82
- - lib/api_base/connection.rb
83
- - lib/api_base/endpoint.rb
84
- - lib/api_base/engine.rb
85
- - lib/api_base/errors/api_error.rb
86
- - lib/api_base/errors/processing_error.rb
87
- - lib/api_base/service.rb
88
- - lib/api_base/version.rb
89
- - lib/tasks/api_base_tasks.rake
90
- homepage: https://github.com/ussd-engine/api-base
130
+ - config.ru
131
+ - lib/.DS_Store
132
+ - lib/active_shrine.rb
133
+ - lib/active_shrine/attached.rb
134
+ - lib/active_shrine/attached/base.rb
135
+ - lib/active_shrine/attached/changes.rb
136
+ - lib/active_shrine/attached/changes/create_many.rb
137
+ - lib/active_shrine/attached/changes/create_one.rb
138
+ - lib/active_shrine/attached/changes/create_one_of_many.rb
139
+ - lib/active_shrine/attached/changes/delete_many.rb
140
+ - lib/active_shrine/attached/changes/delete_one.rb
141
+ - lib/active_shrine/attached/changes/detach_many.rb
142
+ - lib/active_shrine/attached/changes/detach_one.rb
143
+ - lib/active_shrine/attached/changes/purge_many.rb
144
+ - lib/active_shrine/attached/changes/purge_one.rb
145
+ - lib/active_shrine/attached/many.rb
146
+ - lib/active_shrine/attached/one.rb
147
+ - lib/active_shrine/attachment.rb
148
+ - lib/active_shrine/job.rb
149
+ - lib/active_shrine/job/destroy_shrine_attachment.rb
150
+ - lib/active_shrine/job/promote_shrine_attachment.rb
151
+ - lib/active_shrine/model.rb
152
+ - lib/active_shrine/railtie.rb
153
+ - lib/active_shrine/reflection.rb
154
+ - lib/active_shrine/version.rb
155
+ - lib/generators/.DS_Store
156
+ - lib/generators/active_shrine/install/install_generator.rb
157
+ - lib/generators/active_shrine/install/templates/app/jobs/destroy_shrine_attachment_job.rb
158
+ - lib/generators/active_shrine/install/templates/app/jobs/promote_shrine_attachment_job.rb
159
+ - lib/generators/active_shrine/install/templates/config/initializers/shrine.rb
160
+ - lib/generators/active_shrine/install/templates/db/migrate/create_active_shrine_attachments.rb
161
+ - sig/active_shrine.rbs
162
+ homepage: https://rubygems.org
91
163
  licenses:
92
164
  - MIT
93
165
  metadata:
94
166
  allowed_push_host: https://rubygems.org
95
- homepage_uri: https://github.com/ussd-engine/api-base
96
- source_code_uri: https://github.com/ussd-engine/api-base
97
- changelog_uri: https://github.com/ussd-engine/api-base
167
+ homepage_uri: https://rubygems.org
168
+ source_code_uri: https://rubygems.org
169
+ changelog_uri: https://rubygems.org
98
170
  post_install_message:
99
171
  rdoc_options: []
100
172
  require_paths:
@@ -103,15 +175,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
175
  requirements:
104
176
  - - ">="
105
177
  - !ruby/object:Gem::Version
106
- version: '0'
178
+ version: 3.1.0
107
179
  required_rubygems_version: !ruby/object:Gem::Requirement
108
180
  requirements:
109
181
  - - ">="
110
182
  - !ruby/object:Gem::Version
111
183
  version: '0'
112
184
  requirements: []
113
- rubygems_version: 3.3.7
185
+ rubygems_version: 3.5.3
114
186
  signing_key:
115
187
  specification_version: 4
116
- summary: Building blocks for building API Clients
188
+ summary: A compatible ActiveStorage api for attaching Shrine uploads to ActiveRecord
189
+ models
117
190
  test_files: []
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2022 Stefan Froelich
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1 +0,0 @@
1
- //= link_directory ../stylesheets/api_base .css
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- class ApplicationController < ActionController::Base
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- module ApplicationHelper
5
- end
6
- end
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- class ApplicationJob < ActiveJob::Base
5
- end
6
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- class ApplicationMailer < ActionMailer::Base
5
- default from: 'from@example.com'
6
- layout 'mailer'
7
- end
8
- end
@@ -1,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # == Schema Information
4
- #
5
- # Table name: api_logs
6
- #
7
- # id :bigint not null, primary key
8
- # api :text not null
9
- # duration :float
10
- # endpoint :text not null
11
- # exception :jsonb
12
- # method :text not null
13
- # origin :text not null
14
- # request_body :jsonb
15
- # request_headers :jsonb
16
- # response_body :jsonb
17
- # response_headers :jsonb
18
- # source :text not null
19
- # status_code :integer
20
- # created_at :datetime not null
21
- # updated_at :datetime not null
22
- #
23
- require 'English'
24
-
25
- module ApiBase
26
- class ApiLog < ApplicationRecord
27
- attribute :sanitized, :boolean, default: false
28
-
29
- validate :ensure_nothing_changed, unless: :new_record?
30
- validate :ensure_data_sanitized
31
-
32
- validates_presence_of :api, :origin, :source, :endpoint
33
-
34
- validates :source, presence: true, inclusion: { in: %w[outgoing_request incoming_webhook] }
35
- validates :method, presence: true, inclusion: { in: %w[GET POST DELETE PUT] }
36
-
37
- def self.start_outgoing_request(origin, method, endpoint, payload)
38
- ApiLog.new api: origin.identifier, origin: origin.class.to_s, source: 'outgoing_request',
39
- endpoint: "#{origin.connection.url_prefix}#{endpoint}", method:,
40
- request_headers: origin.connection.headers, request_body: payload
41
- end
42
-
43
- def complete_outgoing_request(response, duration)
44
- # Ensure we are recording the actual headers that were sent on the request.
45
- # The ones set from the connection might not be the final headers.
46
- self.request_headers = response.env.request_headers
47
- # Set the rest of the response attributes.
48
- assign_attributes status_code: response.status, duration:,
49
- response_body: response.body, response_headers: response.headers
50
- end
51
-
52
- def self.start_webhook_request(origin, request)
53
- ApiLog.new api: origin, origin: origin.class.to_s, source: 'incoming_webhook',
54
- endpoint: request.fullpath, method: request.method,
55
- request_headers: request.headers.env.reject { |key|
56
- key.to_s.include?('.')
57
- }, request_body: request.params
58
- end
59
-
60
- def complete_webhook_request(response, duration)
61
- # Set the rest of the response attributes.
62
- assign_attributes status_code: response.status, duration:,
63
- response_body: response.body, response_headers: response.headers
64
- end
65
-
66
- def filter_sensitive_data
67
- parse_json_fields
68
-
69
- %i[request_headers request_body response_headers response_body exception].each do |prop|
70
- self[prop] = yield(self[prop]) if self[prop].is_a?(Hash)
71
- end
72
-
73
- self.sanitized = true
74
- end
75
-
76
- private
77
-
78
- def ensure_nothing_changed
79
- errors.add(:base, 'Record is read-only') if changed?
80
- end
81
-
82
- def ensure_data_sanitized
83
- errors.add(:base, 'Data must be sanitized') unless sanitized?
84
- end
85
-
86
- def parse_json_fields
87
- %i[request_headers request_body response_headers response_body exception].each do |prop|
88
- self[prop] = safely_parse_json(self[prop])
89
- end
90
- end
91
-
92
- def safely_parse_json(value)
93
- case value
94
- when nil, Hash
95
- value
96
- when String
97
- JSON.parse value
98
- when StandardError
99
- [e.message, *e.backtrace].join($INPUT_RECORD_SEPARATOR).to_json
100
- else
101
- value.to_s.to_json
102
- end
103
- rescue StandardError
104
- # Something we can't encode. Let's preserve it as a string.
105
- value.to_s.to_json
106
- end
107
- end
108
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- class ApplicationRecord < ActiveRecord::Base
5
- self.abstract_class = true
6
- end
7
- end
@@ -1,15 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Api base</title>
5
- <%= csrf_meta_tags %>
6
- <%= csp_meta_tag %>
7
-
8
- <%= stylesheet_link_tag "api_base/application", media: "all" %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
15
- </html>
data/config/routes.rb DELETED
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ApiBase::Engine.routes.draw do
4
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CreateApiLogs < ActiveRecord::Migration[6.0]
4
- def change
5
- create_table :api_base_api_logs do |t|
6
- t.text :api, null: false
7
- t.text :origin, null: false
8
- t.text :source, null: false
9
- t.text :endpoint, null: false
10
- t.text :method, null: false
11
- t.jsonb :request_headers
12
- t.jsonb :request_body
13
- t.integer :status_code
14
- t.jsonb :response_headers
15
- t.jsonb :response_body
16
- t.float :duration
17
- t.jsonb :exception
18
-
19
- t.timestamps
20
- end
21
- end
22
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- module Behaviours
5
- # Shared concern that adds the ability to get json from an endpoint
6
- module GetJson
7
- include Shared
8
-
9
- alias get_json behaviour_delegate
10
-
11
- protected
12
-
13
- def execute_request(endpoint, _payload)
14
- execute do
15
- connection.get(endpoint) do |req|
16
- req.headers['Content-Type'] = 'application/json'
17
- end
18
- end
19
- end
20
-
21
- def method
22
- 'GET'
23
- end
24
- end
25
- end
26
- end
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- module Behaviours
5
- # Shared concern that adds the ability to post json to an endpoint
6
- module PostJson
7
- include Shared
8
-
9
- alias post_json behaviour_delegate
10
-
11
- protected
12
-
13
- def execute_request(endpoint, payload)
14
- execute do
15
- connection.post(endpoint, payload) do |req|
16
- req.body = payload.to_json
17
- req.headers['Content-Type'] = 'application/json'
18
- end
19
- end
20
- end
21
-
22
- def method
23
- 'POST'
24
- end
25
- end
26
- end
27
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- module Behaviours
5
- # Shared module that adds common methods to api behaviours
6
- module Shared
7
- def behaviour_delegate(endpoint, payload = {})
8
- api_log = ApiBase::ApiLog.start_outgoing_request(self, method, endpoint, payload)
9
- response, duration = make_request(endpoint, payload)
10
- api_log.complete_outgoing_request response, duration
11
-
12
- response
13
- rescue StandardError => e
14
- api_log.exception = e if api_log.present?
15
- raise
16
- ensure
17
- if api_log.present?
18
- api_log.filter_sensitive_data { |data| filter_object(data) }
19
- api_log.save!
20
- end
21
-
22
- validate_status_code response if response.present?
23
- end
24
-
25
- protected
26
-
27
- def make_request(endpoint, payload)
28
- trace_active_tag('request.endpoint', endpoint)
29
- trace_active_tag('request.payload', filter_object(payload))
30
-
31
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
32
- response = execute_request(endpoint, payload)
33
- duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
34
-
35
- trace_active_tag('response.status', response.status)
36
- trace_active_tag('response.body', filter_object(response.body))
37
- trace_active_tag('response.duration', duration)
38
-
39
- [response, duration]
40
- end
41
-
42
- def method
43
- raise NotImplementedError, 'method is not implemented'
44
- end
45
-
46
- def execute_request
47
- raise NotImplementedError, 'execute_request is not implemented'
48
- end
49
- end
50
- end
51
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ApiBase
4
- module Concerns
5
- module Filterer
6
- protected
7
-
8
- def filterer
9
- @filterer ||= ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
10
- end
11
-
12
- def filter_object(obj)
13
- return if obj.nil?
14
-
15
- case obj
16
- when Array then filter_array(obj)
17
- when Hash then filter_hash(obj)
18
- else obj
19
- end
20
- end
21
-
22
- def filter_array(array)
23
- return if array.nil?
24
-
25
- array.map do |item|
26
- case item
27
- when Array then filter_array(item)
28
- when Hash then filter_hash(item)
29
- else item
30
- end
31
- end
32
- end
33
-
34
- def filter_hash(hash)
35
- return if hash.nil?
36
-
37
- hash.each do |key, value|
38
- case value
39
- when Array then hash[key] = filter_array(value)
40
- when Hash then hash[key] = filter_hash(value)
41
- end
42
- end
43
- filterer.filter (hash.try(:permit!) || hash).to_hash
44
- end
45
- end
46
- end
47
- end