Method: HalClient#initialize

Defined in:
lib/hal_client.rb

#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