Class: PactBroker::Api::Resources::BaseResource

Inherits:
Webmachine::Resource
  • Object
show all
Includes:
PactBrokerUrls, Authentication, Logging, Services
Defined in:
lib/pact_broker/api/resources/base_resource.rb

Constant Summary

Constants included from Logging

Logging::LOG_DIR, Logging::LOG_FILE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #log_error, #logger, #logger=

Methods included from Authentication

#authenticated?

Methods included from PactBrokerUrls

#badge_url_for_latest_pact, #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_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, #tag_url, #tags_url, #triggered_webhook_logs_url, #url_encode, #verification_publication_url, #verification_url, #verification_url_from_params, #version_url, #version_url_from_params, #versions_url, #webhook_execution_url, #webhook_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

#initializeBaseResource



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

#userObject

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_urlObject



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_providedObject



65
66
67
# File 'lib/pact_broker/api/resources/base_resource.rb', line 65

def charsets_provided
  [['utf-8', :encode]]
end

#consumer_nameObject



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

#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

#finish_requestObject



37
38
39
40
41
42
# File 'lib/pact_broker/api/resources/base_resource.rb', line 37

def finish_request
  if update_matrix_after_request?
    matrix_service.refresh(identifier_from_path)
  end
  PactBroker.configuration.after_resource.call(self)
end

#forbidden?Boolean



48
49
50
51
# File 'lib/pact_broker/api/resources/base_resource.rb', line 48

def forbidden?
  return false if PactBroker.configuration.authorize.nil?
  !PactBroker.configuration.authorize.call(self, {})
end

#handle_exception(e) ⇒ Object



82
83
84
# File 'lib/pact_broker/api/resources/base_resource.rb', line 82

def handle_exception e
  PactBroker::Api::Resources::ErrorHandler.call(e, request, response)
end

#identifier_from_pathObject 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) #Not load! Otherwise it will try to load Ruby classes.
    false
  rescue StandardError => e
    logger.error "Error parsing JSON #{e} - #{request_body}"
    set_json_error_message "Error parsing JSON - #{e.message}"
    response.headers['Content-Type'] = 'application/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?(authorization_header)
  authenticated?(self, authorization_header)
end

#pact_paramsObject



94
95
96
# File 'lib/pact_broker/api/resources/base_resource.rb', line 94

def pact_params
  @pact_params ||= PactBroker::Pacts::PactParams.from_request request, path_info
end

#pacticipant_nameObject



120
121
122
# File 'lib/pact_broker/api/resources/base_resource.rb', line 120

def pacticipant_name
  identifier_from_path[:pacticipant_name]
end

#paramsObject



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_keysObject



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_nameObject



116
117
118
# File 'lib/pact_broker/api/resources/base_resource.rb', line 116

def provider_name
  identifier_from_path[:provider_name]
end

#request_bodyObject



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_urlObject



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.headers['Content-Type'] = 'application/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.headers['Content-Type'] = 'application/json;charset=utf-8'
  response.body = {errors: errors}.to_json
end

#update_matrix_after_request?Boolean



33
34
35
# File 'lib/pact_broker/api/resources/base_resource.rb', line 33

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

#with_matrix_refresh(&block) ⇒ Object



152
153
154
# File 'lib/pact_broker/api/resources/base_resource.rb', line 152

def with_matrix_refresh &block
  matrix_service.refresh(identifier_from_path, &block)
end