Class: Transit::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/transit/transit_types.rb

Overview

Represents a transit hypermedia link extension type.

Constant Summary collapse

KEYS =
["href", "rel", "name", "render", "prompt"]
RENDER_VALUES =
["link", "image"]

Instance Method Summary collapse

Constructor Details

#Link.new(hash) ⇒ Link #Link.new(href, rel, name, render, prompt) ⇒ Link

Returns a new instance of Link.

Overloads:

  • #Link.new(hash) ⇒ Link

    Parameters:

    • hash (Hash)

      Valid keys are: "href" required, String or Addressable::URI "rel" required, String "name" optional, String "render" optional, String (only "link" or "image") "prompt" optional, String

  • #Link.new(href, rel, name, render, prompt) ⇒ Link

    Parameters:

    • href (String, Addressable::URI)

      required

    • rel (String)

      required

    • name (String)

      optional

    • render (String)

      optional (only "link" or "image")

    • prompt (String)

      optional


189
190
191
192
193
194
195
196
197
# File 'lib/transit/transit_types.rb', line 189

def initialize(*args)
  @values = if args[0].is_a?(Hash)
              reconcile_values(args[0])
            elsif args.length >= 2 && (args[0].is_a?(Addressable::URI) || args[0].is_a?(String))
              reconcile_values(Hash[KEYS.zip(args)])
            else
              raise ArgumentError, "The first argument to Link.new can be a URI, String or a Hash. When the first argument is a URI or String, the second argument, rel, must present."
            end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


209
210
211
# File 'lib/transit/transit_types.rb', line 209

def ==(other)
  other.is_a?(Link) && other.to_h == to_h
end

#hashObject


214
215
216
# File 'lib/transit/transit_types.rb', line 214

def hash
  @values.hash
end

#hrefObject


199
# File 'lib/transit/transit_types.rb', line 199

def href;   @href   ||= @values["href"]   end

#nameObject


201
# File 'lib/transit/transit_types.rb', line 201

def name;   @name   ||= @values["name"]   end

#promptObject


203
# File 'lib/transit/transit_types.rb', line 203

def prompt; @prompt ||= @values["prompt"] end

#relObject


200
# File 'lib/transit/transit_types.rb', line 200

def rel;    @rel    ||= @values["rel"]    end

#renderObject


202
# File 'lib/transit/transit_types.rb', line 202

def render; @render ||= @values["render"] end

#to_hObject


205
206
207
# File 'lib/transit/transit_types.rb', line 205

def to_h
  @values
end