Class: PactBroker::Api::Resources::DefaultBaseResource
Constant Summary
Pacts::Metadata::MAPPINGS
Constants included
from Services
Services::FACTORIES
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logging
included, #log_error
#authenticated?
#append_query_if_present, #badge_url_for_latest_pact, #consumer_webhooks_url, #dashboard_url_for_integration, #decode_pact_metadata, #encode_metadata, #group_url, #hal_browser_url, #integration_url, #label_url, #labels_url, #latest_pact_url, #latest_pacts_url, #latest_tagged_pact_url, #latest_untagged_pact_url, #latest_verification_for_pact_url, #latest_verifications_for_consumer_version_url, #latest_version_url, #matrix_badge_url_for_selectors, #matrix_for_pact_url, #matrix_for_pacticipant_version_url, #matrix_url, #matrix_url_from_params, #new_verification_url, #pact_triggered_webhooks_url, #pact_url, #pact_url_from_params, #pact_version_url, #pact_version_url_with_metadata, #pact_version_url_with_webhook_metadata, #pact_versions_url, #pacticipant_url, #pacticipant_url_from_params, #pacticipants_url, #previous_distinct_diff_url, #previous_distinct_pact_version_url, #provider_webhooks_url, #tag_url, #tagged_pact_versions_url, #tags_url, #templated_can_i_deploy_badge_url, #templated_can_i_deploy_url, #templated_diff_url, #templated_label_url_for_pacticipant, #templated_tag_url_for_pacticipant, #triggered_webhook_logs_url, #url_encode, #verification_publication_url, #verification_triggered_webhooks_url, #verification_url, #verification_url_from_params, #version_url, #version_url_from_params, #versions_url, #webhook_execution_url, #webhook_url, #webhooks_for_consumer_and_provider_url, #webhooks_for_pact_url, #webhooks_status_url, #webhooks_url
#build_metadata_for_latest_pact, #build_metadata_for_pact_for_verification, #build_metadata_for_webhook_triggered_by_pact_publication, #parse_hash, #parse_metadata, #parse_object
Methods included from Services
#badge_service, #certificate_service, #get, #group_service, #index_service, #integration_service, #label_service, #matrix_service, #metrics_service, #pact_service, #pacticipant_service, #register_default_services, #register_service, #tag_service, #verification_service, #version_service, #webhook_service, #webhook_trigger_service
Constructor Details
Returns a new instance of DefaultBaseResource.
25
26
27
28
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 25
def initialize
PactBroker.configuration.before_resource.call(self)
application_context.before_resource&.call(self)
end
|
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
23
24
25
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 23
def user
@user
end
|
Instance Method Details
#application_context ⇒ Object
239
240
241
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 239
def application_context
request.path_info[:application_context]
end
|
#base_url ⇒ Object
70
71
72
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 70
def base_url
request.env["pactbroker.base_url"] || request.base_uri.to_s.chomp('/')
end
|
#charsets_provided ⇒ Object
79
80
81
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 79
def charsets_provided
[["utf-8", :encode]]
end
|
#consumer ⇒ Object
210
211
212
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 210
def consumer
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
end
|
#consumer_name ⇒ Object
144
145
146
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 144
def consumer_name
identifier_from_path[:consumer_name]
end
|
#consumer_specified? ⇒ Boolean
156
157
158
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 156
def consumer_specified?
identifier_from_path.key?(:consumer_name)
end
|
#consumer_version_number ⇒ Object
148
149
150
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 148
def consumer_version_number
identifier_from_path[:consumer_version_number]
end
|
#contract_validation_errors?(contract, params) ⇒ Boolean
197
198
199
200
201
202
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 197
def contract_validation_errors? contract, params
if (invalid = !contract.validate(params))
set_json_validation_error_messages contract.errors.messages
end
invalid
end
|
#database_connector ⇒ Object
235
236
237
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 235
def database_connector
request.env["pactbroker.database_connector"]
end
|
#decorator_class(name) ⇒ Object
243
244
245
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 243
def decorator_class(name)
application_context.decorator_configuration.class_for(name)
end
|
#decorator_context(options = {}) ⇒ Object
92
93
94
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 92
def decorator_context options = {}
application_context.decorator_context_creator.call(self, options)
end
|
#decorator_options(options = {}) ⇒ Object
96
97
98
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 96
def decorator_options options = {}
{ user_options: decorator_context(options) }
end
|
#encode(body) ⇒ Object
We only use utf-8 so leave encoding as it is
84
85
86
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 84
def encode(body)
body
end
|
#find_pacticipant(name, role) ⇒ Object
204
205
206
207
208
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 204
def find_pacticipant name, role
pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant |
set_json_error_message("No #{role} with name '#{name}' found") if pacticipant.nil?
end
end
|
#finish_request ⇒ Object
38
39
40
41
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 38
def finish_request
application_context.after_resource&.call(self)
PactBroker.configuration.after_resource.call(self)
end
|
#forbidden? ⇒ Boolean
47
48
49
50
51
52
53
54
55
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 47
def forbidden?
if application_context.resource_authorizer
!application_context.resource_authorizer.call(self)
elsif PactBroker.configuration.authorize
!PactBroker.configuration.authorize.call(self, {})
else
false
end
end
|
#handle_exception(error) ⇒ Object
100
101
102
103
104
105
106
107
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 100
def handle_exception(error)
error_reference = PactBroker::Errors.generate_error_reference
application_context.error_logger.call(error, error_reference, request)
if PactBroker::Errors.reportable_error?(error)
PactBroker::Errors.report(error, error_reference, request)
end
response.body = application_context.error_response_body_generator.call(error, error_reference, request)
end
|
#identifier_from_path ⇒ Object
Also known as:
path_info
The path_info segments aren’t URL decoded
58
59
60
61
62
63
64
65
66
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 58
def identifier_from_path
@identifier_from_path ||= request.path_info.each_with_object({}) do | (key, value), hash|
if value.is_a?(String)
hash[key] = URI.decode(value)
elsif value.is_a?(Symbol) || value.is_a?(Numeric)
hash[key] = value
end
end
end
|
#integration ⇒ Object
Not necessarily an existing integration
227
228
229
230
231
232
233
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 227
def integration
if consumer_specified? && provider_specified?
OpenStruct.new(consumer: consumer, provider: provider)
else
nil
end
end
|
#invalid_json? ⇒ Boolean
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 176
def invalid_json?
begin
params
false
rescue StandardError => e
logger.info "Error parsing JSON #{e} - #{request_body}"
set_json_error_message "Error parsing JSON - #{e.message}"
response.['Content-Type'] = 'application/hal+json;charset=utf-8'
true
end
end
|
#is_authorized?(authorization_header) ⇒ Boolean
43
44
45
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 43
def is_authorized?()
authenticated?(self, )
end
|
#known_methods ⇒ Object
34
35
36
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 34
def known_methods
super + ['PATCH']
end
|
#options ⇒ Object
30
31
32
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 30
def options
{ 'Access-Control-Allow-Methods' => allowed_methods.join(", ")}
end
|
#pact ⇒ Object
222
223
224
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 222
def pact
@pact ||= pact_service.find_pact(pact_params)
end
|
#pact_params ⇒ Object
126
127
128
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 126
def pact_params
@pact_params ||= PactBroker::Pacts::PactParams.from_request request, path_info
end
|
#pacticipant ⇒ Object
218
219
220
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 218
def pacticipant
@pacticipant ||= identifier_from_path[:pacticipant_name] && find_pacticipant(identifier_from_path[:pacticipant_name], "pacticipant")
end
|
#pacticipant_name ⇒ Object
168
169
170
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 168
def pacticipant_name
identifier_from_path[:pacticipant_name]
end
|
#pacticipant_specified? ⇒ Boolean
172
173
174
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 172
def pacticipant_specified?
identifier_from_path.key?(:pacticipant_name)
end
|
#pacticipant_version_number ⇒ Object
152
153
154
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 152
def pacticipant_version_number
identifier_from_path[:pacticipant_version_number]
end
|
#params(options = {}) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 109
def params(options = {})
return options[:default] if options.key?(:default) && request_body.empty?
symbolize_names = !options.key?(:symbolize_names) || options[:symbolize_names]
if symbolize_names
@params_with_symbol_keys ||= JSON.parse(request_body, { symbolize_names: true }.merge(PACT_PARSING_OPTIONS))
else
@params_with_string_keys ||= JSON.parse(request_body, { symbolize_names: false }.merge(PACT_PARSING_OPTIONS))
end
rescue JSON::JSONError => e
raise InvalidJsonError.new("Error parsing JSON - #{e.message}")
end
|
#params_with_string_keys ⇒ Object
122
123
124
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 122
def params_with_string_keys
params(symbolize_names: false)
end
|
#provider ⇒ Object
214
215
216
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 214
def provider
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
end
|
#provider_name ⇒ Object
164
165
166
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 164
def provider_name
identifier_from_path[:provider_name]
end
|
#provider_specified? ⇒ Boolean
160
161
162
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 160
def provider_specified?
identifier_from_path.key?(:provider_name)
end
|
#request_body ⇒ Object
140
141
142
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 140
def request_body
@request_body ||= request.body.to_s
end
|
#resource_url ⇒ Object
88
89
90
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 88
def resource_url
request.uri.to_s.gsub(/\?.*/, '').chomp('/')
end
|
#set_json_error_message(message) ⇒ Object
130
131
132
133
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 130
def set_json_error_message message
response.['Content-Type'] = 'application/hal+json;charset=utf-8'
response.body = { error: message }.to_json
end
|
#set_json_validation_error_messages(errors) ⇒ Object
135
136
137
138
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 135
def set_json_validation_error_messages errors
response.['Content-Type'] = 'application/hal+json;charset=utf-8'
response.body = { errors: errors }.to_json
end
|
#ui_base_url ⇒ Object
See comments for base_url in lib/pact_broker/doc/controllers/app.rb
75
76
77
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 75
def ui_base_url
request.env["pactbroker.base_url"] || ''
end
|
#validation_errors?(model) ⇒ Boolean
188
189
190
191
192
193
194
195
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 188
def validation_errors? model
if (errors = model.validate).any?
set_json_validation_error_messages errors
true
else
false
end
end
|