Class: CrAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/crapi/client.rb

Overview

Provides a connection mechanism, simple CRUD methods (#delete / #get / #patch / #post / #put), and proxy generators.

Constant Summary collapse

JSON_CONTENT_TYPE =

The content-type for JSON data.

'application/json'.freeze
FORM_CONTENT_TYPE =

The content-type for FORM data.

'application/x-www-form-urlencoded'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri, opts = {}) ⇒ Client

Returns a new instance of Client.

Options Hash (opts):

  • :insecure (true, false)

    Whether to allow insecure SSH connections (i.e. don't attempt to verify the validity of the given certificate).

  • :proxy_host (String)

    The DNS name or IP address of a proxy server to use when connecting to the target system. Maps to Net::HTTP#new's p_addr.

  • :proxy_port (Number)

    The proxy server port to use when connecting to the target system. Maps to Net::HTTP#new's p_port.

  • :proxy_username (String)

    The username to give if authorization is required to access the specified proxy host. Maps to Net::HTTP#new's p_user.

  • :proxy_password (Number)

    The password to give if authorization is required to access the specified proxy host. Maps to Net::HTTP#new's p_pass.

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/crapi/client.rb', line 57

def initialize(base_uri, opts = {})
  @base_uri = case base_uri
              when URI then base_uri
              when String then URI(base_uri)
              else raise CrAPI::ArgumentError, %(Unexpected "base_uri" type: #{base_uri.class})
              end

  @proxy_host = opts[:proxy_host]
  @proxy_port = opts[:proxy_port]
  @proxy_username = opts[:proxy_username]
  @proxy_password = opts[:proxy_password]

  @http = Net::HTTP.new(@base_uri.host, @base_uri.port,
                        @proxy_host, @proxy_port, @proxy_username, @proxy_password)
  @http.use_ssl = (@base_uri.scheme == 'https')
  @http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE if opts[:insecure].present?

  @default_headers = { 'Content-Type': JSON_CONTENT_TYPE }.with_indifferent_access
end

Instance Attribute Details

#default_headersObject

A Hash containing headers to send with every request (unless overriden elsewhere). Headers set by the user via the CRUD methods' headers still override any conflicting default header.



17
18
19
# File 'lib/crapi/client.rb', line 17

def default_headers
  @default_headers
end

Instance Method Details

#delete(path, headers: {}, query: {}) ⇒ Object

CRUD method: DELETE



112
113
114
115
116
117
118
# File 'lib/crapi/client.rb', line 112

def delete(path, headers: {}, query: {})
  headers = @default_headers.merge(headers)

  response = @http.delete(full_path(path, query: query), headers)
  ensure_success!(response)
  parse_response(response)
end

#get(path, headers: {}, query: {}) ⇒ Object

CRUD method: GET



137
138
139
140
141
142
143
# File 'lib/crapi/client.rb', line 137

def get(path, headers: {}, query: {})
  headers = @default_headers.merge(headers)

  response = @http.get(full_path(path, query: query), headers)
  ensure_success!(response)
  parse_response(response)
end

#new_proxy(segment = '/', headers: nil) ⇒ CrAPI::Proxy

Returns a new CrAPI::Proxy for this client.



89
90
91
# File 'lib/crapi/client.rb', line 89

def new_proxy(segment = '/', headers: nil)
  Proxy.new(add: segment, to: self, headers: headers)
end

#patch(path, headers: {}, query: {}, payload: {}) ⇒ Object

CRUD method: PATCH



166
167
168
169
170
171
172
173
# File 'lib/crapi/client.rb', line 166

def patch(path, headers: {}, query: {}, payload: {})
  headers = @default_headers.merge(headers)
  payload = format_payload(payload, as: headers[:'Content-Type'])

  response = @http.patch(full_path(path, query: query), payload, headers)
  ensure_success!(response)
  parse_response(response)
end

#post(path, headers: {}, query: {}, payload: {}) ⇒ Object

CRUD method: POST



196
197
198
199
200
201
202
203
# File 'lib/crapi/client.rb', line 196

def post(path, headers: {}, query: {}, payload: {})
  headers = @default_headers.merge(headers)
  payload = format_payload(payload, as: headers[:'Content-Type'])

  response = @http.post(full_path(path, query: query), payload, headers)
  ensure_success!(response)
  parse_response(response)
end

#put(path, headers: {}, query: {}, payload: {}) ⇒ Object

CRUD method: PUT



226
227
228
229
230
231
232
233
# File 'lib/crapi/client.rb', line 226

def put(path, headers: {}, query: {}, payload: {})
  headers = @default_headers.merge(headers)
  payload = format_payload(payload, as: headers[:'Content-Type'])

  response = @http.put(full_path(path, query: query), payload, headers)
  ensure_success!(response)
  parse_response(response)
end