Class: Totalshares::Webpage

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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Webpage

Returns a new instance of Webpage.



6
7
8
9
# File 'lib/webpage.rb', line 6

def initialize(url)
  @url = url
  @url ="http://#{url}" unless url.start_with? "https://" or url.start_with? "http://"
end

Instance Method Details

#all(opts = {}) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/webpage.rb', line 82

def all(opts = {})
  response =  {}
  SOCIAL_URLS.each do |key, value|
    response[key] = self.send "#{key}"
  end
  puts "#{@url}\n#{response}" if opts[:v]
  response
end

#facebook(opts = {}) ⇒ Object



18
19
20
21
22
23
# File 'lib/webpage.rb', line 18

def facebook(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["facebook"] + @url)
  count = JSON.parse(output.response_body)["shares"].to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end

#gplus(opts = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/webpage.rb', line 59

def gplus(opts = {})
  body = [
      {
        method: "pos.plusones.get",
        id: "p",
        params: {
        nolog: true,
        id: @url,
        source: "widget",
        userId: "@viewer",
        groupId: "@self"
      },
      jsonrpc: "2.0",
      key: "p",
      apiVersion: "v1"
      }
    ]
  output = Typhoeus.post(SOCIAL_URLS["gplus"], body: JSON.dump(body), headers: { "content-type" => "application/json", "accept" => "application/json"})
  count = JSON.parse(output.response_body)[0]["result"]["metadata"]["globalCounts"]["count"].to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end

#linkedin(opts = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/webpage.rb', line 25

def linkedin(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["linkedin"] + @url)
  count = JSON.parse(output.response_body)["count"].to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end

#pinterest(opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webpage.rb', line 46

def pinterest(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["pinterest"] + @url)
  if output.response_code.eql? 200
    output = output.response_body
    json_output = output[output.index("{"), output.rindex("}")- (output.index("{")-1)]
    count = JSON.parse(json_output)["count"].to_i || 0
    puts "#{@url}\n#{count}" if opts[:v]
    count
  else
    nil
  end
end

#reddit(opts = {}) ⇒ Object



32
33
34
35
36
37
# File 'lib/webpage.rb', line 32

def reddit(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["reddit"] + @url)
  count = JSON.parse(output.response_body)["data"]["children"].size.to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end

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



91
92
93
94
95
96
97
98
# File 'lib/webpage.rb', line 91

def shares_selected(social_networks,opts = {})
  response =  {}
  social_networks.each do |sn|
    response[sn] = self.send "#{sn}" unless SOCIAL_URLS[sn].nil? or SOCIAL_URLS[sn] == ""
  end
  puts "#{@url}\n#{response}" if opts[:v]
  response
end

#stumbledupon(opts = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/webpage.rb', line 39

def stumbledupon(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["stumbledupon"] + @url)
  count = JSON.parse(output.response_body)["result"]["views"].to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end

#twitter(opts = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/webpage.rb', line 11

def twitter(opts = {})
  output = Typhoeus.get(SOCIAL_URLS["twitter"] + @url)
  count = JSON.parse(output.response_body)["count"].to_i || 0
  puts "#{@url}\n#{count}" if opts[:v]
  count
end