File: curb_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 (55 lines) | stat: -rw-r--r-- 1,395 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
  require 'oauth/request_proxy/base'
require 'curb'
require 'uri'
require 'cgi'

module OAuth::RequestProxy::Curl
  class Easy < OAuth::RequestProxy::Base
    # Proxy for signing Curl::Easy requests
    # Usage example:
    # oauth_params = {:consumer => oauth_consumer, :token => access_token}
    # req = Curl::Easy.new(uri)
    # oauth_helper = OAuth::Client::Helper.new(req, oauth_params.merge(:request_uri => uri))
    # req.headers.merge!({"Authorization" => oauth_helper.header})
    # req.http_get
    # response = req.body_str
    proxies ::Curl::Easy

    def method
      nil
    end

    def uri
      options[:uri].to_s
    end

    def parameters
      if options[:clobber_request]
        options[:parameters]
      else
        post_parameters.merge(query_parameters).merge(options[:parameters] || {})
      end
    end

    private

    def query_parameters
      query = URI.parse(request.url).query
      return(query ? CGI.parse(query) : {})
    end

    def post_parameters
      post_body = {}

      # Post params are only used if posting form data
      if (request.headers['Content-Type'] && request.headers['Content-Type'].to_s.downcase.start_with?("application/x-www-form-urlencoded"))

        request.post_body.split("&").each do |str|
          param = str.split("=")
          post_body[param[0]] = param[1]
        end
      end
      post_body
    end
  end
end