class SocialNetworksLib
Public Instance Methods
side_signin(sign_as, user_id, email, access_token, token_expired)
click to toggle source
# File lib/social_networks.rb, line 3 def side_signin sign_as, user_id, email, access_token, token_expired # решил прикрепить скрепу! if @current_user access_card_exist = OauthAccessCard.find_by({oauth_name: sign_as, oauth_uid: user_id}) access_card = @current_user.oauth_access_cards.find_by({oauth_name: sign_as, oauth_uid: user_id}) if access_card.nil? if access_card_exist # спизданул u = access_card_exist.user_card access_card_exist.update(user_id: @current_user.id) access_card = access_card_exist # если забрал последнюю скрепу и нет больше карточек доступа - дезинтегрируем юзера if u.access_cards_count == 0 u.destroy else u.set_active_card end else # прикрепил access_card = @current_user.oauth_access_cards.create({oauth_name: sign_as, oauth_uid: user_id, access_token: access_token, token_expired: token_expired}) end else # просто заапдейтил токен access_card.update(access_token: access_token, token_expired: token_expired) end social_network_api :update_user_profile, sign_as, access_card # дополнение профиля пользователя данными из соц сети set_cookies @current_user, access_card else access_card = OauthAccessCard.find_by({oauth_name: sign_as, oauth_uid: user_id}) if access_card user = access_card.user_card access_card.update(access_token: access_token, token_expired: token_expired) social_network_api :update_user_profile, sign_as, access_card # дополнение профиля пользователя данными из соц сети user.authenticate(user.password) set_cookies user, access_card else # создаём клиента user = UserCard.new get_params ActionController::Parameters.new({user: {password: SecureRandom.hex(8), oauth_access_cards_attributes: [oauth_name: sign_as, oauth_uid: user_id, access_token: access_token, token_expired: token_expired]}}) if user.save # печеньки для кабеля access_card = user.oauth_access_cards.first set_cookies user, access_card social_network_api :update_user_profile, sign_as, access_card # дополнение профиля пользователя данными из соц сети else msg = '' user.errors.full_messages.each do |message| msg += message end @result = {type: :bad, msg: msg} end end end redirect_to profile_path end
Private Instance Methods
get_age(bday)
click to toggle source
# File lib/social_networks.rb, line 324 def get_age bday TimeDifference.between(bday, DateTime.now).in_years.floor end
get_declined_permissions(social_network, access_token)
click to toggle source
# File lib/social_networks.rb, line 287 def get_declined_permissions social_network, access_token case social_network when :fb uri = URI "#{CONFIG[social_network][:api][:uri]}#{CONFIG[social_network][:api][:check_permissions][:prefix]}" params = {access_token: access_token} uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri permissions = ActiveSupport::JSON.decode(result.body)['data'].map { |permission_obj| permission_obj['permission'] if permission_obj['status'] == 'declined' }.compact permissions.empty?? nil : permissions end end
get_missed_permissions(social_network, scopes)
click to toggle source
# File lib/social_networks.rb, line 283 def get_missed_permissions social_network, scopes CONFIG[social_network][:auth][:step1][:scope].split(',') - scopes end
get_reg_info(social_network, access_params)
click to toggle source
# File lib/social_networks.rb, line 270 def get_reg_info social_network, access_params case social_network when :fb uri = URI CONFIG[social_network][:api][:uri] params = CONFIG[social_network][:api][:get_reg_info][:params] params.merge!(access_params) unless access_params.nil? uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body) end end
get_sex(sex, network)
click to toggle source
# File lib/social_networks.rb, line 301 def get_sex sex, network case network when :vk case sex.to_i when 1 :female when 2 :male else nil end when :mailru case sex.to_i when 0 :male when 1 :female else nil end end end
get_user_info(social_network, access_params)
click to toggle source
# File lib/social_networks.rb, line 210 def get_user_info social_network, access_params case social_network when :vk uri = URI "#{CONFIG[social_network][:api][:uri]}#{CONFIG[social_network][:api][:users_get][:prefix]}" params = CONFIG[social_network][:api][:united_params].merge!(CONFIG[social_network][:api][:users_get][:params]) params.merge!(access_params) unless access_params.nil? uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body)['response'][0] when :fb uri = URI CONFIG[social_network][:api][:uri] params = CONFIG[social_network][:api][:get_user_info][:params] params.merge!(access_params) unless access_params.nil? uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body) when :google uri = URI CONFIG[social_network][:api][:get_user_info][:uri] params = access_params uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body) when :yandex uri = URI CONFIG[social_network][:api][:get_user_info][:uri] params = CONFIG[social_network][:api][:get_user_info][:params] params.merge!(access_params) unless access_params.nil? uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body) when :mailru uri = URI CONFIG[social_network][:api][:get_user_info][:uri] params = CONFIG[social_network][:api][:get_user_info][:params] params.merge!(access_params) unless access_params.nil? sig = Digest::MD5.hexdigest params.sort.map{|k,v| "#{k}=#{v}"}.join() + CONFIG[social_network][:auth][:step2][:client_secret] # охуенная подпись ! params = params.merge({sig: sig}) uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body)[0] end end
get_user_picture(social_network, width, height, access_params)
click to toggle source
# File lib/social_networks.rb, line 257 def get_user_picture social_network, width, height, access_params case social_network when :fb uri = URI "#{CONFIG[social_network][:api][:uri]}#{CONFIG[social_network][:api][:get_picture][:prefix]}" params = {width: width, height: height, redirect: false} params.merge!(access_params) unless access_params.nil? uri.query = URI.encode_www_form params result = Net::HTTP.get_response uri ActiveSupport::JSON.decode(result.body) end end