Class: PactBroker::Client::BaseClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty, StringToSymbol, UrlHelpers
Defined in:
lib/pact_broker/client/base_client.rb

Direct Known Subclasses

Pacticipants, Pacts, Versions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringToSymbol

#string_keys_to_symbols

Methods included from UrlHelpers

#encode_param

Constructor Details

#initialize(options) ⇒ BaseClient

Returns a new instance of BaseClient.



31
32
33
34
35
36
# File 'lib/pact_broker/client/base_client.rb', line 31

def initialize options
  @base_url = options[:base_url]
  @client_options = options[:client_options] || {}
  self.class.base_uri base_url
  self.class.basic_auth(client_options[:basic_auth][:username], client_options[:basic_auth][:password]) if client_options[:basic_auth]
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



29
30
31
# File 'lib/pact_broker/client/base_client.rb', line 29

def base_url
  @base_url
end

#client_optionsObject (readonly)

Returns the value of attribute client_options.



29
30
31
# File 'lib/pact_broker/client/base_client.rb', line 29

def client_options
  @client_options
end

Instance Method Details

#default_get_headersObject



42
43
44
# File 'lib/pact_broker/client/base_client.rb', line 42

def default_get_headers
  default_request_headers
end

#default_patch_headersObject



46
47
48
# File 'lib/pact_broker/client/base_client.rb', line 46

def default_patch_headers
  default_request_headers.merge('Content-Type' => 'application/json')
end

#default_put_headersObject



50
51
52
# File 'lib/pact_broker/client/base_client.rb', line 50

def default_put_headers
  default_request_headers.merge('Content-Type' => 'application/json')
end

#default_request_headersObject



38
39
40
# File 'lib/pact_broker/client/base_client.rb', line 38

def default_request_headers
  {'Accept' => 'application/json'}
end

#get(url, *args) ⇒ Object



72
73
74
# File 'lib/pact_broker/client/base_client.rb', line 72

def get url, *args
  self.class.get(url, *args)
end

#handle_response(response) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/pact_broker/client/base_client.rb', line 54

def handle_response response
  if response.success?
    yield
  elsif response.code == 404
    nil
  else
    raise response.body
  end
end

#patch(url, options) ⇒ Object



64
65
66
# File 'lib/pact_broker/client/base_client.rb', line 64

def patch url, options
  self.class.patch(url, options.merge(body: options[:body].to_json))
end

#put(url, *args) ⇒ Object



68
69
70
# File 'lib/pact_broker/client/base_client.rb', line 68

def put url, *args
  self.class.put(url, *args)
end