Sha256: 26f7ae47f155fbbd22bbad5475912f49fa177e6d62c63b0d7a356ff566e15c48

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

require 'faraday'

# @private
module FaradayMiddleware
  # @private
  class OAuth2 < Faraday::Middleware
    def call(env)
      if env[:method] == :get or env[:method] == :delete
        if env[:url].query.nil?
          query = {}
        else
          query = Faraday::Utils.parse_query(env[:url].query)
        end
        if @access_token and not query["client_secret"]
          # env[:url].query = Faraday::Utils.build_query(query.merge(:access_token => @access_token))
          env[:request_headers] = env[:request_headers].merge('Authorization' => "Bearer #{@access_token}")
        elsif @client_id
          env[:url].query = Faraday::Utils.build_query(query.merge(:appkey => @client_id))
        end
      else
        if @access_token and not env[:body] && env[:body][:client_secret]
          env[:body] = {} if env[:body].nil?
          # env[:body] = env[:body].merge(:access_token => @access_token)
          env[:request_headers] = env[:request_headers].merge('Authorization' => "Bearer #{@access_token}")
        elsif @client_id
          env[:body] = env[:body].merge(:client_id => @client_id)
        end
      end

      @app.call env
    end

    def initialize(app, client_id, access_token=nil)
      @app = app
      @client_id = client_id
      @access_token = access_token
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
douban_api-0.1.2 lib/faraday/oauth2.rb
douban_api-0.1.1 lib/faraday/oauth2.rb
douban_api-0.1.0 lib/faraday/oauth2.rb