Class: PactBroker::Client::BaseClient
- Inherits:
-
Object
- Object
- PactBroker::Client::BaseClient
show all
- Includes:
- HTTParty, StringToSymbol, UrlHelpers
- Defined in:
- lib/pact_broker/client/base_client.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#string_keys_to_symbols
Methods included from UrlHelpers
#encode_param, #encode_query_param
Constructor Details
#initialize(options) ⇒ BaseClient
Returns a new instance of BaseClient.
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/pact_broker/client/base_client.rb', line 39
def initialize options
@base_url = options[:base_url]
@client_options = options[:client_options] || {}
@verbose = @client_options[:verbose]
self.class.base_uri base_url
self.class.debug_output($stderr) if verbose?
self.class.basic_auth(client_options[:basic_auth][:username], client_options[:basic_auth][:password]) if client_options[:basic_auth]
self.class.('Authorization' => "Bearer #{client_options[:token]}") if client_options[:token]
self.class.ssl_ca_file(ENV['SSL_CERT_FILE']) if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
self.class.ssl_ca_path(ENV['SSL_CERT_DIR']) if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
37
38
39
|
# File 'lib/pact_broker/client/base_client.rb', line 37
def base_url
@base_url
end
|
#client_options ⇒ Object
Returns the value of attribute client_options.
37
38
39
|
# File 'lib/pact_broker/client/base_client.rb', line 37
def client_options
@client_options
end
|
Instance Method Details
55
56
57
|
# File 'lib/pact_broker/client/base_client.rb', line 55
def
end
|
59
60
61
|
# File 'lib/pact_broker/client/base_client.rb', line 59
def
.merge('Content-Type' => 'application/json')
end
|
63
64
65
|
# File 'lib/pact_broker/client/base_client.rb', line 63
def
.merge('Content-Type' => 'application/json')
end
|
51
52
53
|
# File 'lib/pact_broker/client/base_client.rb', line 51
def
{'Accept' => 'application/hal+json, application/json'}
end
|
#get(url, *args) ⇒ Object
100
101
102
|
# File 'lib/pact_broker/client/base_client.rb', line 100
def get url, *args
self.class.get(url, *args)
end
|
#handle_response(response) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/pact_broker/client/base_client.rb', line 67
def handle_response response
if response.success?
yield response
elsif response.code == 404
nil
elsif response.code == 401
raise Error.new("Authentication failed")
else
error_message = nil
begin
errors = JSON.parse(response.body)['errors']
error_message = if errors.is_a?(Array)
errors.join("\n")
elsif errors.is_a?(Hash)
errors.collect{ |key, value| "#{key}: #{value}" }.join("\n")
else
response.body
end
rescue
raise Error.new(response.body)
end
raise Error.new(error_message)
end
end
|
#patch(url, options) ⇒ Object
92
93
94
|
# File 'lib/pact_broker/client/base_client.rb', line 92
def patch url, options
self.class.patch(url, options.merge(body: options[:body].to_json))
end
|
#put(url, *args) ⇒ Object
96
97
98
|
# File 'lib/pact_broker/client/base_client.rb', line 96
def put url, *args
self.class.put(url, *args)
end
|
#url_for_relation(relation_name, params) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/pact_broker/client/base_client.rb', line 104
def url_for_relation relation_name, params
handle_response(get("/", headers: )) do | response |
relation = (JSON.parse(response.body)['_links'] || {})[relation_name]
if relation
url = relation['href']
params.each do | (key, value) |
url = url.gsub("{#{key}}", value)
end
url
else
raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version.")
end
end
end
|
#verbose? ⇒ Boolean
119
120
121
|
# File 'lib/pact_broker/client/base_client.rb', line 119
def verbose?
@verbose
end
|