Class: HalClient
- Inherits:
-
Object
- Object
- HalClient
- Extended by:
- EntryPointCovenienceMethods
- Defined in:
- lib/hal_client.rb,
lib/hal_client/errors.rb,
lib/hal_client/version.rb,
lib/hal_client/collection.rb,
lib/hal_client/links_section.rb,
lib/hal_client/curie_resolver.rb,
lib/hal_client/representation.rb,
lib/hal_client/representation_set.rb
Overview
Adapter used to access resources.
Defined Under Namespace
Modules: EntryPointCovenienceMethods Classes: Collection, CurieResolver, HttpError, LinksSection, Representation, RepresentationSet
Constant Summary collapse
- InvalidRepresentationError =
The representation is not a valid HAL document.
Class.new(StandardError)
- NotACollectionError =
The representation is not a HAL collection
Class.new(StandardError)
- HttpClientError =
Server response with a 4xx status code
Class.new(HttpError)
- HttpServerError =
Server responded with a 5xx status code
Class.new(HttpError)
- VERSION =
"3.1"
Instance Method Summary collapse
-
#get(url, headers = {}) ⇒ Object
Returns a
Representationof the resource identified byurl. -
#initialize(options = {}) ⇒ HalClient
constructor
Initializes a new client instance.
-
#post(url, data, headers = {}) ⇒ Object
Post a
RepresentationorStringto the resource identified aturl.
Constructor Details
#initialize(options = {}) ⇒ HalClient
Initializes a new client instance
options - hash of configuration options
:accept - one or more content types that should be
prepended to the `Accept` header field of each request.
:content_type - a single content type that should be
prepended to the `Content-Type` header field of each request.
:headers - a hash of other headers to send on each request.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hal_client.rb', line 26 def initialize(={}) = HTTP::Headers.new @default_entity_request_headers = HTTP::Headers.new .set('Accept', [:accept]) if [:accept] # Explicit accept option has precedence over accepts in the # headers option. .fetch(:headers, {}).each do |name, value| if entity_header_field? name default_entity_request_headers.add(name, value) else .add(name, value) end end default_entity_request_headers.set('Content-Type', [:content_type]) if [:content_type] # Explicit content_content options has precedence over content # type in the headers option. default_entity_request_headers.set('Content-Type', 'application/hal+json') unless default_entity_request_headers['Content-Type'] # We always want a content type. If the user doesn't explicitly # specify one we provide a default. accept_values = Array(.get('Accept')) + ['application/hal+json;q=0'] .set('Accept', accept_values.join(", ")) # We can work with HAL so provide a back stop accept. end |
Instance Method Details
#get(url, headers = {}) ⇒ Object
Returns a Representation of the resource identified by url.
url - The URL of the resource of interest. headers - custom header fields to use for this request
63 64 65 |
# File 'lib/hal_client.rb', line 63 def get(url, headers={}) interpret_response client_for_get(override_headers: headers).get(url) end |
#post(url, data, headers = {}) ⇒ Object
Post a Representation or String to the resource identified at url.
url - The URL of the resource of interest. data - a String or an object that responds to #to_hal headers - custom header fields to use for this request
72 73 74 75 76 77 78 79 80 |
# File 'lib/hal_client.rb', line 72 def post(url, data, headers={}) req_body = if data.respond_to? :to_hal data.to_hal else data end interpret_response client_for_post(headers).post(url, body: req_body) end |