Class: Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-bot.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Bot

Returns a new instance of Bot.



11
12
13
14
15
16
# File 'lib/simple-bot.rb', line 11

def initialize *args
  args.each do |a|
    @url = a.to_s if a =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
  end
  create_bot
end

Instance Method Details

#create_botObject



18
19
20
21
# File 'lib/simple-bot.rb', line 18

def create_bot
  @agent = Mechanize.new
  @agent.get(@url)
end


23
24
25
26
27
28
29
# File 'lib/simple-bot.rb', line 23

def grab_all_links
  @all_news_links = []
  @agent.page.parser.css('a').each do |link|
    @all_news_links << {text: link.text, url: link['href']} if (link['href'] =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix)
  end
  return @all_news_links
end