class Totalshares::Webpage

Public Class Methods

new(url) click to toggle source
# 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

Public Instance Methods

all(opts = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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