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.



40
41
42
43
# File 'lib/tent-client/link_header.rb', line 40

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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



21
22
23
# File 'lib/tent-client/link_header.rb', line 21

def attributes
  @attributes
end

#uriObject

Returns the value of attribute uri.



21
22
23
# File 'lib/tent-client/link_header.rb', line 21

def uri
  @uri
end

Class Method Details

.parse(link_text) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tent-client/link_header.rb', line 23

def self.parse(link_text)
  s = StringScanner.new(link_text)
  s.scan(/[^<]+/)
  link = s.scan(/<[^\s]+>/)
  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



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

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

#[](k) ⇒ Object



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

def [](k)
  attributes[k]
end

#[]=(k, v) ⇒ Object



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

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

#to_sObject



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

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