Class: Pingback::Sender

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_url, urls = []) ⇒ Sender

source_url is the page that contains outgoing links. urls are the links on that page. urls can also be added dynamically by using +obj.urls << ‘url’.



38
39
40
41
# File 'lib/webby-pingback.rb', line 38

def initialize(source_url, urls = [])
  @source_url = source_url
  @urls       = ([] << urls).flatten
end

Instance Attribute Details

#urlsObject

Returns the value of attribute urls.



42
43
44
# File 'lib/webby-pingback.rb', line 42

def urls
  @urls
end

Instance Method Details

#find_pingback_url(url) ⇒ Object

From a given URL try to find a pingback source from HTTP Header, if not found try to download the page and look for a <link rel=“pingback”> element and use that.



65
66
67
68
69
70
71
72
73
74
# File 'lib/webby-pingback.rb', line 65

def find_pingback_url(url)
  res = fetch_pingback_url(url)
  if res['X-Pingback'].nil?
    res = fetch_pingback_url(url, :Get)
    doc = Hpricot(res.body)
    (doc/"//link[@rel='pingback']")[0]['href'] rescue false
  else
    res['X-Pingback']
  end
end

#start(verbose = false) ⇒ Object

Tries to find a pingback url from a given URL. If found ping it.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/webby-pingback.rb', line 45

def start(verbose = false)
  @urls.each do |url|
  begin
    print "#{url} => "
    pingback_url = find_pingback_url(url)
    if pingback_url
      send_ping(pingback_url, url)
    else
      puts 'Nothing to ping'
    end
  rescue Exception => e
    STDERR.puts e.message
    next
  end
  end
end