Class: Discodactyl::XRD::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/discodactyl/xrd/document.rb

Direct Known Subclasses

HostMeta

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rawObject

Returns the value of attribute raw


20
21
22
# File 'lib/discodactyl/xrd/document.rb', line 20

def raw
  @raw
end

Class Method Details

.parse(string) ⇒ Object


9
10
11
12
13
14
15
16
17
# File 'lib/discodactyl/xrd/document.rb', line 9

def parse(string)
  raw = Nokogiri::XML(string)
  Discodactyl.log.debug("parsing xrd: #{raw}") if Discodactyl.log
  doc = self.new

  doc.raw = raw

  doc
end

Instance Method Details

#append(link) ⇒ Object

take an XML fragment for a link and append it to the document


42
43
44
45
46
47
48
# File 'lib/discodactyl/xrd/document.rb', line 42

def append(link)
  initial_ids = ids
  raw.root.add_child(link)
  elem = Link.parse(raw.root.last_element_child)
  elem.id = generate_tag_uri if elem.id.nil? || initial_ids.include?(elem.id)
  elem
end

#escapeXPath(str) ⇒ Object


22
23
24
25
# File 'lib/discodactyl/xrd/document.rb', line 22

def escapeXPath(str)
  inner = str.split('\'').join('\',"\'",\'')
  outer = 'concat(\'\',\'%s\')' % inner
end

60
61
62
# File 'lib/discodactyl/xrd/document.rb', line 60

def find_all_links_by_rel(rel)
    links.find_all {|link| link.rel == rel }
end

56
57
58
# File 'lib/discodactyl/xrd/document.rb', line 56

def find_link_by_id(link_id)
    links.find {|link| link.id == link_id}
end

#generate_tag_uriObject


76
77
78
79
80
81
82
# File 'lib/discodactyl/xrd/document.rb', line 76

def generate_tag_uri
  scheme = 'tag'
  authority = 'dactylo.us'
  date = Date.today.to_s
  specific = "/xrd/link/#{rand(2**10)}"
  "#{scheme}:#{authority},#{date}:#{specific}"
end

#idsObject


64
65
66
# File 'lib/discodactyl/xrd/document.rb', line 64

def ids
  links.map(&:id).reject(&:nil?)
end

#linkelems_by_rel(rel) ⇒ Object


27
28
29
30
# File 'lib/discodactyl/xrd/document.rb', line 27

def linkelems_by_rel(rel)
  path = '/xrd:XRD/xrd:Link[@rel=%s]'% escapeXPath(rel)
  @raw.xpath path, XMLNS
end

50
51
52
53
54
# File 'lib/discodactyl/xrd/document.rb', line 50

def links
  raw.xpath('/xrd:XRD/xrd:Link', XMLNS).collect {|elem|
    Link.parse(elem)
  }
end

32
33
34
35
# File 'lib/discodactyl/xrd/document.rb', line 32

def links_by_rel(rel)
  # linkelems_by_rel(rel).map {|e| Link.parse(e) }
  find_all_links_by_rel(rel)
end

#relsObject


72
73
74
# File 'lib/discodactyl/xrd/document.rb', line 72

def rels
  links.map(&:rel).reject(&:nil?).uniq
end

#subjectObject


84
85
86
# File 'lib/discodactyl/xrd/document.rb', line 84

def subject
  raw.xpath('/xrd:XRD/xrd:Subject', XMLNS).text
end

#to_sObject


68
69
70
# File 'lib/discodactyl/xrd/document.rb', line 68

def to_s
  raw.to_s
end

#uris_by_rel(rel, params = {}) ⇒ Object


37
38
39
# File 'lib/discodactyl/xrd/document.rb', line 37

def uris_by_rel(rel, params = {})
  links_by_rel(rel).map {|l| l.to_uri(params) }
end