class Halibut::Core::Resource

This class represents a HAL Resource object.

spec spec spec

Attributes

embedded[R]

A collection of embedded resources, grouped by relation.

namespace[R]

A collection of namespaces defined in the document

ns[R]

A collection of namespaces defined in the document

properties[R]

All the properties set in this resource

Public Class Methods

new(href=nil, properties={}, links={}, embedded={}) click to toggle source

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

==(other) click to toggle source

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
[]=(property, value) click to toggle source
# File lib/halibut/core/resource.rb, line 83
def []=(property, value)
  tap { @properties[property] = value }
end
add_embedded_resource(relation, resource) click to toggle source

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
add_namespace(name, href) click to toggle source

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
embed_resource(relation, resource) click to toggle source

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
href() click to toggle source

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
namespaces() click to toggle source
# File lib/halibut/core/resource.rb, line 62
def namespaces
  @links['curie']
end
property(property) click to toggle source

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
set_property(property, value) click to toggle source

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
to_hash() click to toggle source

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