Module: Halibut::Adapter::JSON

Defined in:
lib/halibut/adapter/json.rb

Overview

This adapter converts Halibut::Core::Resources to JSON encoded strings and back.

Examples:

resource = Halibut::Builder.new('http://example.com') do
  link "posts", '/posts'
  link "author", 'http://locks.io'

  property "title", 'Entry point'
end.resource

dumped = Halibut::Adapter::JSON.dump resource
# => "{\"title\":\"Entry point\",\"_links\":{\"self\":{\"href\":\"http://example.com\"},\"posts\":{\"href\":\"/posts\"},\"author\":{\"href\":\"http://locks.io\"}}}"

loaded = Halibut::Adapter::JSON.load dumped
resource == loaded
# => true

Defined Under Namespace

Modules: ConvenienceMethods, InstanceMethods Classes: ResourceExtractor

Class Method Summary collapse

Class Method Details

.dump(resource) ⇒ Object

Returns a JSON string representation of an Halibut::Core::Resource


33
34
35
# File 'lib/halibut/adapter/json.rb', line 33

def self.dump(resource)
  ::JSON.dump resource.to_hash
end

.parse(json) ⇒ Object

Returns an Halibut::Core::Resource from a JSON string

Parameters:

  • json (StringIO)

    the JSON to parse


28
29
30
# File 'lib/halibut/adapter/json.rb', line 28

def self.parse(json)
  ResourceExtractor.new(json).resource
end