Class: Halibut::Core::Link

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/halibut/core/link.rb

Overview

This class represents a HAL Link object.

spec spec spec.

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href, opts = {}) ⇒ Halibut::Core::Link

Returns an instance of a HAL Link object

If you pass an options that is not one of the reserved link properties as defined by the spec, they are dropped. It should possibly raise an error, hafta think about it.

Examples:

Link with no options

link = Link.new('http://homeopathy.org')

Link with name and type options

link = Link.new('http://homeopathy.org', name: 'Homeopath'
                                       , type: 'text/html')

Parameters:

  • href (String)

    URI or URI Template

  • opts (Hash) (defaults to: {})

    Options: type, name, profile, title, hreflang


31
32
33
34
# File 'lib/halibut/core/link.rb', line 31

def initialize(href, opts={})
  @href    = href
  @options = Options.new opts
end

Instance Attribute Details

#hrefObject (readonly)

The URI associated with this link.


9
10
11
# File 'lib/halibut/core/link.rb', line 9

def href
  @href
end

Instance Method Details

#==(other) ⇒ true, false

Generic comparison method.

Two objects are the same if they have the same href and the same options.

Examples:

link_one = Link.new('/link', name: 'One', type: 'text/html')
link_two = Link.new('/link', name: 'One', type: 'text/html')
link_one == link_two
# => true

Parameters:

  • other (Link)

    Link object to compare to

Returns:

  • (true, false)

    return of the comparison of the two objects


60
61
62
# File 'lib/halibut/core/link.rb', line 60

def ==(other)
  @href == other.href && @options == other.options
end

#to_hashHash

Simply returns a hash of the href and the options that are not empty.

Examples:

link = Link.new('/links', name: 'Links')
link.to_hash
# => { "href" => "/links", "name" => "links" }

Returns:

  • (Hash)

    hash from Link Object


44
45
46
# File 'lib/halibut/core/link.rb', line 44

def to_hash
  { 'href' => href }.merge @options
end