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.1"

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.


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

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`



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

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`



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

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