File: em_http_request.rb

package info (click to toggle)
ruby-oauth 0.5.4-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 584 kB
  • sloc: ruby: 4,070; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 1,604 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'oauth/request_proxy/base'
# em-http also uses adddressable so there is no need to require uri.
require 'em-http'
require 'cgi'

module OAuth::RequestProxy::EventMachine
  class HttpRequest < OAuth::RequestProxy::Base

    # A Proxy for use when you need to sign EventMachine::HttpClient instances.
    # It needs to be called once the client is construct but before data is sent.
    # Also see oauth/client/em-http
    proxies ::EventMachine::HttpClient

    # Request in this con

    def method
      request.method
    end

    def uri
      request.normalize_uri.to_s
    end

    def parameters
      if options[:clobber_request]
        options[:parameters]
      else
        all_parameters
      end
    end

    protected

    def all_parameters
      merged_parameters({}, post_parameters, query_parameters, options[:parameters])
    end

    def query_parameters
      CGI.parse(request.normalize_uri.query.to_s)
    end

    def post_parameters
      headers = request.options[:head] || {}
      form_encoded = headers['Content-Type'].to_s.downcase.start_with?("application/x-www-form-urlencoded")
      if ['POST', 'PUT'].include?(method) && form_encoded
        CGI.parse(request.normalize_body.to_s)
      else
        {}
      end
    end

    def merged_parameters(params, *extra_params)
      extra_params.compact.each do |params_pairs|
        params_pairs.each_pair do |key, value|
          if params.has_key?(key)
            params[key] += value
          else
            params[key] = [value].flatten
          end
        end
      end
      params
    end

  end
end