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/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
-
#get(url, options = {}) ⇒ Object
Returns a ‘Representation` of the resource identified by `url`.
-
#initialize(options = {}) ⇒ HalClient
constructor
Initializes a new client instance.
-
#post(url, data, options = {}) ⇒ Object
Post a ‘Representation` or `String` to the resource identified at `url`.
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(={}) @default_accept = .fetch(:accept, 'application/hal+json') @default_content_type = .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, ={}) resp = RestClient.get url, () 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, ={}) resp = RestClient.post url, data, () begin Representation.new hal_client: self, parsed_json: MultiJson.load(resp) rescue MultiJson::ParseError, InvalidRepresentationError => e resp end end |