ruby 这是一个从Facebook,Twitter,Google Plus,Pinterest,LinkedIn和StumbleUpon获得股票的图书馆。如果你需要更精致的话

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 这是一个从Facebook,Twitter,Google Plus,Pinterest,LinkedIn和StumbleUpon获得股票的图书馆。如果你需要更精致的话相关的知识,希望对你有一定的参考价值。

require 'open-uri'

module SocialUtils

  def self.get_facebook_shares(url)
  	f = open("http://graph.facebook.com/?id=#{url}")
		response = f.read()
		shares = JSON.parse(response)['shares']
		return shares.nil? ? 0 : JSON.parse(response)['shares']
	end

	def self.get_twitter_shares(url)
		f = open("https://cdn.api.twitter.com/1/urls/count.json?url=#{url}")
		return JSON.parse(f.read())['count']
	end

	def self.get_pinterest_shares(url)
		f = open("http://api.pinterest.com/v1/urls/count.json?url=#{url}")
		return JSON.parse(f.read().gsub('receiveCount(','').gsub(')',''))['count']
	end

	def self.get_linkedin_shares(url)
		f = open("http://www.linkedin.com/countserv/count/share?url=#{url}&format=json")
		return JSON.parse(f.read())['count']
	end

	def self.get_stumbleupon_shares(url)
		f = open("http://www.stumbleupon.com/services/1.01/badge.getinfo?url=#{url}")
		response = f.read()
		views = JSON.parse(response)['result']['views']
		return views.blank? ? 0 : JSON.parse(response)['result']['views']
	end

	def self.get_googleplus_shares(url)
		f = open("https://plusone.google.com/_/+1/fastbutton?url=#{URI::encode(url)}")
		response = f.read()
		shares = response[/window.__SSR = {c\: \d+.\d+/]
		return shares.nil? ? 0 : shares[/\d+.\d+/]
	end

end

## Your social_utils_spec.rb
## uses vcr gem: https://github.com/vcr/vcr
## expected count is the count at the time of first run
require 'open-uri'
require 'spec_helper'
require "#{Rails.root}/lib/social_utils.rb"

describe SocialUtils do

	let(:url) { 'http://maricrisnonato.com' }

	it "get_facebook_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_facebook_shares(url)
			expect(count).eql?('0')
		end
	end

	it "get_twitter_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_twitter_shares(url)
			expect(count).eql?('1')
		end
	end

	it "get_pinterest_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_pinterest_shares(url)
			expect(count).eql?('0')
		end
	end

	it "get_linkedin_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_linkedin_shares(url)
			expect(count).eql?('0')
		end
	end

	it "get_stumbleupon_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_stumbleupon_shares(url)
			expect(count).eql?('0')
		end
	end

	it "get_googleplus_shares", :vcr do
		VCR.use_cassette('social_utils', :record => :new_episodes) do
			count = SocialUtils.get_googleplus_shares(url)
			expect(count).eql?('0')
		end
	end
	
end

以上是关于ruby 这是一个从Facebook,Twitter,Google Plus,Pinterest,LinkedIn和StumbleUpon获得股票的图书馆。如果你需要更精致的话的主要内容,如果未能解决你的问题,请参考以下文章

使用 ruby​​ gem xmpp 进行 Facebook 聊天

从我的应用程序启动 Facebook 应用程序并使其打开不属于我的个人资料

leetcode 355 Design Twitte

Rails - 设计/Omniauth - 无方法错误配置

使用 Ruby on Rails 时如何创建 Facebook 共享功能?

ruby 手动OAuth2 facebook for Rails API的要点