class Transit::Link

Represents a transit hypermedia link extension type. @see github.com/cognitect/transit-format @see amundsen.com/media-types/collection/format/#arrays-links

Constants

KEYS
RENDER_VALUES

Public Class Methods

new(*args) click to toggle source

@overload Link.new(hash)

@param [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

@overload Link.new(href, rel, name, render, prompt)

@param [String, Addressable::URI] href required
@param [String] rel required
@param [String] name optional
@param [String] render optional (only "link" or "image")
@param [String] prompt optional
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/transit/transit_types.rb, line 209
def ==(other)
  other.is_a?(Link) && other.to_h == to_h
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/transit/transit_types.rb, line 214
def hash
  @values.hash
end
href() click to toggle source
# File lib/transit/transit_types.rb, line 199
def href;   @href   ||= @values["href"]   end
name() click to toggle source
# File lib/transit/transit_types.rb, line 201
def name;   @name   ||= @values["name"]   end
prompt() click to toggle source
# File lib/transit/transit_types.rb, line 203
def prompt; @prompt ||= @values["prompt"] end
rel() click to toggle source
# File lib/transit/transit_types.rb, line 200
def rel;    @rel    ||= @values["rel"]    end
render() click to toggle source
# File lib/transit/transit_types.rb, line 202
def render; @render ||= @values["render"] end
to_h() click to toggle source
# File lib/transit/transit_types.rb, line 205
def to_h
  @values
end

Private Instance Methods

reconcile_values(map) click to toggle source
# File lib/transit/transit_types.rb, line 220
def reconcile_values(map)
  map.dup.tap do |m|
    m["href"] = Addressable::URI.parse(m["href"]) if m["href"].is_a?(String)
    if m["render"]
      render = m["render"].downcase
      if RENDER_VALUES.include?(render)
        m["render"] = render
      else
        raise ArgumentError, "render must be either #{RENDER_VALUES[0]} or #{RENDER_VALUES[1]}"
      end
    end
  end.freeze
end