Sha256: 339d9a3a0b404cf7c4a3b3e40bba1a4e270af2062e8f7fdbdc4b49d799cb90c1

Contents?: true

Size: 936 Bytes

Versions: 14

Compression:

Stored size: 936 Bytes

Contents

# encoding: utf-8

require 'faraday'

module Nimbu
  module Request
    class OAuth2 < Faraday::Middleware
      include Nimbu::Utils::Url

      ACCESS_TOKEN = 'access_token'.freeze
      AUTH_HEADER  = 'Authorization'.freeze

      dependency 'oauth2'

      def call(env)
        # Extract parameters from the query
        params = { ACCESS_TOKEN => @token }.update query_params(env[:url])

        if token = params[ACCESS_TOKEN] and !token.empty?
          env[:url].query = build_query params
          env[:request_headers].merge!(AUTH_HEADER => "Token token=\"#{token}\"")
        end

        @app.call env
      end

      def initialize(app, *args)
        super app
        @app = app
        @token = args.shift
      end

      def query_params(url)
        if url.query.nil? or url.query.empty?
          {}
        else
          parse_query url.query
        end
      end
    end # OAuth2
  end # Request
end # Nimbu

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
nimbu-api-0.3.0 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2.1 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2.beta.3 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2.beta.2 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2.beta.1 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.2.beta lib/nimbu-api/request/oauth2.rb
nimbu-api-0.1.5 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.1.4 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.1.3 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.1.1 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.1 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.0.2 lib/nimbu-api/request/oauth2.rb
nimbu-api-0.0.1 lib/nimbu-api/request/oauth2.rb