Class: PactBroker::Api::Resources::BaseResource
Direct Known Subclasses
AllWebhooks, Badge, Dashboard, ErrorTest, Group, Index, Integrations, Label, LatestPact, LatestPacts, LatestVerificationsForConsumerVersion, Matrix, MatrixForConsumerAndProvider, 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, #consumer_webhooks_url, #hal_browser_url, #label_url, #labels_url, #latest_pact_url, #latest_pacts_url, #latest_untagged_pact_url, #latest_verifications_for_consumer_version_url, #latest_version_url, #matrix_url_from_params, #new_verification_url, #pact_triggered_webhooks_url, #pact_url, #pact_url_from_params, #pact_version_url, #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
Methods included from Services
#badge_service, #certificate_service, #group_service, #index_service, #label_service, #matrix_service, #pact_service, #pacticipant_service, #tag_service, #verification_service, #version_service, #webhook_service
Constructor Details
Returns a new instance of BaseResource.
29
30
31
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 29
def initialize
PactBroker.configuration.before_resource.call(self)
end
|
Instance Attribute Details
#user ⇒ Object
Returns the value of attribute user.
27
28
29
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 27
def user
@user
end
|
Instance Method Details
#base_url ⇒ Object
62
63
64
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 62
def base_url
request.base_uri.to_s.chomp('/')
end
|
#charsets_provided ⇒ Object
66
67
68
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 66
def charsets_provided
[['utf-8', :encode]]
end
|
#consumer ⇒ Object
159
160
161
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 159
def consumer
@consumer ||= identifier_from_path[:consumer_name] && find_pacticipant(identifier_from_path[:consumer_name], "consumer")
end
|
#consumer_name ⇒ Object
113
114
115
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 113
def consumer_name
identifier_from_path[:consumer_name]
end
|
#contract_validation_errors?(contract, params) ⇒ Boolean
146
147
148
149
150
151
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 146
def contract_validation_errors? contract, params
if (invalid = !contract.validate(params))
set_json_validation_error_messages contract.errors.messages
end
invalid
end
|
#decorator_context(options = {}) ⇒ Object
79
80
81
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 79
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
71
72
73
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 71
def encode(body)
body
end
|
#find_pacticipant(name, role) ⇒ Object
153
154
155
156
157
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 153
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
41
42
43
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 41
def finish_request
PactBroker.configuration.after_resource.call(self)
end
|
#handle_exception(e) ⇒ Object
#identifier_from_path ⇒ Object
Also known as:
path_info
54
55
56
57
58
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 54
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
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 125
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
45
46
47
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 45
def is_authorized?()
authenticated?(self, )
end
|
#options ⇒ Object
33
34
35
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 33
def options
{ 'Access-Control-Allow-Methods' => allowed_methods.join(", ")}
end
|
#pact ⇒ Object
167
168
169
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 167
def pact
@pact ||= pact_service.find_pact(pact_params)
end
|
#pacticipant_name ⇒ Object
121
122
123
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 121
def pacticipant_name
identifier_from_path[:pacticipant_name]
end
|
#params ⇒ Object
87
88
89
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 87
def params
@params ||= JSON.parse(request.body.to_s, {symbolize_names: true}.merge(PACT_PARSING_OPTIONS))
end
|
#params_with_string_keys ⇒ Object
91
92
93
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 91
def params_with_string_keys
JSON.parse(request.body.to_s, {symbolize_names: false}.merge(PACT_PARSING_OPTIONS))
end
|
#provider ⇒ Object
163
164
165
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 163
def provider
@provider ||= identifier_from_path[:provider_name] && find_pacticipant(identifier_from_path[:provider_name], "provider")
end
|
#provider_name ⇒ Object
117
118
119
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 117
def provider_name
identifier_from_path[:provider_name]
end
|
#request_body ⇒ Object
109
110
111
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 109
def request_body
@request_body ||= request.body.to_s
end
|
#resource_url ⇒ Object
75
76
77
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 75
def resource_url
request.uri.to_s.gsub(/\?.*/, '')
end
|
#set_json_error_message(message) ⇒ Object
99
100
101
102
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 99
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
104
105
106
107
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 104
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
37
38
39
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 37
def update_matrix_after_request?
false
end
|
#validation_errors?(model) ⇒ Boolean
137
138
139
140
141
142
143
144
|
# File 'lib/pact_broker/api/resources/base_resource.rb', line 137
def validation_errors? model
if (errors = model.validate).any?
set_json_validation_error_messages errors
true
else
false
end
end
|