class Halibut::Core::Resource
This class represents a HAL Resource
object.
spec spec spec
Attributes
A collection of embedded resources, grouped by relation.
A collection of links, grouped by relation
A collection of namespaces defined in the document
A collection of namespaces defined in the document
All the properties set in this resource
Public Class Methods
Initialize a new Resource
.
As defined in the spec, the resource SHOULD have a self link, but it isn't required. Also optionally, I'm toying with the idea of being able to pass in properties, links and embedded resources as parameters to this method, like suggested in github.com/locks/halibut/issues/1.
# Resource without self link (e.g. POSTing a new resource) resource = Halibut::Core::Resource.new resource.set_property :name, 'Halibut Rules' resource.set_property :winner, 'Tiger Blood' # Resource with a self link resource = Halibut::Core::Resource.new
@param [String] href Link
that will be added to the self relation.
# File lib/halibut/core/resource.rb, line 40 def initialize(href=nil, properties={}, links={}, embedded={}) @namespaces = RelationMap.new @links = RelationMap.new @embedded = RelationMap.new @embedded_arrays = RelationMap.new(single_item_arrays: true) @properties = {} add_link('self', href) if href end
Public Instance Methods
Compares two resources.
@param [Halibut::Core::Resource] other Resource
to compare to @return [true, false] Result of the comparison
# File lib/halibut/core/resource.rb, line 165 def ==(other) @properties == other.properties && @links == other.links && @embedded == other.embedded end
# File lib/halibut/core/resource.rb, line 83 def []=(property, value) tap { @properties[property] = value } end
Embeds resource in a relation array
To embed a single object, see embed_resource
.
@param [String] relation relation @param [Resource] resource resource to embed
# File lib/halibut/core/resource.rb, line 142 def add_embedded_resource(relation, resource) @embedded_arrays.add relation, resource end
Adds link to relation.
resource = Halibut::Core::Resource.new resource.add_link 'next', '/resource/2', name: 'Foo' link = resource.links['next'].first link.href # => "/resource/2" link.name # => "Foo"
@param [String] relation relation @param [String] href href @param [Hash] opts options: templated, type, name, profile,
title, hreflang
# File lib/halibut/core/resource.rb, line 121 def add_link(relation, href, opts={}) @links.add relation, Link.new(href, opts) end
Adds a namespace to the resource.
@param [String] name The name of the namespace @param [String] href The templated URI of the namespace
# File lib/halibut/core/resource.rb, line 103 def add_namespace(name, href) add_link 'curie', href, templated: true, name: name end
Embeds resource in relation
To embed many resources, call add_embedded_resource
with each item
@param [String] relation relation @param [Resource] resource resource to embed
# File lib/halibut/core/resource.rb, line 131 def embed_resource(relation, resource) warn 'Calling Halibut::Core::Resource#embed_resource for populating an array is deprecated. Use Halibut::Core::Resource#add_embeded_resource instead.' if @embedded[relation] @embedded.add relation, resource end
Returns the self link of the resource.
@return [String,nil] the self link of the resource
# File lib/halibut/core/resource.rb, line 53 def href @links.fetch('self', []).map(&:href).first end
# File lib/halibut/core/resource.rb, line 62 def namespaces @links['curie'] end
Returns the value of a property in the resource
resource = Halibut::Core::Resource.new resource.set_property :name, 'FooBar' resource.property :name # => "FooBar"
@param [String] property property
# File lib/halibut/core/resource.rb, line 95 def property(property) @properties.fetch(property, nil) end
Sets a property in the resource.
resource = Halibut::Core::Resource.new resource.set_property :name, 'FooBar' resource.property :name # => "FooBar"
@param [Object] property the key @param [Object] value the value
# File lib/halibut/core/resource.rb, line 75 def set_property(property, value) if property == '_links' || property == '_embedded' raise ArgumentError, "Argument #{property} is a reserved property" end tap { @properties[property] = value } end
Hash representation of the resource. Will ommit links and embedded keys if they're empty
@return [Hash] hash representation of the resource
# File lib/halibut/core/resource.rb, line 150 def to_hash {}.merge(@properties).tap do |h| h['_links'] = {}.merge @links unless @links.empty? combined_embedded = {} combined_embedded.merge! @embedded unless @embedded.empty? combined_embedded.merge! @embedded_arrays unless @embedded_arrays.empty? h['_embedded'] = combined_embedded unless combined_embedded.empty? end end