Class: Atom::Service

Inherits:
Element show all
Defined in:
lib/atom/service.rb

Overview

Atom::Service represents an Atom Publishing Protocol service document. Its only child is #workspaces, which is an Array of Atom::Workspace s

Instance Attribute Summary

Attributes inherited from Element

#base, #extensions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#append_elem, attributes, #build, builders, def_get, def_set, do_parsing, #get, #get_atom_attrb, #get_atom_elem, #get_atom_elems, #get_elem, #get_elems, is_atom_element, is_element, on_build, on_init, parse, run_initters, #set, #set_atom_attrb, #to_s, #to_xml

Methods included from Parsers

#on_parse, #on_parse_attr, #on_parse_many, #on_parse_root, #parse_plain

Methods included from Converters

#atom_attrb, #atom_element, #atom_elements, #atom_link, #atom_string, #atom_time, #attrb, #build_plain, #element, #elements, #strings, #time

Constructor Details

#initialize(service_url = "", http = Atom::HTTP.new) ⇒ Service

retrieves and parses an Atom service document.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/atom/service.rb', line 27

def initialize(service_url = "", http = Atom::HTTP.new)
  super()

  @http = http

  return if service_url.empty?

  base = URI.parse(service_url)

  rxml = nil

  res = @http.get(base, "Accept" => "application/atomsvc+xml")
  res.validate_content_type(["application/atomsvc+xml"])

  unless res.code == "200"
    raise Atom::HTTPException, "Unexpected HTTP response code: #{res.code}"
  end

  self.class.parse(res.body, base, self)
end

Class Method Details

.discover(url, http = Atom::HTTP.new) ⇒ Object

given a URL, attempt to find a service document



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/atom/service.rb', line 53

def self.discover url, http = Atom::HTTP.new
  res = http.get(url, 'Accept' => 'application/atomsvc+xml, text/html')

  case res.content_type
  when /application\/atomsvc\+xml/
    Service.parse res.body, url
  when /html/
    begin
      require 'hpricot'
    rescue
      raise 'autodiscovering from HTML requires Hpricot.'
    end

    h = Hpricot(res.body)

    links = h.search('//link')

    service_links = links.select { |l| (' ' + l['rel'] + ' ').match(/ service /i) }

    unless service_links.empty?
      url = url.to_uri + service_links.first['href']
      return Service.new(url.to_s, http)
    end

    rsd_links = links.select { |l| (' ' + l['rel'] + ' ').match(/ EditURI /i) }

    unless rsd_links.empty?
      url = url.to_uri + rsd_links.first['href']
      return Service.from_rsd(url, http)
    end

    raise AutodiscoveryFailure, "couldn't find any autodiscovery links in the HTML"
  else
    raise AutodiscoveryFailure, "can't autodiscover from a document of type #{res.content_type}"
  end
end

.from_rsd(url, http = Atom::HTTP.new) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/atom/service.rb', line 90

def self.from_rsd url, http = Atom::HTTP.new
  rsd = http.get(url)

  doc = REXML::Document.new(rsd.body)

  atom = REXML::XPath.first(doc, '/rsd/service/apis/api[@name="Atom"]')

  unless atom
    raise AutodiscoveryFailure, "couldn't find an Atom link in the RSD"
  end

  url = url.to_uri + atom.attributes['apiLink']

  Service.new(url.to_s, http)
end

Instance Method Details

#collectionsObject



48
49
50
# File 'lib/atom/service.rb', line 48

def collections
  self.workspaces.map { |ws| ws.collections }.flatten
end