Created
April 3, 2012 17:01
-
-
Save albertbellonch/2293672 to your computer and use it in GitHub Desktop.
Link Watch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
class LinkWatch | |
def initialize(url, link, keyword) | |
@url = url | |
@link = link | |
@keyword = keyword | |
end | |
def is_ok? | |
@is_ok ||= begin | |
uri = URI.parse(@url) | |
body = uri.read | |
!!(body =~ /\<a.+href\=\".*#{@link}.*\".*#{@keyword}.*<\/a\>/) | |
end | |
end | |
end | |
# Tests | |
describe LinkWatch do | |
context "existing links" do | |
context "and existing keyword" do | |
it "should go well with Enaldi" do | |
LinkWatch.new("http://enaldi.wordpress.com", "camaloon.com", "Chapas personalizadas").is_ok?.should == true | |
end | |
it "should go wrong with Enaldi" do | |
LinkWatch.new("http://www.milloringlix.org/", "camaloon.com", "xapes personalitzades").is_ok?.should == true | |
end | |
end | |
context "and non-existing keyword" do | |
it "should go wrong with Enaldi" do | |
LinkWatch.new("http://enaldi.wordpress.com", "camaloon.com", "Imanes personalizados").is_ok?.should == false | |
end | |
it "should go wrong with Milloringlix" do | |
LinkWatch.new("http://www.milloringlix.org/", "camaloon.com", "Chapas personalizadas").is_ok?.should == false | |
end | |
end | |
end | |
context "non-existing links" do | |
it "should go wrong with Google" do | |
LinkWatch.new("http://google.es", "camaloon.com", "").is_ok?.should == false | |
end | |
it "should go wrong with Itnig" do | |
LinkWatch.new("http://google.es", "camaloon.com", "").is_ok?.should == false | |
end | |
it "should go wrong with Twitter" do | |
LinkWatch.new("http://google.es", "camaloon.com", "").is_ok?.should == false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment