Skip to content

Instantly share code, notes, and snippets.

@norbajunior
Last active August 29, 2015 14:26
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 norbajunior/af1b10b7d3783e1e18c3 to your computer and use it in GitHub Desktop.
Save norbajunior/af1b10b7d3783e1e18c3 to your computer and use it in GitHub Desktop.
Estrutura de Serviço de Conta de Usuário
class UserAccount
include LinkWithFacebook
include LinkWithTwitter
include SignUpWithFacebook
include SignUpWithTwitter
def initialize(params)
@user_params = params[:user]
@social_params = params[:social]
end
private
attr_reader :social_params, :user_params
def save
end
def facebook_params_valid?
social_params[:uid].present? && social_params[:token].present?
end
def twitter_params_valid?
social_params[:uid].present? && social_params[:token].present? && social_params[:secret].present?
end
end
# app/business/concerns/sign_up_with_facebook.rb
module SignUpWithFacebook
extend ActiveSupport::Concern
included do
def self.sign_up_with_facebook(params)
new(params).sign_up_with_facebook
end
end
private
def sign_up_with_facebook
graph = Koala::Facebook::API.new(social_params[:token])
profile = graph.get_object("me")
image_profile = graph.get_picture(social_params[:uid])
user_params[:email] = profile["email"]
user_params[:name] = profile["first_name"]
user_params[:facebook] = profile["link"]
user_params[:remote_avatar_url] = image_profile
user_params[:timezone] = ActiveSupport::TimeZone.new(profile["timezone"].to_i).name
user_params[:fuid] = social_params[:uid]
user_params[:fb_token] = social_params[:token]
user_params[:fb_token_expires_at] = Time.at( (social_params[:expires_at] || (Time.now + 1.day)).to_i )
user_params[:password] = Devise.friendly_token[0,20]
end
end
# app/business/concerns/sign_up_with_twitter.rb
module SignUpWithTwitter
extend ActiveSupport::Concern
included do
def self.sign_up_with_twitter(params)
new(params).sign_up_with_twitter
end
end
private
def sign_up_with_twitter
# client = Twitter::REST::Client.new(
# consumer_key: Rails.application.config.twitter_key,
# consumer_secret: Rails.application.config.twitter_secret,
# access_token: social[:token],
# access_token_secret: social[:secret]
# )
client = Twitter::REST::Client.new(
consumer_key: "H0Ja9fulUgTd2V46cLJo1rGPl",
consumer_secret: "bIW41brFYu7HFFGM9MBqAuaG9p2ZPZeo2zzsWkElDkwdg7xQd2",
access_token: "227845421-zPK2bYTs5WW0gSs29sr30JrR2CL4NxbYsbJH1xYm",
access_token_secret: "q5IQyPhgY6sP2NNW32J7N10WYxrl4Mjv4sK4gd57MyQAq"
)
puts "teste #{client.user.methods.sort}"
user_params[:name] = client.user.name
user_params[:remote_avatar_url] = client.user.profile_image_url
user_params[:twitter] = client.user.screen_name
user_params[:timezone] = client.user.time_zone
user_params[:tuid] = social_params[:uid]
user_params[:tw_token] = social_params[:token]
user_params[:tw_secret] = social_params[:secret]
user_params[:password] = Devise.friendly_token[0,20]
# user_params[:email] = profile["email"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment