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.2.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.
-
#patch(url, data, headers = {}) ⇒ Object
Patch a
RepresentationorStringto the resource identified aturl. -
#post(url, data, headers = {}) ⇒ Object
Post a
RepresentationorStringto the resource identified aturl. -
#put(url, data, headers = {}) ⇒ Object
Put 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.
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 58 59 |
# File 'lib/hal_client.rb', line 28 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
65 66 67 |
# File 'lib/hal_client.rb', line 65 def get(url, headers={}) interpret_response client_for_get(override_headers: headers).get(url) end |
#patch(url, data, headers = {}) ⇒ Object
Patch 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
104 105 106 107 108 109 110 111 112 |
# File 'lib/hal_client.rb', line 104 def patch(url, data, headers={}) req_body = if data.respond_to? :to_hal data.to_hal else data end interpret_response client_for_post(override_headers: headers).patch(url, body: req_body) 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
74 75 76 77 78 79 80 81 82 |
# File 'lib/hal_client.rb', line 74 def post(url, data, headers={}) req_body = if data.respond_to? :to_hal data.to_hal else data end interpret_response client_for_post(override_headers: headers).post(url, body: req_body) end |
#put(url, data, headers = {}) ⇒ Object
Put 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
89 90 91 92 93 94 95 96 97 |
# File 'lib/hal_client.rb', line 89 def put(url, data, headers={}) req_body = if data.respond_to? :to_hal data.to_hal else data end interpret_response client_for_post(override_headers: headers).put(url, body: req_body) end |