Class: TentClient::LinkHeader::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/tent-client/link_header.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, attributes = {}) ⇒ Link

Returns a new instance of Link.



45
46
47
48
# File 'lib/tent-client/link_header.rb', line 45

def initialize(uri, attributes = {})
  @uri = uri
  @attributes = indifferent_hash(attributes)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



23
24
25
# File 'lib/tent-client/link_header.rb', line 23

def attributes
  @attributes
end

#uriObject

Returns the value of attribute uri.



23
24
25
# File 'lib/tent-client/link_header.rb', line 23

def uri
  @uri
end

Class Method Details

.parse(link_text) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tent-client/link_header.rb', line 25

def self.parse(link_text)
  s = StringScanner.new(link_text)
  s.scan(/[^<]+/)
  link = s.scan(/<[^\s]+>/)

  raise MalformedLinkHeader.new("Link: #{s.inspect}") unless link

  link = link[1..-2]

  s.scan(/[^a-z]+/)
  attrs = {}
  while attr = s.scan(/[a-z0-9*\-]+=/)
    next if attr =~ /\*/
    val = s.scan(/".+?"|[^\s";]+/).sub(/\A"/, '').sub(/"\Z/, '')
    attrs[attr[0..-2]] = val
    s.scan(/[^a-z]+/)
  end
  new(link, attrs)
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
# File 'lib/tent-client/link_header.rb', line 50

def ==(other)
  false unless is_a?(self.class)
  uri == other.uri && attributes == other.attributes
end

#[](k) ⇒ Object



55
56
57
# File 'lib/tent-client/link_header.rb', line 55

def [](k)
  attributes[k]
end

#[]=(k, v) ⇒ Object



59
60
61
# File 'lib/tent-client/link_header.rb', line 59

def []=(k, v)
  attributes[k.to_s] = v.to_s
end

#to_sObject



63
64
65
66
# File 'lib/tent-client/link_header.rb', line 63

def to_s
  attr_string = "; " + attributes.sort.map { |k,v| "#{k}=#{v.inspect}" }.join('; ') if attributes
  "<#{uri}>#{attr_string}"
end