Class: Riak::Link

Inherits:
Object show all
Includes:
Util::Escape, Util::Translation
Defined in:
lib/riak/link.rb

Overview

Represents a link from one object to another in Riak

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Escape

#escape, #maybe_escape, #maybe_unescape, #unescape

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

#initialize(url, tag) ⇒ Link #initialize(bucket, key, tag) ⇒ Link

Returns a new instance of Link.

Overloads:

  • #initialize(url, tag) ⇒ Link

    Parameters:

    • url (String)

      the url of the related resource

    • tag (String)

      the tag for the related resource

  • #initialize(bucket, key, tag) ⇒ Link

    Parameters:

    • bucket (String)

      the bucket of the related resource

    • key (String)

      the key of the related resource

    • tag (String)

      the tag for the related resource

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
# File 'lib/riak/link.rb', line 45

def initialize(*args)
  raise ArgumentError unless (2..3).include?(args.size)
  if args.size == 2
    self.url, @tag = args
  else
    @bucket, @key, @tag = args
  end
end

Instance Attribute Details

#bucketString

Returns the bucket of the related resource.

Returns:

  • (String)

    the bucket of the related resource



18
19
20
# File 'lib/riak/link.rb', line 18

def bucket
  @bucket
end

#keyString

Returns the key of the related resource.

Returns:

  • (String)

    the key of the related resource



21
22
23
# File 'lib/riak/link.rb', line 21

def key
  @key
end

#tagString Also known as: rel

Returns the relationship tag (or “rel”) of the other resource to this one.

Returns:

  • (String)

    the relationship tag (or “rel”) of the other resource to this one



13
14
15
# File 'lib/riak/link.rb', line 13

def tag
  @tag
end

Class Method Details

.parse(header_string) ⇒ Array<Link>

Returns an array of Riak::Link structs parsed from the header.

Parameters:

  • header_string (String)

    the string value of the Link: HTTP header from a Riak response

Returns:

  • (Array<Link>)

    an array of Riak::Link structs parsed from the header



32
33
34
35
36
# File 'lib/riak/link.rb', line 32

def self.parse(header_string)
  header_string.scan(%r{<([^>]+)>\s*;\s*(?:rel|riaktag)=\"([^\"]+)\"}).map do |match|
    new(match[0], match[1])
  end
end

Instance Method Details

#==(other) ⇒ Object



87
88
89
# File 'lib/riak/link.rb', line 87

def ==(other)
  other.is_a?(Link) && url == other.url && tag == other.tag
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/riak/link.rb', line 83

def eql?(other)
  self == other
end

#hashObject



79
80
81
# File 'lib/riak/link.rb', line 79

def hash
  self.to_s.hash
end

#inspectObject



71
72
73
# File 'lib/riak/link.rb', line 71

def inspect
  to_s
end

#to_s(new_scheme = false) ⇒ Object



75
76
77
# File 'lib/riak/link.rb', line 75

def to_s(new_scheme = false)
  %Q[<#{url(new_scheme)}>; riaktag="#{tag}"]
end

#to_walk_specObject



91
92
93
94
# File 'lib/riak/link.rb', line 91

def to_walk_spec
  raise t("bucket_link_conversion") if tag == "up" || key.nil?
  WalkSpec.new(:bucket => bucket, :tag => tag)
end

#url(new_scheme = false) ⇒ String

Returns the URL (relative or absolute) of the related resource.

Returns:

  • (String)

    the URL (relative or absolute) of the related resource



55
56
57
58
59
60
61
62
63
# File 'lib/riak/link.rb', line 55

def url(new_scheme = false)
  return @url unless @bucket

  if new_scheme
    "/buckets/#{escape(bucket)}" + (key.blank? ? "" : "/keys/#{escape(key)}")
  else
    "/riak/#{escape(bucket)}" + (key.blank? ? "" : "/#{escape(key)}")
  end
end

#url=(value) ⇒ Object



65
66
67
68
69
# File 'lib/riak/link.rb', line 65

def url=(value)
  @url = value
  @bucket = unescape($1) if value =~ %r{^/buckets/([^/]+)/?} || value =~ %r{^/[^/]+/([^/]+)/?}
  @key = unescape($1) if value =~ %r{^/buckets/[^/]+/keys/([^/]+)/?} || value =~ %r{^/[^/]+/[^/]+/([^/]+)/?}
end