Skip to content

Commit a27962a

Browse files
committed
➖ em-http-request
- lazy Loaded so it can be optional, since some systems fail to install it, and it doesn't seem maintained
1 parent 5dd8a76 commit a27962a

File tree

9 files changed

+192
-161
lines changed

9 files changed

+192
-161
lines changed

.idea/oauth.iml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.rubocop_gradual.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
"lib/oauth/tokens/consumer_token.rb:3696415131": [
3535
[9, 5, 155, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 349576019]
3636
],
37-
"oauth.gemspec:108374725": [
37+
"oauth.gemspec:3033391650": [
3838
[133, 3, 56, "Gemspec/DependencyVersion: Dependency version specification is required.", 644892567],
39-
[154, 3, 40, "Gemspec/DependencyVersion: Dependency version specification is required.", 2300588954],
40-
[156, 3, 44, "Gemspec/DependencyVersion: Dependency version specification is required.", 1905290578],
41-
[157, 3, 46, "Gemspec/DependencyVersion: Dependency version specification is required.", 4289565910]
39+
[153, 3, 40, "Gemspec/DependencyVersion: Dependency version specification is required.", 2300588954],
40+
[155, 3, 44, "Gemspec/DependencyVersion: Dependency version specification is required.", 1905290578],
41+
[156, 3, 46, "Gemspec/DependencyVersion: Dependency version specification is required.", 4289565910]
4242
],
4343
"spec/oauth/backwards_compatibility_spec.rb:4041711732": [
4444
[3, 16, 25, "RSpec/DescribeClass: The first argument to describe should be the class or module being tested.", 3956042931]

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Please file a bug if you notice a violation of semantic versioning.
2323
- converted minitest => rspec
2424
### Deprecated
2525
### Removed
26+
- dependency on em-http-request
27+
- now lazy Loaded so it can be optional, since some systems fail to install it, and it doesn't seem maintained
2628
### Fixed
2729
### Security
2830

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ GEM
162162
pp (0.6.2)
163163
prettyprint
164164
prettyprint (0.2.0)
165-
prism (1.4.0)
165+
prism (1.5.0)
166166
psych (5.2.6)
167167
date
168168
stringio
@@ -301,7 +301,7 @@ GEM
301301
snaky_hash (2.0.3)
302302
hashie (>= 0.1.0, < 6)
303303
version_gem (>= 1.1.8, < 3)
304-
standard (1.51.0)
304+
standard (1.51.1)
305305
language_server-protocol (~> 3.17.0.2)
306306
lint_roller (~> 1.0)
307307
rubocop (~> 1.80.2)

gemfiles/modular/optional.gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# Optional dependencies are not depended on directly, but may be used if present.
2+
3+
# em-http-request depends on http_parser.rb, which is supposed to be,
4+
# but appears to not be, compatible with JRuby.
5+
# http_parser.rb is highly unmaintained, with no responses on issues for 2 years.
6+
# em-http-request is even less maintained, with no releases in 5 years.
7+
gem "em-http-request", "~> 1.1.7" # ruby >= 0

lib/oauth/client/em_http.rb

Lines changed: 104 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,126 @@
11
# frozen_string_literal: true
22

3-
require "em-http"
43
require "oauth/helper"
5-
require "oauth/request_proxy/em_http_request"
4+
require "oauth/optional"
65

7-
# Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient
8-
# instance. This is purely syntactic sugar.
9-
module EventMachine
10-
class HttpClient
11-
attr_reader :oauth_helper
6+
if OAuth::Optional.em_http_available?
7+
require "oauth/request_proxy/em_http_request"
128

13-
# Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting
14-
# this may add a header, additional query string parameters, or additional POST body parameters.
15-
# The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
16-
# header.
17-
#
18-
# * http - Configured Net::HTTP instance, ignored in this scenario except for getting host.
19-
# * consumer - OAuth::Consumer instance
20-
# * token - OAuth::Token instance
21-
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
22-
# +signature_method+, +nonce+, +timestamp+)
23-
#
24-
# This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version.
25-
#
26-
# See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1]
27-
def oauth!(http, consumer = nil, token = nil, options = {})
28-
options = {
29-
request_uri: normalized_oauth_uri(http),
30-
consumer: consumer,
31-
token: token,
32-
scheme: "header",
33-
signature_method: nil,
34-
nonce: nil,
35-
timestamp: nil,
36-
}.merge(options)
9+
# Extensions for em-http so that we can use consumer.sign! with an EventMachine::HttpClient
10+
# instance. This is purely syntactic sugar.
11+
module EventMachine
12+
class HttpClient
13+
attr_reader :oauth_helper
3714

38-
@oauth_helper = OAuth::Client::Helper.new(self, options)
39-
__send__(:"set_oauth_#{options[:scheme]}")
40-
end
15+
# Add the OAuth information to an HTTP request. Depending on the <tt>options[:scheme]</tt> setting
16+
# this may add a header, additional query string parameters, or additional POST body parameters.
17+
# The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
18+
# header.
19+
#
20+
# * http - Configured Net::HTTP instance, ignored in this scenario except for getting host.
21+
# * consumer - OAuth::Consumer instance
22+
# * token - OAuth::Token instance
23+
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
24+
# +signature_method+, +nonce+, +timestamp+)
25+
#
26+
# This method also modifies the <tt>User-Agent</tt> header to add the OAuth gem version.
27+
#
28+
# See Also: {OAuth core spec version 1.0, section 5.4.1}[http://oauth.net/core/1.0#rfc.section.5.4.1]
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)
4139

42-
# Create a string suitable for signing for an HTTP request. This process involves parameter
43-
# normalization as specified in the OAuth specification. The exact normalization also depends
44-
# on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request
45-
# itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
46-
# header.
47-
#
48-
# * http - Configured Net::HTTP instance
49-
# * consumer - OAuth::Consumer instance
50-
# * token - OAuth::Token instance
51-
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
52-
# +signature_method+, +nonce+, +timestamp+)
53-
#
54-
# See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
55-
def signature_base_string(http, consumer = nil, token = nil, options = {})
56-
options = {
57-
request_uri: normalized_oauth_uri(http),
58-
consumer: consumer,
59-
token: token,
60-
scheme: "header",
61-
signature_method: nil,
62-
nonce: nil,
63-
timestamp: nil,
64-
}.merge(options)
40+
@oauth_helper = OAuth::Client::Helper.new(self, options)
41+
__send__(:"set_oauth_#{options[:scheme]}")
42+
end
6543

66-
OAuth::Client::Helper.new(self, options).signature_base_string
67-
end
44+
# Create a string suitable for signing for an HTTP request. This process involves parameter
45+
# normalization as specified in the OAuth specification. The exact normalization also depends
46+
# on the <tt>options[:scheme]</tt> being used so this must match what will be used for the request
47+
# itself. The default scheme is +header+, in which the OAuth parameters as put into the +Authorization+
48+
# header.
49+
#
50+
# * http - Configured Net::HTTP instance
51+
# * consumer - OAuth::Consumer instance
52+
# * token - OAuth::Token instance
53+
# * options - Request-specific options (e.g. +request_uri+, +consumer+, +token+, +scheme+,
54+
# +signature_method+, +nonce+, +timestamp+)
55+
#
56+
# See Also: {OAuth core spec version 1.0, section 9.1.1}[http://oauth.net/core/1.0#rfc.section.9.1.1]
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)
6867

69-
# This code was lifted from the em-http-request because it was removed from
70-
# the gem June 19, 2010
71-
# see: https://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b
72-
def normalize_uri
73-
@normalized_uri ||= begin
74-
uri = @conn.dup
75-
encoded_query = encode_query(@conn, @req[:query])
76-
path, query = encoded_query.split("?", 2)
77-
uri.query = query unless encoded_query.empty?
78-
uri.path = path
79-
uri
68+
OAuth::Client::Helper.new(self, options).signature_base_string
8069
end
81-
end
8270

83-
protected
71+
# This code was lifted from the em-http-request because it was removed from
72+
# the gem June 19, 2010
73+
# see: https://github.com/igrigorik/em-http-request/commit/d536fc17d56dbe55c487eab01e2ff9382a62598b
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
84+
85+
protected
8486

85-
def combine_query(path, query, uri_query)
86-
combined_query = if query.is_a?(Hash)
87-
query.map { |k, v| encode_param(k, v) }.join("&")
88-
else
89-
query.to_s
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}"
9095
end
91-
combined_query = [combined_query, uri_query].reject(&:empty?).join("&") unless uri_query.to_s.empty?
92-
combined_query.to_s.empty? ? path : "#{path}?#{combined_query}"
93-
end
9496

95-
# Since we expect to get the host etc details from the http instance (...),
96-
# we create a fake url here. Surely this is a horrible, horrible idea?
97-
def normalized_oauth_uri(http)
98-
uri = URI.parse(normalize_uri.path)
99-
uri.host = http.address
100-
uri.port = http.port
97+
# Since we expect to get the host etc details from the http instance (...),
98+
# we create a fake url here. Surely this is a horrible, horrible idea?
99+
def normalized_oauth_uri(http)
100+
uri = URI.parse(normalize_uri.path)
101+
uri.host = http.address
102+
uri.port = http.port
101103

102-
uri.scheme = if http.respond_to?(:use_ssl?) && http.use_ssl?
103-
"https"
104-
else
105-
"http"
104+
uri.scheme = if http.respond_to?(:use_ssl?) && http.use_ssl?
105+
"https"
106+
else
107+
"http"
108+
end
109+
uri.to_s
106110
end
107-
uri.to_s
108-
end
109111

110-
def set_oauth_header
111-
req[:head] ||= {}
112-
req[:head].merge!("Authorization" => @oauth_helper.header)
113-
end
112+
def set_oauth_header
113+
req[:head] ||= {}
114+
req[:head].merge!("Authorization" => @oauth_helper.header)
115+
end
114116

115-
def set_oauth_body
116-
raise NotImplementedError, "please use the set_oauth_header method instead"
117-
end
117+
def set_oauth_body
118+
raise NotImplementedError, "please use the set_oauth_header method instead"
119+
end
118120

119-
def set_oauth_query_string
120-
raise NotImplementedError, "please use the set_oauth_header method instead"
121+
def set_oauth_query_string
122+
raise NotImplementedError, "please use the set_oauth_header method instead"
123+
end
121124
end
122125
end
123126
end

lib/oauth/optional.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module OAuth
4+
# Helpers for optional, lazily loaded integrations.
5+
module Optional
6+
class << self
7+
# Try to load EventMachine HTTP client support provided by em-http-request.
8+
#
9+
# Returns true if available, false if the dependency is not installed.
10+
# Never raises LoadError.
11+
def em_http_available?
12+
# em-http-request provides "em-http" entrypoint
13+
require "em-http"
14+
true
15+
rescue LoadError
16+
false
17+
end
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)