saddle 0.0.34 → 0.0.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +7 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +12 -8
- data/README.md +12 -0
- data/lib/saddle/middleware/authentication/oauth1.rb +54 -0
- data/lib/saddle/middleware/authentication/oauth2.rb +33 -0
- data/lib/saddle/version.rb +1 -1
- data/spec/middleware/authentication/oauth2_authentication_spec.rb +38 -0
- metadata +7 -2
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -6,10 +6,12 @@ gem 'faraday', '~> 0.8.7'
|
|
6
6
|
gem 'faraday_middleware', '~> 0.9.0'
|
7
7
|
|
8
8
|
group :test do
|
9
|
-
gem '
|
9
|
+
gem 'rake'
|
10
|
+
gem 'rspec', '~> 2.14.1'
|
10
11
|
gem 'rspec-instafail', '~> 0.2'
|
11
12
|
|
12
13
|
gem 'airbrake'
|
14
|
+
gem 'simple_oauth', '~> 0.2.0' # Optional dependency
|
13
15
|
gem 'statsd-ruby', :require => ['statsd']
|
14
16
|
end
|
15
17
|
|
data/Gemfile.lock
CHANGED
@@ -24,15 +24,17 @@ GEM
|
|
24
24
|
coderay (~> 1.0.5)
|
25
25
|
method_source (~> 0.8)
|
26
26
|
slop (~> 3.4)
|
27
|
-
|
28
|
-
|
29
|
-
rspec-
|
30
|
-
rspec-
|
31
|
-
|
32
|
-
rspec-
|
27
|
+
rake (10.1.0)
|
28
|
+
rspec (2.14.1)
|
29
|
+
rspec-core (~> 2.14.0)
|
30
|
+
rspec-expectations (~> 2.14.0)
|
31
|
+
rspec-mocks (~> 2.14.0)
|
32
|
+
rspec-core (2.14.2)
|
33
|
+
rspec-expectations (2.14.0)
|
33
34
|
diff-lcs (>= 1.1.3, < 2.0)
|
34
35
|
rspec-instafail (0.2.4)
|
35
|
-
rspec-mocks (2.
|
36
|
+
rspec-mocks (2.14.1)
|
37
|
+
simple_oauth (0.2.0)
|
36
38
|
slop (3.4.5)
|
37
39
|
statsd-ruby (1.2.0)
|
38
40
|
|
@@ -45,6 +47,8 @@ DEPENDENCIES
|
|
45
47
|
faraday (~> 0.8.7)
|
46
48
|
faraday_middleware (~> 0.9.0)
|
47
49
|
pry
|
48
|
-
|
50
|
+
rake
|
51
|
+
rspec (~> 2.14.1)
|
49
52
|
rspec-instafail (~> 0.2)
|
53
|
+
simple_oauth (~> 0.2.0)
|
50
54
|
statsd-ruby
|
data/README.md
CHANGED
@@ -60,3 +60,15 @@ Saddle enables you to create beautifully stable and functionaly API clients, in
|
|
60
60
|
|
61
61
|
## todo
|
62
62
|
* xml posting/parsing
|
63
|
+
|
64
|
+
|
65
|
+
## Code Status
|
66
|
+
|
67
|
+
* [](https://travis-ci.org/mLewisLogic/saddle)
|
68
|
+
* [](https://codeclimate.com/github/mLewisLogic/saddle)
|
69
|
+
* [](https://gemnasium.com/mLewisLogic/saddle)
|
70
|
+
|
71
|
+
|
72
|
+
## License
|
73
|
+
|
74
|
+
Saddle is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'simple_oauth'
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
module Saddle
|
7
|
+
module Middleware
|
8
|
+
module Authentication
|
9
|
+
|
10
|
+
## Add OAuth 1.0 authentication tokens to requests
|
11
|
+
#
|
12
|
+
class OAuth1 < Faraday::Middleware
|
13
|
+
|
14
|
+
TYPE_URLENCODED = 'application/x-www-form-urlencoded'.freeze
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
if env[:request][:client_options][:oauth1] &&
|
18
|
+
env[:request][:client_options][:oauth1][:consumer_key] &&
|
19
|
+
env[:request][:client_options][:oauth1][:consumer_secret] &&
|
20
|
+
env[:request][:client_options][:oauth1][:token] &&
|
21
|
+
env[:request][:client_options][:oauth1][:token_secret]
|
22
|
+
|
23
|
+
env[:request_headers]['Authorization'] ||= SimpleOAuth::Header.new(
|
24
|
+
env[:method],
|
25
|
+
env[:url].to_s,
|
26
|
+
filtered_body_params(env),
|
27
|
+
env[:request][:client_options][:oauth1]
|
28
|
+
).to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
@app.call(env)
|
32
|
+
end
|
33
|
+
|
34
|
+
def body_params(env)
|
35
|
+
# Only process body params if it's url-encoded or missing it's Content-Type
|
36
|
+
# see RFC 5489, section 3.4.1.3.1 for details
|
37
|
+
if !(type = env[:request_headers]['Content-Type']) or type == TYPE_URLENCODED
|
38
|
+
if env[:body].respond_to?(:to_str)
|
39
|
+
Faraday::Utils::parse_nested_query(env[:body])
|
40
|
+
else
|
41
|
+
env[:body]
|
42
|
+
end
|
43
|
+
end || {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def filtered_body_params(env)
|
47
|
+
body_params(env).reject {|k,v| v.respond_to?(:content_type) }
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
module Saddle
|
6
|
+
module Middleware
|
7
|
+
module Authentication
|
8
|
+
|
9
|
+
## Add OAuth 2.0 authentication tokens to requests
|
10
|
+
#
|
11
|
+
class OAuth2 < Faraday::Middleware
|
12
|
+
|
13
|
+
def initialize(app, key_name='access_token')
|
14
|
+
super(app)
|
15
|
+
@key_name = key_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
if env[:request][:client_options][@key_name.to_sym]
|
20
|
+
new_query = []
|
21
|
+
new_query << env[:url].query if env[:url].query
|
22
|
+
new_query << "#{@key_name}=#{CGI.escape(env[:request][:client_options][@key_name.to_sym].to_s)}"
|
23
|
+
env[:url].query = new_query.join('&')
|
24
|
+
end
|
25
|
+
|
26
|
+
@app.call(env)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/saddle/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'saddle/middleware/authentication/oauth2'
|
4
|
+
|
5
|
+
|
6
|
+
describe Saddle::Middleware::Authentication::OAuth2 do
|
7
|
+
|
8
|
+
context "Test authentication middleware" do
|
9
|
+
|
10
|
+
it 'should attach the oauth2 access token' do
|
11
|
+
TEST_ACCESS_TOKEN = 'testes'
|
12
|
+
|
13
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
14
|
+
stub.get("/test?oauth2_access_token=#{TEST_ACCESS_TOKEN}") {
|
15
|
+
[
|
16
|
+
200,
|
17
|
+
{},
|
18
|
+
'Party on!',
|
19
|
+
]
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
class OAuth2Client < Saddle::Client
|
24
|
+
add_middleware({
|
25
|
+
:klass => Saddle::Middleware::Authentication::OAuth2,
|
26
|
+
:args => ['oauth2_access_token'],
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
client = OAuth2Client.create(
|
31
|
+
:oauth2_access_token => TEST_ACCESS_TOKEN,
|
32
|
+
:stubs => stubs
|
33
|
+
)
|
34
|
+
client.requester.get('/test').should == 'Party on!'
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saddle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.35
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -67,6 +67,7 @@ extra_rdoc_files: []
|
|
67
67
|
files:
|
68
68
|
- .gitignore
|
69
69
|
- .rspec
|
70
|
+
- .travis.yml
|
70
71
|
- Gemfile
|
71
72
|
- Gemfile.lock
|
72
73
|
- LICENSE
|
@@ -76,6 +77,8 @@ files:
|
|
76
77
|
- lib/saddle/endpoint.rb
|
77
78
|
- lib/saddle/errors.rb
|
78
79
|
- lib/saddle/method_tree_builder.rb
|
80
|
+
- lib/saddle/middleware/authentication/oauth1.rb
|
81
|
+
- lib/saddle/middleware/authentication/oauth2.rb
|
79
82
|
- lib/saddle/middleware/logging/airbrake.rb
|
80
83
|
- lib/saddle/middleware/logging/rails.rb
|
81
84
|
- lib/saddle/middleware/logging/statsd.rb
|
@@ -91,6 +94,7 @@ files:
|
|
91
94
|
- lib/saddle/requester.rb
|
92
95
|
- lib/saddle/version.rb
|
93
96
|
- saddle.gemspec
|
97
|
+
- spec/middleware/authentication/oauth2_authentication_spec.rb
|
94
98
|
- spec/middleware/instrumentation_spec.rb
|
95
99
|
- spec/middleware/logging/airbrake_spec.rb
|
96
100
|
- spec/middleware/logging/rails_spec.rb
|
@@ -128,6 +132,7 @@ specification_version: 3
|
|
128
132
|
summary: A full-featured, generic consumer layer for you to build API client implementations
|
129
133
|
with.
|
130
134
|
test_files:
|
135
|
+
- spec/middleware/authentication/oauth2_authentication_spec.rb
|
131
136
|
- spec/middleware/instrumentation_spec.rb
|
132
137
|
- spec/middleware/logging/airbrake_spec.rb
|
133
138
|
- spec/middleware/logging/rails_spec.rb
|