Last active
August 29, 2015 14:26
-
-
Save norbajunior/af1b10b7d3783e1e18c3 to your computer and use it in GitHub Desktop.
Estrutura de Serviço de Conta de Usuário
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/business/concerns/link_with_facebook.rb | |
module LinkWithFacebook | |
extend ActiveSupport::Concern | |
included do | |
def self.link_with_facebook(params) | |
new(params).link_with_facebook | |
end | |
end | |
private | |
def link_with_facebook | |
if facebook_params_valid? | |
graph = Koala::Facebook::API.new(social_params[:token]) | |
profile = graph.get_object("me") | |
user_params[:facebook] = profile['link'] | |
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 ) | |
else | |
user_params[:facebook] = "" | |
user_params[:fuid] = "" | |
user_params[:fb_token] = "" | |
user_params[:fb_token_expires_at] = "" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/business/concerns/link_with_twitter.rb | |
module LinkWithTwitter | |
extend ActiveSupport::Concern | |
included do | |
def self.link_with_twitter(params) | |
new(params).link_with_twitter | |
end | |
end | |
private | |
def link_with_twitter | |
if twitter_params_valid? | |
client = Twitter::REST::Client.new( | |
consumer_key: Rails.application.config.twitter_key, | |
consumer_secret: Rails.application.config.twitter_secret, | |
access_token: social_params[:token], | |
access_token_secret: social_params[:secret] | |
) | |
user_params[:tuid] = social_params[:uid] | |
user_params[:tw_token] = social_params[:token] | |
user_params[:tw_secret] = social_params[:secret] | |
user_params[:twitter] = client.user.screen_name | |
else | |
user_params[:twitter] = "" | |
user_params[:tuid] = "" | |
user_params[:tw_token] = "" | |
user_params[:tw_secret] = "" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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