Class: CLX::HTTPClient
- Inherits:
-
Object
- Object
- CLX::HTTPClient
- Defined in:
- lib/clx_api/http_client.rb
Overview
HTTP Client class for making RESTful HTTP-requests
Instance Attribute Summary collapse
-
#base_url ⇒ Object
Gives access to base_url so this can be modified.
-
#http_adapter ⇒ Object
Gives access to the adapter so Auth credentials can be monipulated.
Instance Method Summary collapse
-
#delete(url) ⇒ Object
Make a DELETE-request to specified API-path (Not implemented).
-
#get(url) ⇒ hash
Make a GET-request to specified API-path.
-
#get_full_url(url) ⇒ Mixed
private
Build the full API request URL.
-
#initialize(base_url, http_adapter) ⇒ HTTPClient
constructor
Initializer.
-
#parseResponse(response) ⇒ JSON
private
Parse response from a request and return body content as JSON.
-
#post(url, data = nil) ⇒ Object
Make a POST-request to specified API-path (Not implemented).
-
#put(url, data = nil) ⇒ Object
Make a PUT-request to specified API-path (Not implemented).
Constructor Details
#initialize(base_url, http_adapter) ⇒ HTTPClient
Initializer
17 18 19 20 |
# File 'lib/clx_api/http_client.rb', line 17 def initialize(base_url, http_adapter) @base_url = base_url @http_adapter = http_adapter end |
Instance Attribute Details
#base_url ⇒ Object
Gives access to base_url so this can be modified
8 9 10 |
# File 'lib/clx_api/http_client.rb', line 8 def base_url @base_url end |
#http_adapter ⇒ Object
Gives access to the adapter so Auth credentials can be monipulated
12 13 14 |
# File 'lib/clx_api/http_client.rb', line 12 def http_adapter @http_adapter end |
Instance Method Details
#delete(url) ⇒ Object
Make a DELETE-request to specified API-path
(Not implemented)
60 61 62 63 64 |
# File 'lib/clx_api/http_client.rb', line 60 def delete(url) uri = get_full_url(url) response = @http_adapter.delete(uri) return parseResponse(response) end |
#get(url) ⇒ hash
Make a GET-request to specified API-path
26 27 28 29 30 |
# File 'lib/clx_api/http_client.rb', line 26 def get(url) uri = get_full_url(url) response = @http_adapter.get(uri) return parseResponse(response) end |
#get_full_url(url) ⇒ Mixed (private)
Build the full API request URL
75 76 77 78 |
# File 'lib/clx_api/http_client.rb', line 75 def get_full_url(url) full_url = @base_url + url return URI(URI.encode(full_url)) end |
#parseResponse(response) ⇒ JSON (private)
Parse response from a request and return body content as JSON
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/clx_api/http_client.rb', line 83 def parseResponse(response) begin result = JSON.parse(response.body, object_class: OpenStruct) rescue raise CLXException, 'Unable to parse JSON response' end if(response.code.to_i > 399) = (result.error && result.error.) ? result.error. : 'No error message available' code = (result.error && result.error.code) ? result.error.code : 'No error code available' raise CLXAPIException.new(, code), '400: Bad request' if response.code == 400 raise CLXAPIException.new(, code), '401: Unauthorized' if response.code == 401 raise CLXAPIException.new(, code), '403: Forbidden' if response.code == 403 raise CLXAPIException.new(, code), '404: Not Found' if response.code == 404 raise CLXAPIException.new(, code), 'Unknown error' end return result end |
#post(url, data = nil) ⇒ Object
Make a POST-request to specified API-path
(Not implemented)
38 39 40 41 42 |
# File 'lib/clx_api/http_client.rb', line 38 def post(url, data = nil) uri = get_full_url(url) response = @http_adapter.post(uri, data) return parseResponse(response) end |
#put(url, data = nil) ⇒ Object
Make a PUT-request to specified API-path
(Not implemented)
50 51 52 53 54 |
# File 'lib/clx_api/http_client.rb', line 50 def put(url, data = nil) uri = get_full_url(url) response = @http_adapter.put(uri, data) return parseResponse(response) end |