Module: RSpec::Hal::Helpers

Defined in:
lib/rspec/hal/helpers.rb

Instance Method Summary collapse

Instance Method Details

#parse_hal(jsonish) ⇒ Object

Returns HalClient::Representation for ‘jsonish` (or HalClient::Collection if `jsonish` is a RFC 6573 collection)

jsonish - A HAL document (as a string or pre-parsed hash) or

an object that can be converted into one via its `#to_hal`
or `#to_json` methods.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec/hal/helpers.rb', line 10

def parse_hal(jsonish)
  json = if jsonish.kind_of? String
           jsonish

         elsif jsonish.respond_to? :to_hal
           jsonish.to_hal

         else jsonish.respond_to? :to_json
           jsonish.to_json
         end

  repr = HalClient::Representation.new(parsed_json: MultiJson.load(json))

  if repr.has_related? "item"
    HalClient::Collection.new(repr)
  else
    repr
  end
end