Skip to content

Instantly share code, notes, and snippets.

@majioa
Created September 26, 2012 07:38
Show Gist options
  • Save majioa/3786610 to your computer and use it in GitHub Desktop.
Save majioa/3786610 to your computer and use it in GitHub Desktop.
Sample function in a post model to share to the post Facebook and Twitter
gem 'koala'
gem 'bitly', '~> 0.8.0'
gem 'twitter'
# Post model of Rails
class Post
field :text, type: String, default: ''
# NOTE: field image is an image object of Dragonfly
belongs_to :user
def share_to user, targets={}
logger.debug "[share_to] <<< user: #{user.inspect}, targets = #{targets.inspect}"
return nil if !user || targets.delete_if do |key, value| !value end.empty?
shared = false
message = self.text
tiny_url = self.tiny_url
if targets[:facebook] && (facebook = user.facebook_client)
logger.debug "[share_to]> Sharing to Facebook via #{facebook.inspect}"
facebook.put_picture(File.open(self.image.path), "image/jpeg",
{ :message => message + " | " + tiny_url }) if self.image
shared ||= true
end
if targets[:twitter] && (twitter = user.twitter_client)
logger.debug "[share_to]> Sharing to Twitter via #{twitter.inspect}"
twit = ''
message.each_char do |c|
break if twit.bytesize + c.bytesize > 139 - tiny_url.size
twit << c
end
twitter.update_with_media(twit + ' ' + tiny_url,
File.open(self.image.path))
shared ||= true
end
shared
rescue Exception
logger.info "[share_to]> #{$!.class}: #{$!}\n\t#{[email protected]("\n\t")}"
false
end
protected
def tiny_url
Bitly.use_api_version_3 # required for short link
bitly = Bitly.new(Settings.bitly_key, Settings.bitly_secret)
short_url = bitly.shorten(File.join(Settings.app_url,
'post',self.id.to_s)).short_url
logger.debug "[tiny_url]>>> #{short_url.inspect}"
short_url
end
end
# Posts Controller of Rails
class PostsController < ApplicationController
# NOTE: before filtering the share we calls find_post method
# NOTE: current_user is method of Devise (or may be predefined)
def share
shared = @post.share_to(current_user,
:facebook => params[:share_facebook] == 'true',
:twitter => params[:share_twitter] == 'true' )
head( shared && :created || :bad_requests ) if action_name == 'share'
end
end
# User model of Rails
class User
field :facebook_token, type: String
field :twitter_token, type: String
field :twitter_secret, type: String
# NOTE: UserException::InvalidSocial is an invalid social exception for the User model
def authenticated_in_social? social
case social.to_s.to_sym
when :twitter
!self.twitter_token.blank? && !self.twitter_secret.blank?
when :facebook
!self.facebook_token.blank?
else
raise UserException::InvalidSocial
end
end
def facebook_client
return nil unless self.authenticated_in_social? :facebook
Koala::Facebook::API.new(self.token)
rescue
nil
end
def twitter_client
return nil unless self.authenticated_in_social? :twitter
Twitter::Client.new(
:oauth_token => self.twitter_token,
:oauth_token_secret => self.twitter_secret
)
rescue
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment