Sha256: 5eb147adac25ef0536eb1570757f4ad56994a24b3a25f3f3e598be2d7f3e4fe3

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

class OauthIm < 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 = user_self({access_token: params['access_token']})
        {
            state: :ok,
            oauth_name: @name,
            oauth_uid: params['user']['id'],
            access_token: params['access_token'],
            token_expired: DateTime.now + 30.days,
            photourl: params['user']['profile_picture']
        }
      else
        error_response "#{params['error']} #{params['error_reason']} #{params['error_description']}"
      end
    rescue Exception => error
      error_response "rescue #{error.message} #{params} #{result.body}"
    end
  end

  def user_self 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_im.rb
digital_heroes_startkit-0.1.1.3 lib/oauth/oauth_im.rb
digital_heroes_startkit-0.1.1.2 lib/oauth/oauth_im.rb
digital_heroes_startkit-0.1.1.1 lib/oauth/oauth_im.rb
digital_heroes_startkit-0.1.1 lib/oauth/oauth_im.rb