Class: Pingback::Client

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

Overview

This class allows to send pingback requests.

Instance Method Summary collapse

Instance Method Details

#ping(source_uri, target_uri) ⇒ String

send an pingback request to the targets associated pingback server.

Parameters:

  • source_uri (String)

    the address of the site containing the link.

  • target_uri (String)

    the target of the link on the source site.

Returns:

  • (String)

    message indicating that the request was successful

Raises:

  • (Pingback::InvalidTargetException)

    raised if the target is not a pingback-enabled resource

  • (XMLRPC::FaultException)

    raised if the server responds with a faultcode



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pingback/client.rb', line 16

def ping(source_uri, target_uri)
  header = request_header target_uri
  pingback_server = header['X-Pingback']

  unless pingback_server
    doc = Nokogiri::HTML(request_all(target_uri).body)
    link = doc.xpath('//link[@rel="pingback"]/attribute::href').first
    pingback_server = URI.escape(link.content) if link
  end

  raise InvalidTargetException unless pingback_server

  send_pingback pingback_server, source_uri, target_uri
end