Class: CrAPI::Client
- Inherits:
-
Object
- Object
- CrAPI::Client
- Defined in:
- lib/crapi/client.rb
Overview
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
-
#default_headers ⇒ Object
A Hash containing headers to send with every request (unless overriden elsewhere).
Instance Method Summary collapse
-
#delete(path, headers: {}, query: {}) ⇒ Object
CRUD method: DELETE.
-
#get(path, headers: {}, query: {}) ⇒ Object
CRUD method: GET.
-
#initialize(base_uri, opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#new_proxy(segment = '/', headers: nil) ⇒ CrAPI::Proxy
Returns a new CrAPI::Proxy for this client.
-
#patch(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PATCH.
-
#post(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: POST.
-
#put(path, headers: {}, query: {}, payload: {}) ⇒ Object
CRUD method: PUT.
Constructor Details
#initialize(base_uri, opts = {}) ⇒ Client
Returns a new instance of Client.
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_headers ⇒ Object
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 |