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,
lib/hal_client/representation_editor.rb
Overview
Adapter used to access resources.
Defined Under Namespace
Modules: EntryPointCovenienceMethods Classes: Collection, CurieResolver, HttpError, LinksSection, Representation, RepresentationEditor, 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.9.0"
Instance Method Summary collapse
-
#get(url, headers = {}) ⇒ Object
Returns a
Representationof the resource identified byurl. -
#initialize(options = {}) ⇒ HalClient
constructor
Initializes a new client instance.
Methods included from EntryPointCovenienceMethods
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.
:authorization - a `#call`able which takes the url being
requested and returns the header value to use
for the request or a string which will always be the value of
the header
:headers - a hash of other headers to send on each request.
:base_client - An HTTP::Client object to use.
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 58 59 60 61 62 63 64 65 66 |
# File 'lib/hal_client.rb', line 33 def initialize(={}) = HTTP::Headers.new @default_entity_request_headers = HTTP::Headers.new @auth_helper = as_callable(.fetch(:authorization, NullAuthHelper)) @base_client ||= [:base_client] .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
72 73 74 75 76 77 78 |
# File 'lib/hal_client.rb', line 72 def get(url, headers={}) headers = auth_headers(url).merge(headers) interpret_response client_for_get(override_headers: headers).get(url) rescue HttpError => e fail e.class.new("GET <#{url}> failed with code #{e.response.status}", e.response) end |