class Oauth

Public Class Methods

new(verify_url, state) click to toggle source
# File lib/oauth/oauth.rb, line 6
def initialize verify_url, state
  @verify_url = verify_url
  @key = state.keys.first
  @name = state.values.first[:name].to_sym
end

Public Instance Methods

auth_request(step = :step1, add_params = nil) click to toggle source
# File lib/oauth/oauth.rb, line 12
def auth_request step = :step1, add_params = nil
  uri = URI CONFIG[:oauth][@name][:auth][step][:uri]
  params = CONFIG[:oauth][@name][:auth][:united_params].merge(CONFIG[:oauth][@name][:auth][step].except(:uri)).merge({redirect_uri: @verify_url, state: @key})
  # для второго шага
  params.merge!(add_params) unless add_params.nil?
  uri.query = URI.encode_www_form params

  case step
    when :step1
      uri.to_s
    when :step2
      oauth_access_card_params Net::HTTP.get_response(uri)
  end

end
error_response(error_message) click to toggle source
# File lib/oauth/oauth.rb, line 65
def error_response error_message
  {
      state: :shit,
      error: error_message
  }
end
get_user_info(access_params) click to toggle source
# File lib/oauth/oauth.rb, line 55
def get_user_info access_params
  uri = URI "#{CONFIG[:oauth][@name][:api][:uri]}#{CONFIG[:oauth][@name][:api][:users_get][:prefix]}"
  params = CONFIG[:oauth][@name][:api][:united_params].merge!(CONFIG[:oauth][@name][: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]
end
oauth_access_card_params(result) click to toggle source
# File lib/oauth/oauth.rb, line 28
def oauth_access_card_params result
  begin
    params = ActiveSupport::JSON.decode(result.body)
    unless params['error'].present?
      add_info = get_user_info({access_token: params['access_token']})
      {
        state: :ok,
        oauth_name: @name,
        oauth_uid: params['user_id'],
        access_token: params['access_token'],
        token_expired: (DateTime.now + params['expires_in'].second).to_s(:db),
        photourl: add_info['photo_100']
      }
    else
      {
          state: :shit,
          error: "#{params['error']}:#{params['error_description']}"
      }
    end
  rescue Exception => error
    {
        state: :shit,
        error: error.message
    }
  end
end