Skip to content

Instantly share code, notes, and snippets.

@roneesh
Created October 31, 2013 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roneesh/7254605 to your computer and use it in GitHub Desktop.
Save roneesh/7254605 to your computer and use it in GitHub Desktop.
This is my first line of thinking. I have had all the individual social class sub-class from SocialData, and each one only has four methods, initialize, symbolize, shares, and response, which is protected in each. This idea came about because I wanted to escaped_url to be a single method definition across all the sub-classes (DRY). Currently the…
require 'typhoeus'
require 'uri'
class SocialData
attr_accessor :url
def initialize(url)
self.url = url
end
def fetch
@@sources.each_with_object({}) do |source_name, share_data|
source = source_name.constantize.new(url)
share_data[source.symbolize] = source.shares
end
end
@@sources = [
'FacebookData',
'TwitterData',
'GoogleData'
]
class RequestFailure < StandardError; end
protected
def escaped_url
CGI.escape @url
end
end
class FacebookData < SocialData
attr_accessor :url
def initialize(url)
self.url = url
end
def symbolize
:facebook
end
def shares #just parses response
response.first['total_count']
end
protected
def response
Fql.execute(query)
rescue Fql::Exception
raise SocialData::RequestFailure
end
def query
%{SELECT total_count FROM link_stat WHERE url="#{escaped_url}"}
end
end
class TwitterData < SocialData
attr_accessor :url
def initialize(url)
self.url = url
end
def symbolize
:twitter
end
def shares
MultiJson.load(response)['count']
end
protected
def response
base_url = "http://urls.api.twitter.com/1/urls/count.json?url="
Typhoeus::Request.get(base_url + escaped_url).body
rescue
raise SocialData::RequestFailure
end
end
class GoogleData < SocialData
attr_accessor :url
def initialize(url)
@url = url
end
def symbolize
:google
end
def shares
10 #because actual pulling from link is not working
end
protected
def response
request_url = "https://plusone.google.com/_/+1/fastbutton?url=#{escaped_url}&count=true"
Typhoeus.get(request_url)
rescue
raise SocialData::RequestFailure
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment