active_shrine 0.1.0 → 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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.rspec +1 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +42 -14
  9. data/Rakefile +2 -8
  10. data/config.ru +9 -0
  11. data/lib/.DS_Store +0 -0
  12. data/lib/active_shrine/attached/base.rb +24 -0
  13. data/lib/active_shrine/attached/changes/create_many.rb +45 -0
  14. data/lib/active_shrine/attached/changes/create_one.rb +51 -0
  15. data/lib/active_shrine/attached/changes/create_one_of_many.rb +17 -0
  16. data/lib/active_shrine/attached/changes/delete_many.rb +28 -0
  17. data/lib/active_shrine/attached/changes/delete_one.rb +24 -0
  18. data/lib/active_shrine/attached/changes/detach_many.rb +24 -0
  19. data/lib/active_shrine/attached/changes/detach_one.rb +31 -0
  20. data/lib/active_shrine/attached/changes/purge_many.rb +34 -0
  21. data/lib/active_shrine/attached/changes/purge_one.rb +34 -0
  22. data/lib/active_shrine/attached/changes.rb +24 -0
  23. data/lib/active_shrine/attached/many.rb +78 -0
  24. data/lib/active_shrine/attached/one.rb +90 -0
  25. data/lib/active_shrine/attached.rb +14 -0
  26. data/lib/active_shrine/attachment.rb +111 -0
  27. data/lib/active_shrine/job/destroy_shrine_attachment.rb +18 -0
  28. data/lib/active_shrine/job/promote_shrine_attachment.rb +21 -0
  29. data/lib/active_shrine/job.rb +12 -0
  30. data/lib/active_shrine/model.rb +200 -0
  31. data/lib/active_shrine/railtie.rb +11 -0
  32. data/lib/active_shrine/reflection.rb +75 -0
  33. data/lib/active_shrine/version.rb +5 -0
  34. data/lib/active_shrine.rb +18 -0
  35. data/lib/generators/.DS_Store +0 -0
  36. data/lib/generators/active_shrine/install/install_generator.rb +29 -0
  37. data/lib/generators/active_shrine/install/templates/app/jobs/destroy_shrine_attachment_job.rb +5 -0
  38. data/lib/generators/active_shrine/install/templates/app/jobs/promote_shrine_attachment_job.rb +5 -0
  39. data/lib/generators/active_shrine/install/templates/config/initializers/shrine.rb +51 -0
  40. data/lib/generators/active_shrine/install/templates/db/migrate/create_active_shrine_attachments.rb +28 -0
  41. data/sig/active_shrine.rbs +4 -0
  42. metadata +115 -47
  43. data/MIT-LICENSE +0 -20
  44. data/app/assets/config/api_base_manifest.js +0 -1
  45. data/app/assets/stylesheets/api_base/application.css +0 -15
  46. data/app/controllers/api_base/application_controller.rb +0 -6
  47. data/app/helpers/api_base/application_helper.rb +0 -6
  48. data/app/jobs/api_base/application_job.rb +0 -6
  49. data/app/mailers/api_base/application_mailer.rb +0 -8
  50. data/app/models/api_base/api_log.rb +0 -108
  51. data/app/models/api_base/application_record.rb +0 -7
  52. data/app/views/layouts/api_base/application.html.erb +0 -15
  53. data/config/routes.rb +0 -4
  54. data/db/migrate/20220612165032_create_api_logs.rb +0 -22
  55. data/lib/api_base/behaviours/get_json.rb +0 -26
  56. data/lib/api_base/behaviours/post_json.rb +0 -27
  57. data/lib/api_base/behaviours/shared.rb +0 -51
  58. data/lib/api_base/concerns/filterer.rb +0 -47
  59. data/lib/api_base/concerns/traceable.rb +0 -25
  60. data/lib/api_base/connection.rb +0 -24
  61. data/lib/api_base/endpoint.rb +0 -65
  62. data/lib/api_base/engine.rb +0 -7
  63. data/lib/api_base/errors/api_error.rb +0 -8
  64. data/lib/api_base/errors/processing_error.rb +0 -8
  65. data/lib/api_base/service.rb +0 -17
  66. data/lib/api_base/version.rb +0 -5
  67. data/lib/api_base.rb +0 -14
  68. 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,167 @@
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.2.0
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: 2025-01-11 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: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.0'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
28
+ name: shrine
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 6.0.3
33
+ version: '0'
34
34
  type: :runtime
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: 6.0.3
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: stoplight
42
+ name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 3.0.0
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 3.0.0
55
- description: Building blocks for building API Clients
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activestorage
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: combustion
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: appraisal
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Write a longer description or delete this line.
56
112
  email:
57
- - sfroelich01@gmail.com
58
113
  executables: []
59
114
  extensions: []
60
115
  extra_rdoc_files: []
61
116
  files:
62
- - MIT-LICENSE
117
+ - ".DS_Store"
118
+ - ".rspec"
119
+ - ".ruby-version"
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - LICENSE.txt
63
123
  - README.md
64
124
  - 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
125
+ - config.ru
126
+ - lib/.DS_Store
127
+ - lib/active_shrine.rb
128
+ - lib/active_shrine/attached.rb
129
+ - lib/active_shrine/attached/base.rb
130
+ - lib/active_shrine/attached/changes.rb
131
+ - lib/active_shrine/attached/changes/create_many.rb
132
+ - lib/active_shrine/attached/changes/create_one.rb
133
+ - lib/active_shrine/attached/changes/create_one_of_many.rb
134
+ - lib/active_shrine/attached/changes/delete_many.rb
135
+ - lib/active_shrine/attached/changes/delete_one.rb
136
+ - lib/active_shrine/attached/changes/detach_many.rb
137
+ - lib/active_shrine/attached/changes/detach_one.rb
138
+ - lib/active_shrine/attached/changes/purge_many.rb
139
+ - lib/active_shrine/attached/changes/purge_one.rb
140
+ - lib/active_shrine/attached/many.rb
141
+ - lib/active_shrine/attached/one.rb
142
+ - lib/active_shrine/attachment.rb
143
+ - lib/active_shrine/job.rb
144
+ - lib/active_shrine/job/destroy_shrine_attachment.rb
145
+ - lib/active_shrine/job/promote_shrine_attachment.rb
146
+ - lib/active_shrine/model.rb
147
+ - lib/active_shrine/railtie.rb
148
+ - lib/active_shrine/reflection.rb
149
+ - lib/active_shrine/version.rb
150
+ - lib/generators/.DS_Store
151
+ - lib/generators/active_shrine/install/install_generator.rb
152
+ - lib/generators/active_shrine/install/templates/app/jobs/destroy_shrine_attachment_job.rb
153
+ - lib/generators/active_shrine/install/templates/app/jobs/promote_shrine_attachment_job.rb
154
+ - lib/generators/active_shrine/install/templates/config/initializers/shrine.rb
155
+ - lib/generators/active_shrine/install/templates/db/migrate/create_active_shrine_attachments.rb
156
+ - sig/active_shrine.rbs
157
+ homepage: https://rubygems.org
91
158
  licenses:
92
159
  - MIT
93
160
  metadata:
94
161
  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
162
+ homepage_uri: https://rubygems.org
163
+ source_code_uri: https://rubygems.org
164
+ changelog_uri: https://rubygems.org
98
165
  post_install_message:
99
166
  rdoc_options: []
100
167
  require_paths:
@@ -103,15 +170,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
103
170
  requirements:
104
171
  - - ">="
105
172
  - !ruby/object:Gem::Version
106
- version: '0'
173
+ version: '3.2'
107
174
  required_rubygems_version: !ruby/object:Gem::Requirement
108
175
  requirements:
109
176
  - - ">="
110
177
  - !ruby/object:Gem::Version
111
178
  version: '0'
112
179
  requirements: []
113
- rubygems_version: 3.3.7
180
+ rubygems_version: 3.5.16
114
181
  signing_key:
115
182
  specification_version: 4
116
- summary: Building blocks for building API Clients
183
+ summary: A compatible ActiveStorage api for attaching Shrine uploads to ActiveRecord
184
+ models
117
185
  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