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



182
183
184
185
186
187
188
189
190
# File 'lib/transit/transit_types.rb', line 182

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?



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

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

#hashObject



207
208
209
# File 'lib/transit/transit_types.rb', line 207

def hash
  @values.hash
end

#hrefObject



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

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

#nameObject



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

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

#promptObject



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

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

#relObject



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

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

#renderObject



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

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

#to_hObject



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

def to_h
  @values
end