Module: HTTP::RestClient::DSL
- Defined in:
- lib/http/rest_client/dsl.rb
Overview
Client DSL, mostly inpired from Nestful and HTTP::Chainable APIs.
Instance Method Summary collapse
-
#accept(type) ⇒ Hash
Sets the client expected HTTP response media type.
-
#content_type(type) ⇒ Hash
Sets the client expected HTTP request content type.
-
#endpoint(value = nil) ⇒ String
Defines the client endpoint.
-
#error_response?(response, _parsed_response) ⇒ TrueClass
Validate error response.
-
#extract_error(response, _parsed_response) ⇒ String
Extracts the error message from the response.
-
#handle_response(response) ⇒ Hash
Response handler, raises an [HTTP::RestClient::ResponseError] on errors.
-
#headers(header = nil) ⇒ Hash
Updates the client headers.
-
#parse_response(response) ⇒ Object
Will try to parse the response.
-
#path(value = nil) ⇒ String
Defines the client resource path.
-
#proxy(*value) ⇒ String
Defines the client proxy hosts.
-
#request(verb, uri, options = {}) ⇒ Object
Makes an HTTP request and returns a parsed response where possible.
-
#uri(*parts) ⇒ URI
Extends the endpoint and path full URL with new parts.
-
#url ⇒ URI
Parses and returns the endpoint and path full URL.
Instance Method Details
#accept(type) ⇒ Hash
Sets the client expected HTTP response media type
79 80 81 |
# File 'lib/http/rest_client/dsl.rb', line 79 def accept(type) headers(HTTP::Headers::ACCEPT => HTTP::MimeType.normalize(type)) end |
#content_type(type) ⇒ Hash
Sets the client expected HTTP request content type
71 72 73 |
# File 'lib/http/rest_client/dsl.rb', line 71 def content_type(type) headers(HTTP::Headers::CONTENT_TYPE => HTTP::MimeType.normalize(type)) end |
#endpoint(value = nil) ⇒ String
Defines the client endpoint. Inheritable-safe
32 33 34 35 36 37 38 |
# File 'lib/http/rest_client/dsl.rb', line 32 def endpoint(value = nil) @endpoint = value if value return @endpoint if @endpoint superclass.respond_to?(:endpoint) ? superclass.endpoint : nil end |
#error_response?(response, _parsed_response) ⇒ TrueClass
Validate error response
Looks at the response code by default.
158 159 160 |
# File 'lib/http/rest_client/dsl.rb', line 158 def error_response?(response, _parsed_response) !(200..299).cover?(response.code) end |
#extract_error(response, _parsed_response) ⇒ String
Extracts the error message from the response
146 147 148 |
# File 'lib/http/rest_client/dsl.rb', line 146 def extract_error(response, _parsed_response) [response.status.to_s, response.body.to_str].reject(&:empty?).join(': ') end |
#handle_response(response) ⇒ Hash
Response handler, raises an [HTTP::RestClient::ResponseError] on errors
119 120 121 122 123 124 125 |
# File 'lib/http/rest_client/dsl.rb', line 119 def handle_response(response) parsed = parse_response(response) return parsed unless error_response?(response, parsed) raise ResponseError.new(extract_error(response, parsed), parsed) end |
#headers(header = nil) ⇒ Hash
Updates the client headers
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/http/rest_client/dsl.rb', line 17 def headers(header = nil) if header.is_a?(Hash) @headers ||= {} @headers.merge!(header) end return @headers if @headers superclass.respond_to?(:headers) ? superclass.headers : nil end |
#parse_response(response) ⇒ Object
Will try to parse the response
Will return nothing on failure.
134 135 136 137 138 |
# File 'lib/http/rest_client/dsl.rb', line 134 def parse_response(response) response.parse rescue HTTP::Error nil end |
#path(value = nil) ⇒ String
Defines the client resource path. Inheritable-safe
44 45 46 47 48 49 50 |
# File 'lib/http/rest_client/dsl.rb', line 44 def path(value = nil) @path = value if value return @path if @path superclass.respond_to?(:path) ? superclass.path : nil end |
#proxy(*value) ⇒ String
Defines the client proxy hosts. Inheritable-safe
Expects an array with host, port, username and password. The last two are optional.
59 60 61 62 63 64 65 |
# File 'lib/http/rest_client/dsl.rb', line 59 def proxy(*value) @proxy = value if value && !value.empty? return @proxy if @proxy superclass.respond_to?(:proxy) ? superclass.proxy : nil end |
#request(verb, uri, options = {}) ⇒ Object
Makes an HTTP request and returns a parsed response where possible
109 110 111 112 113 |
# File 'lib/http/rest_client/dsl.rb', line 109 def request(verb, uri, = {}) client = HTTP::Client.new(headers: headers) client = client.via(*proxy) if proxy handle_response(client.request(verb, uri, )) end |
#uri(*parts) ⇒ URI
Extends the endpoint and path full URL with new parts
94 95 96 97 98 99 100 101 |
# File 'lib/http/rest_client/dsl.rb', line 94 def uri(*parts) # If an absolute URI already return parts.first if parts.first.is_a?(URI) && parts.first.host joined = [url, *parts].map(&:to_s).reject(&:empty?) * '/' URI.parse(joined) end |
#url ⇒ URI
Parses and returns the endpoint and path full URL
86 87 88 |
# File 'lib/http/rest_client/dsl.rb', line 86 def url URI.parse(endpoint).join(path.to_s).to_s end |