Class: HalClient

Inherits:
Object
  • Object
show all
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.11.1"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EntryPointCovenienceMethods

post

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 authorization header value to use
  for the request or a string which will always be the value of
  the authorization 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(options={})
  @default_message_request_headers = HTTP::Headers.new
  @default_entity_request_headers = HTTP::Headers.new
  @auth_helper = as_callable(options.fetch(:authorization, NullAuthHelper))
  @base_client ||= options[:base_client]

  default_message_request_headers.set('Accept', options[:accept]) if
    options[:accept]
  # Explicit accept option has precedence over accepts in the
  # headers option.

  options.fetch(:headers, {}).each do |name, value|
    if entity_header_field? name
      default_entity_request_headers.add(name, value)
    else
      default_message_request_headers.add(name, value)
    end
  end

  default_entity_request_headers.set('Content-Type', options[:content_type]) if
    options[: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(default_message_request_headers.get('Accept')) +
    ['application/hal+json;q=0']
  default_message_request_headers.set('Accept', accept_values.join(", "))
  # We can work with HAL so provide a back stop accept.
end

Class Method Details

.delete(url, headers = {}) ⇒ Object



141
142
143
# File 'lib/hal_client.rb', line 141

def delete(url, headers={})
  new.delete(url, headers)
end

Instance Method Details

#delete(url, headers = {}) ⇒ Object

Delete a Representation or String to the resource identified at url.

url - The URL of the resource of interest. headers - custom header fields to use for this request



129
130
131
132
133
134
135
136
137
138
# File 'lib/hal_client.rb', line 129

def delete(url, headers={})
  headers = auth_headers(url).merge(headers)

  begin
    interpret_response client_for_post(override_headers: headers)
                        .request(:delete, url)
  rescue HttpError => e
    fail e.class.new("DELETE <#{url}> failed with code #{e.response.status}", e.response)
  end
end

#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