Class: Totalshares::Website

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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Website

Returns a new instance of Website.



5
6
7
8
9
10
11
12
13
# File 'lib/website.rb', line 5

def initialize(url)
  @url = url
  @url ="http://#{url}" unless url.start_with? "https://" or url.start_with? "http://"
  SOCIAL_URLS.each do |key, value|
    instance_variable_set("@#{key}_count", 0)
    self.class.send(:define_method, "#{key}") {|opts={}| self.shares(key, opts)}
    self.class.send(:define_method, "all") {|opts={}| self.shares("all", opts)}
  end
end

Instance Method Details

#shares_selected(social_networks, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/website.rb', line 15

def shares_selected(social_networks, opts = {})
  social_networks.each do |sn|
    instance_variable_set("@#{sn}_count", 0)
  end
  output = {}
  opts[:skip_query_strings] = true
  Anemone.crawl(@url, opts) do | anemone |
    anemone.on_every_page do |page|
      webpage = Webpage.new(page.url.to_s)
      count = webpage.send("shares_selected",social_networks)
      print_flag=false
      count.each do |key,value|
        print_flag=true if value > 0
        sum_of_count = instance_variable_get("@#{key}_count") + value
        instance_variable_set("@#{key}_count", sum_of_count)
      end
      puts "#{page.url.to_s}\n#{count}" if print_flag and opts[:v]
      output[page.url.to_s]=count if print_flag
    end
  end
  total=0
  social_networks.each do |sn|
    variable_name = "@#{sn}_count"
    puts "#{sn}: #{instance_variable_get(variable_name)}"
    total += instance_variable_get(variable_name)
  end
  puts "Total Shares: #{total}" if opts[:v]
  return output, total
end