class EventMachine::HttpClient

Attributes

oauth_helper[R]

Public Instance Methods

normalize_uri() click to toggle source

This code was lifted from the em-http-request because it was removed from the gem June 19, 2010 see: github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b

   # File lib/oauth/client/em_http.rb
74 def normalize_uri
75   @normalized_uri ||= begin
76     uri = @conn.dup
77     encoded_query = encode_query(@conn, @req[:query])
78     path, query = encoded_query.split("?", 2)
79     uri.query = query unless encoded_query.empty?
80     uri.path = path
81     uri
82   end
83 end
oauth!(http, consumer = nil, token = nil, options = {}) click to toggle source

Add the OAuth information to an HTTP request. Depending on the options[:scheme] setting this may add a header, additional query string parameters, or additional POST body parameters. The default scheme is header, in which the OAuth parameters as put into the Authorization header.

  • http - Configured Net::HTTP instance, ignored in this scenario except for getting host.

  • consumer - OAuth::Consumer instance

  • token - OAuth::Token instance

  • options - Request-specific options (e.g. request_uri, consumer, token, scheme, signature_method, nonce, timestamp)

This method also modifies the User-Agent header to add the OAuth gem version.

See Also: OAuth core spec version 1.0, section 5.4.1

   # File lib/oauth/client/em_http.rb
29 def oauth!(http, consumer = nil, token = nil, options = {})
30   options = {
31     request_uri: normalized_oauth_uri(http),
32     consumer: consumer,
33     token: token,
34     scheme: "header",
35     signature_method: nil,
36     nonce: nil,
37     timestamp: nil,
38   }.merge(options)
39 
40   @oauth_helper = OAuth::Client::Helper.new(self, options)
41   __send__(:"set_oauth_#{options[:scheme]}")
42 end
signature_base_string(http, consumer = nil, token = nil, options = {}) click to toggle source

Create a string suitable for signing for an HTTP request. This process involves parameter normalization as specified in the OAuth specification. The exact normalization also depends on the options[:scheme] being used so this must match what will be used for the request itself. The default scheme is header, in which the OAuth parameters as put into the Authorization header.

  • http - Configured Net::HTTP instance

  • consumer - OAuth::Consumer instance

  • token - OAuth::Token instance

  • options - Request-specific options (e.g. request_uri, consumer, token, scheme, signature_method, nonce, timestamp)

See Also: OAuth core spec version 1.0, section 9.1.1

   # File lib/oauth/client/em_http.rb
57 def signature_base_string(http, consumer = nil, token = nil, options = {})
58   options = {
59     request_uri: normalized_oauth_uri(http),
60     consumer: consumer,
61     token: token,
62     scheme: "header",
63     signature_method: nil,
64     nonce: nil,
65     timestamp: nil,
66   }.merge(options)
67 
68   OAuth::Client::Helper.new(self, options).signature_base_string
69 end

Protected Instance Methods

combine_query(path, query, uri_query) click to toggle source
   # File lib/oauth/client/em_http.rb
87 def combine_query(path, query, uri_query)
88   combined_query = if query.is_a?(Hash)
89     query.map { |k, v| encode_param(k, v) }.join("&")
90   else
91     query.to_s
92   end
93   combined_query = [combined_query, uri_query].reject(&:empty?).join("&") unless uri_query.to_s.empty?
94   combined_query.to_s.empty? ? path : "#{path}?#{combined_query}"
95 end
normalized_oauth_uri(http) click to toggle source

Since we expect to get the host etc details from the http instance (…), we create a fake url here. Surely this is a horrible, horrible idea?

    # File lib/oauth/client/em_http.rb
 99 def normalized_oauth_uri(http)
100   uri = URI.parse(normalize_uri.path)
101   uri.host = http.address
102   uri.port = http.port
103 
104   uri.scheme = if http.respond_to?(:use_ssl?) && http.use_ssl?
105     "https"
106   else
107     "http"
108   end
109   uri.to_s
110 end
set_oauth_body() click to toggle source
    # File lib/oauth/client/em_http.rb
117 def set_oauth_body
118   raise NotImplementedError, "please use the set_oauth_header method instead"
119 end
set_oauth_header() click to toggle source
    # File lib/oauth/client/em_http.rb
112 def set_oauth_header
113   req[:head] ||= {}
114   req[:head].merge!("Authorization" => @oauth_helper.header)
115 end
set_oauth_query_string() click to toggle source
    # File lib/oauth/client/em_http.rb
121 def set_oauth_query_string
122   raise NotImplementedError, "please use the set_oauth_header method instead"
123 end