Sha256: 88f0d7a37a37ef6637a884bea7b777e3559301cda6e88f27c37801763de9b829

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

require 'simple_oauth'

module EventMachine
  module Middleware

    class OAuth
      include HttpEncoding

      def initialize(opts = {})
        @opts = opts.dup
        # Allow both `oauth` gem and `simple_oauth` gem opts formats
        @opts[:token] ||= @opts.delete(:access_token)
        @opts[:token_secret] ||= @opts.delete(:access_token_secret)
      end

      def request(client, head, body)
        request = client.req
        uri     = request.uri.join(encode_query(request.uri, request.query))
        params  = {}

        # from https://github.com/oauth/oauth-ruby/blob/master/lib/oauth/request_proxy/em_http_request.rb
        if ["POST", "PUT"].include?(request.method)
          head["content-type"] ||= "application/x-www-form-urlencoded" if body.is_a? Hash
          form_encoded = head["content-type"].to_s.downcase.start_with?("application/x-www-form-urlencoded")

          if form_encoded
            CGI.parse(client.normalize_body(body)).each do |k,v|
              # Since `CGI.parse` always returns values as an array
              params[k] = v.size == 1 ? v.first : v
            end
          end
        end

        head["Authorization"] = SimpleOAuth::Header.new(request.method, uri, params, @opts)

        [head,body]
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
em-http-request-samesite-0.1.1 lib/em-http/middleware/oauth.rb
em-http-request-samesite-1.1.7 lib/em-http/middleware/oauth.rb
em-http-request-1.1.7 lib/em-http/middleware/oauth.rb
em-http-request-1.1.6 lib/em-http/middleware/oauth.rb
em-http-request-1.1.5 lib/em-http/middleware/oauth.rb
em-http-request-1.1.4 lib/em-http/middleware/oauth.rb
em-http-request-1.1.3 lib/em-http/middleware/oauth.rb
em-http-request-1.1.2 lib/em-http/middleware/oauth.rb
em-http-request-1.1.1 lib/em-http/middleware/oauth.rb
em-http-request-1.1.0 lib/em-http/middleware/oauth.rb
em-http-request-1.0.3 lib/em-http/middleware/oauth.rb
em-http-request-1.0.2 lib/em-http/middleware/oauth.rb
em-http-request-1.0.1 lib/em-http/middleware/oauth.rb
em-http-request-1.0.0 lib/em-http/middleware/oauth.rb