Sha256: 38183426c4951a915776c1b91b6551c7eda3db8cc82840622058b5b792b68d55

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

class OauthYx < Oauth
  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?

    case step
      when :step1
        uri.query = URI.encode_www_form params
        uri.to_s
      when :step2
        oauth_access_card_params Net::HTTP.post_form uri, params
    end

  end

  def oauth_access_card_params result
    begin
      params = ActiveSupport::JSON.decode(result.body)

      unless params['error'].present?
        add_info = yandex_passport_api({oauth_token: params['access_token']})
        {
            state: :ok,
            oauth_name: @name,
            oauth_uid: add_info['id'],
            access_token: params['access_token'],
            token_expired: (DateTime.now + params['expires_in'].second).to_s(:db),
            photourl: add_info['is_avatar_empty'] == true ? nil : "https://avatars.yandex.net/get-yapic/#{add_info['default_avatar_id']}/islands-retina-50"
        }
      else
        error_response "#{params['error']}:#{params['error_description']}"
      end
    rescue Exception => error
      error_response "rescue #{error.message} #{params} #{result.body} #{add_info}"
    end
  end

  def yandex_passport_api access_params
    uri = URI CONFIG[:oauth][@name][:api][:passport][:uri]
    params = CONFIG[:oauth][@name][:api][:passport][: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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
digital_heroes_startkit-0.1.1.4 lib/oauth/oauth_yx.rb
digital_heroes_startkit-0.1.1.3 lib/oauth/oauth_yx.rb
digital_heroes_startkit-0.1.1.2 lib/oauth/oauth_yx.rb
digital_heroes_startkit-0.1.1.1 lib/oauth/oauth_yx.rb
digital_heroes_startkit-0.1.1 lib/oauth/oauth_yx.rb