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/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, Representation, RepresentationSet

Constant Summary collapse

InvalidRepresentationError =
Class.new(StandardError)
NotACollectionError =
Class.new(StandardError)
VERSION =
"2.2.0"

Instance Method Summary collapse

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.


20
21
22
23
# File 'lib/hal_client.rb', line 20

def initialize(options={})
  @default_accept = options.fetch(:accept, 'application/hal+json')
  @default_content_type = options.fetch(:content_type, 'application/hal+json')
end

Instance Method Details

#get(url, options = {}) ⇒ Object

Returns a ‘Representation` of the resource identified by `url`.

url - The URL of the resource of interest. options - set of options to pass to ‘RestClient#get`



29
30
31
32
# File 'lib/hal_client.rb', line 29

def get(url, options={})
  resp = RestClient.get url, get_options(options)
  Representation.new hal_client: self, parsed_json: MultiJson.load(resp)
end

#post(url, data, options = {}) ⇒ 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` options - set of options to pass to `RestClient#post`



39
40
41
42
43
44
45
46
47
# File 'lib/hal_client.rb', line 39

def post(url, data, options={})
  resp = RestClient.post url, data, post_options(options)

  begin
    Representation.new hal_client: self, parsed_json: MultiJson.load(resp)
  rescue MultiJson::ParseError, InvalidRepresentationError => e
    resp
  end
end