Class: PactBroker::Api::Resources::DefaultBaseResource
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logging
included, #log_error
#authenticated?
#badge_url_for_latest_pact, #build_webhook_metadata, #consumer_webhooks_url, #dashboard_url_for_integration, #decode_webhook_metadata, #encode_webhook_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_versions_url, #pacticipant_url, #pacticipant_url_from_params, #pacticipants_url, #previous_distinct_diff_url, #previous_distinct_pact_version_url, #provider_webhooks_url, #tag_url, #tags_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_webhook_triggered_by_pact_publication
Methods included from Services
#badge_service, #certificate_service, #group_service, #index_service, #integration_service, #label_service, #matrix_service, #metrics_service, #pact_service, #pacticipant_service, #tag_service, #verification_service, #version_service, #webhook_service, #webhook_trigger_service
Constructor Details
Returns a new instance of DefaultBaseResource.
25
26
27
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 25
def initialize
PactBroker.configuration.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
#base_url ⇒ Object
58
59
60
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 58
def base_url
PactBroker.configuration.base_url || request.base_uri.to_s.chomp('/')
end
|
#charsets_provided ⇒ Object
67
68
69
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 67
def charsets_provided
[['utf-8', :encode]]
end
|
#consumer ⇒ Object
189
190
191
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 189
def consumer
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
end
|
#consumer_name ⇒ Object
127
128
129
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 127
def consumer_name
identifier_from_path[:consumer_name]
end
|
#consumer_specified? ⇒ Boolean
135
136
137
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 135
def consumer_specified?
identifier_from_path.key?(:consumer_name)
end
|
#consumer_version_number ⇒ Object
131
132
133
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 131
def consumer_version_number
identifier_from_path[:consumer_version_number]
end
|
#contract_validation_errors?(contract, params) ⇒ Boolean
176
177
178
179
180
181
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 176
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
205
206
207
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 205
def database_connector
request.env["pactbroker.database_connector"]
end
|
#decorator_context(options = {}) ⇒ Object
80
81
82
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 80
def decorator_context options = {}
Decorators::DecoratorContext.new(base_url, resource_url, request.env, options)
end
|
#decorator_options(options = {}) ⇒ Object
84
85
86
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 84
def decorator_options options = {}
{ user_options: decorator_context(options) }
end
|
#encode(body) ⇒ Object
We only use utf-8 so leave encoding as it is
72
73
74
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 72
def encode(body)
body
end
|
#find_pacticipant(name, role) ⇒ Object
183
184
185
186
187
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 183
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
37
38
39
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 37
def finish_request
PactBroker.configuration.after_resource.call(self)
end
|
#handle_exception(e) ⇒ Object
#identifier_from_path ⇒ Object
Also known as:
path_info
50
51
52
53
54
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 50
def identifier_from_path
request.path_info.each_with_object({}) do | pair, hash|
hash[pair.first] = pair.last === String ? URI.decode(pair.last) : pair.last
end
end
|
#invalid_json? ⇒ Boolean
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 155
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
41
42
43
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 41
def is_authorized?()
authenticated?(self, )
end
|
#known_methods ⇒ Object
33
34
35
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 33
def known_methods
super + ['PATCH']
end
|
#options ⇒ Object
29
30
31
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 29
def options
{ 'Access-Control-Allow-Methods' => allowed_methods.join(", ")}
end
|
#pact ⇒ Object
201
202
203
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 201
def pact
@pact ||= pact_service.find_pact(pact_params)
end
|
#pacticipant ⇒ Object
197
198
199
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 197
def pacticipant
@pacticipant ||= identifier_from_path[:pacticipant_name] && find_pacticipant(identifier_from_path[:pacticipant_name], "pacticipant")
end
|
#pacticipant_name ⇒ Object
147
148
149
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 147
def pacticipant_name
identifier_from_path[:pacticipant_name]
end
|
#pacticipant_specified? ⇒ Boolean
151
152
153
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 151
def pacticipant_specified?
identifier_from_path.key?(:pacticipant_name)
end
|
#params(options = {}) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 92
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
105
106
107
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 105
def params_with_string_keys
params(symbolize_names: false)
end
|
#provider ⇒ Object
193
194
195
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 193
def provider
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
end
|
#provider_name ⇒ Object
143
144
145
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 143
def provider_name
identifier_from_path[:provider_name]
end
|
#provider_specified? ⇒ Boolean
139
140
141
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 139
def provider_specified?
identifier_from_path.key?(:provider_name)
end
|
#request_body ⇒ Object
123
124
125
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 123
def request_body
@request_body ||= request.body.to_s
end
|
#resource_url ⇒ Object
76
77
78
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 76
def resource_url
request.uri.to_s.gsub(/\?.*/, '').chomp('/')
end
|
#set_json_error_message(message) ⇒ Object
113
114
115
116
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 113
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
118
119
120
121
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 118
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
63
64
65
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 63
def ui_base_url
PactBroker.configuration.base_url || ''
end
|
#validation_errors?(model) ⇒ Boolean
167
168
169
170
171
172
173
174
|
# File 'lib/pact_broker/api/resources/default_base_resource.rb', line 167
def validation_errors? model
if (errors = model.validate).any?
set_json_validation_error_messages errors
true
else
false
end
end
|