Class: PactBroker::Api::Resources::BaseResource
Direct Known Subclasses
AllWebhooks, Badge, Dashboard, ErrorTest, Group, Index, Integration, Integrations, Label, LatestPact, LatestPacts, LatestVerificationsForConsumerVersion, Matrix, MatrixForConsumerAndProvider, Metrics, Pact, PactContentDiff, PactTriggeredWebhooks, PactVersions, PactWebhooks, PactWebhooksStatus, Pacticipant, Pacticipants, PacticipantsForLabel, PreviousDistinctPactVersion, ProviderPacts, Relationships, Tag, TaggedPactVersions, TriggeredWebhookLogs, Verification, VerificationTriggeredWebhooks, Verifications, Version, Versions, Webhook, WebhookExecution, Webhooks
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, #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_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, #parse_webhook_metadata, #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
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 BaseResource.
28
29
30
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 28
def initialize
PactBroker.configuration.before_resource.call(self)
end
|
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
26
27
28
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 26
def user
@user
end
|
Instance Method Details
#base_url ⇒ Object
61
62
63
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 61
def base_url
request.base_uri.to_s.chomp('/')
end
|
#charsets_provided ⇒ Object
65
66
67
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 65
def charsets_provided
[['utf-8', :encode]]
end
|
#consumer ⇒ Object
158
159
160
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 158
def consumer
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
end
|
#consumer_name ⇒ Object
112
113
114
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 112
def consumer_name
identifier_from_path[:consumer_name]
end
|
#contract_validation_errors?(contract, params) ⇒ Boolean
145
146
147
148
149
150
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 145
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
170
171
172
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 170
def database_connector
request.env["pactbroker.database_connector"]
end
|
#decorator_context(options = {}) ⇒ Object
78
79
80
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 78
def decorator_context options = {}
Decorators::DecoratorContext.new(base_url, resource_url, options)
end
|
#encode(body) ⇒ Object
We only use utf-8 so leave encoding as it is
70
71
72
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 70
def encode(body)
body
end
|
#find_pacticipant(name, role) ⇒ Object
152
153
154
155
156
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 152
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
40
41
42
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 40
def finish_request
PactBroker.configuration.after_resource.call(self)
end
|
#handle_exception(e) ⇒ Object
#identifier_from_path ⇒ Object
Also known as:
path_info
53
54
55
56
57
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 53
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
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 124
def invalid_json?
begin
JSON.parse(request_body, PACT_PARSING_OPTIONS) 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
44
45
46
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 44
def is_authorized?()
authenticated?(self, )
end
|
#options ⇒ Object
32
33
34
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 32
def options
{ 'Access-Control-Allow-Methods' => allowed_methods.join(", ")}
end
|
#pact ⇒ Object
166
167
168
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 166
def pact
@pact ||= pact_service.find_pact(pact_params)
end
|
#pacticipant_name ⇒ Object
120
121
122
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 120
def pacticipant_name
identifier_from_path[:pacticipant_name]
end
|
#params ⇒ Object
86
87
88
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 86
def params
@params ||= JSON.parse(request.body.to_s, {symbolize_names: true}.merge(PACT_PARSING_OPTIONS))
end
|
#params_with_string_keys ⇒ Object
90
91
92
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 90
def params_with_string_keys
JSON.parse(request.body.to_s, {symbolize_names: false}.merge(PACT_PARSING_OPTIONS))
end
|
#provider ⇒ Object
162
163
164
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 162
def provider
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
end
|
#provider_name ⇒ Object
116
117
118
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 116
def provider_name
identifier_from_path[:provider_name]
end
|
#request_body ⇒ Object
108
109
110
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 108
def request_body
@request_body ||= request.body.to_s
end
|
#resource_url ⇒ Object
74
75
76
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 74
def resource_url
request.uri.to_s.gsub(/\?.*/, '')
end
|
#set_json_error_message(message) ⇒ Object
98
99
100
101
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 98
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
103
104
105
106
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 103
def set_json_validation_error_messages errors
response.['Content-Type'] = 'application/hal+json;charset=utf-8'
response.body = {errors: errors}.to_json
end
|
#update_matrix_after_request? ⇒ Boolean
36
37
38
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 36
def update_matrix_after_request?
false
end
|
#validation_errors?(model) ⇒ Boolean
136
137
138
139
140
141
142
143
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 136
def validation_errors? model
if (errors = model.validate).any?
set_json_validation_error_messages errors
true
else
false
end
end
|