Class: Snackhack2::WebsiteLinks

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(save_file: true) ⇒ WebsiteLinks

Returns a new instance of WebsiteLinks.



9
10
11
12
# File 'lib/snackhack2/website_links.rb', line 9

def initialize(save_file: true)
  @site = site
  @save_file = save_file
end

Instance Attribute Details

#save_fileObject

Returns the value of attribute save_file.



7
8
9
# File 'lib/snackhack2/website_links.rb', line 7

def save_file
  @save_file
end

#siteObject

Returns the value of attribute site.



7
8
9
# File 'lib/snackhack2/website_links.rb', line 7

def site
  @site
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/snackhack2/website_links.rb', line 14

def run
  doc = Nokogiri::HTML(URI.open(@site))
  links = doc.xpath('//a')
  all_links = links.map { |e| e['href'] }.compact
  content = all_links.uniq.join("\n")
  if @save_file
    Snackhack2::file_save(@site, "links", content)
  else
    all_links.each do |links|
      puts links
    end
  end
end