Class: YADIS

Inherits:
Object
  • Object
show all
Defined in:
lib/yadis/yadis.rb

Overview

High level class for performing the Yadis protocol on a given URL. The YADIS.discover class method is a good place to get started in determining which services a URL supports.

Constant Summary collapse

@@ca_path =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ YADIS

Discover services for a URI using the Yadis protocol. uri should be a valid URI represented as a string. This method may raise YADISParseError in the case of an invalid or unparsable XRDS file, or YADISHTTPError is the URI cannot be fetched.

Raises:



51
52
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
# File 'lib/yadis/yadis.rb', line 51

def initialize(uri)
  http = NetHTTPFetcher.new
  http.ca_path = @@ca_path if @@ca_path    
  headers = {'Accept' => 'application/xrds+xml'}

  response = http.get(uri, headers)
  raise YADISHTTPError, "Could not fetch #{uri}" if response.nil?

  uri, resp_payload = response
  xrds_uri = uri

  header = resp_payload['x-xrds-location']
  header = resp_payload['x-yadis-location'] if header.nil?

  if header
    xrds_uri = header
    response = http.get(xrds_uri)
    raise YADISHTTPError, "Could not fetch XRDS #{xrds_uri}" if response.nil?
    resp_payload = response[1]
  end

  unless resp_payload['content-type'] == 'application/xrds+xml'
    loc = html_yadis_location(resp_payload.body)
    unless loc.nil?
      xrds_uri, resp_payload = http.get(loc)
    end
  end

  xrds = XRDS.parse(resp_payload.body)
  raise YADISParseError, "Bad XRDS" if xrds.nil?

  @uri = uri
  @xrds_uri = xrds_uri
  @xrds = xrds
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



15
16
17
# File 'lib/yadis/yadis.rb', line 15

def uri
  @uri
end

#xrdsObject

Returns the value of attribute xrds.



15
16
17
# File 'lib/yadis/yadis.rb', line 15

def xrds
  @xrds
end

#xrds_uriObject

Returns the value of attribute xrds_uri.



15
16
17
# File 'lib/yadis/yadis.rb', line 15

def xrds_uri
  @xrds_uri
end

Class Method Details

.ca_path=(ca_path) ⇒ Object

Set the path to a certificate authority pem file, for verifying server certificates of HTTPS pages. If you are interested in verifying certs like the mozilla web browser, have a look at the files here:

curl.haxx.se/docs/caextract.html



38
39
40
41
42
43
44
45
# File 'lib/yadis/yadis.rb', line 38

def YADIS.ca_path=(ca_path)
  ca_path = ca_path.to_s
  if File.exists?(ca_path)
    @@ca_path = ca_path
  else
    raise ArgumentError, "#{ca_path} is not a valid file path"
  end
end

.discover(uri) ⇒ Object

Discover services for a given URI. Please note that no normalization will be done to the passed in URI, it should be a textually valid URI string before calling discover.

Returns nil if no XRDS was found, or a YADIS object on success. This method is essentially the same as YADIS.new, but does not raise any exceptions.



24
25
26
27
28
29
30
31
# File 'lib/yadis/yadis.rb', line 24

def YADIS.discover(uri)
  return nil unless uri
  begin
    return YADIS.new(uri)
  rescue
    return nil
  end
end

Instance Method Details

#filter_services(filter) ⇒ Object

Returns a list of services, ordered by priority, that match the filter. filter is a Proc object that produces ServiceEnpoint objects, subclasses of ServiceEnpoint or nil. This method is useful for extracting several types of services while maintaining priority, for example you may write a filter Proc to extract OpenID and LID ServiceEnpoint objects.



99
100
101
102
103
104
105
106
# File 'lib/yadis/yadis.rb', line 99

def filter_services(filter)
  # product a list of filtered ServiceEndpoint objects.  filtered
  # will contain a list of nil or ServiceEnpoint (subclasses) objects.
  filtered = self.services.collect {|s| filter.call(s)}
  
  # return all object in filtered that are not nil
  return filtered.find_all {|s| s}
end

#servicesObject

Returns an Array Service objects sorted by priority.



88
89
90
91
# File 'lib/yadis/yadis.rb', line 88

def services
  @xrds.services.each {|s| s.yadis = self}
  @xrds.services
end