faraday_middleware-escher 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -0
- data/VERSION +1 -0
- data/faraday_middleware-escher.gemspec +16 -11
- data/lib/faraday_middleware/escher.rb +7 -7
- data/lib/faraday_middleware/escher/base_middleware.rb +15 -0
- data/lib/faraday_middleware/escher/request_signer.rb +30 -0
- data/lib/faraday_middleware/escher/response_validator.rb +3 -0
- data/spec/escher/base_middleware_spec.rb +47 -0
- data/spec/escher/request_signer_spec.rb +74 -0
- data/spec/spec_helper.rb +48 -0
- metadata +85 -6
- data/lib/faraday_middleware/escher/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93e28632c2c22aaa0ab477dbda353b69696d2fb3
|
4
|
+
data.tar.gz: 4712f9d091691aa0b820358acb2db74a4f5c6733
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e0c93b74c6ff5de92c5bfef6aedb253ec62f138753d9a05cba5965c0e771f204f9f39c8d8e1a2ddf7c2bdf5385b5cceb5837258092ab96719c8dd7a609f42b5
|
7
|
+
data.tar.gz: d3ca6593d986eea89548bd44e7b8d6957308ffe54241e69e82832a8a595f5557995fd7c09bdba5ced369d0a805250bfcd60317f94ac8c1046bbdf4e58f177fb2
|
data/README.md
CHANGED
@@ -18,6 +18,26 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### for sign requests
|
22
|
+
|
23
|
+
The FaradayMiddleware::Escher::RequestSigner will help you sign your requests before sending them
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
|
27
|
+
|
28
|
+
require 'faraday_middleware/escher'
|
29
|
+
conn = Faraday.new do |builder|
|
30
|
+
|
31
|
+
builder.use FaradayMiddleware::Escher::RequestSigner, credential_scope: 'example/credential/scope' do
|
32
|
+
{api_key_id: 'EscherExample', api_secret: 'TheBeginningOfABeautifulFriendship'}
|
33
|
+
end
|
34
|
+
|
35
|
+
builder.adapter :net_http
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
```
|
40
|
+
|
21
41
|
TODO: Write usage instructions here
|
22
42
|
|
23
43
|
## Contributing
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -1,13 +1,10 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'faraday_middleware/escher/version'
|
5
|
-
|
6
2
|
Gem::Specification.new do |spec|
|
7
|
-
|
8
|
-
spec.
|
9
|
-
spec.
|
10
|
-
spec.
|
3
|
+
|
4
|
+
spec.name = 'faraday_middleware-escher'
|
5
|
+
spec.version = File.read(File.join(File.dirname(__FILE__),'VERSION')).split("\n")[0].chomp
|
6
|
+
spec.authors = ['Adam Luzsi']
|
7
|
+
spec.email = ['adamluzsi@gmail.com']
|
11
8
|
spec.summary = %q{escher sign and validation for faraday http rest client}
|
12
9
|
spec.description = %q{WIP!!! escher sign and validation for faraday http rest client}
|
13
10
|
spec.homepage = 'https://github.com/adamluzsi/faraday_middleware-escher'
|
@@ -18,6 +15,14 @@ Gem::Specification.new do |spec|
|
|
18
15
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
16
|
spec.require_paths = ["lib"]
|
20
17
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
|
18
|
+
spec.add_development_dependency('bundler', '>= 1.6')
|
19
|
+
spec.add_development_dependency('rake')
|
20
|
+
spec.add_development_dependency('rspec')
|
21
|
+
spec.add_development_dependency('webmock')
|
22
|
+
|
23
|
+
spec.add_dependency('faraday')
|
24
|
+
spec.add_dependency('faraday_middleware')
|
25
|
+
|
26
|
+
spec.add_dependency('escher')
|
27
|
+
|
28
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
module FaradayMiddleware
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
1
|
+
require 'escher'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
module FaradayMiddleware::Escher
|
4
|
+
require 'faraday_middleware/escher/base_middleware'
|
5
|
+
require 'faraday_middleware/escher/request_signer'
|
6
|
+
require 'faraday_middleware/escher/response_validator'
|
7
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'socket'
|
2
|
+
class FaradayMiddleware::Escher::BaseMiddleware < Faraday::Middleware
|
3
|
+
|
4
|
+
def initialize(app,options={},&escher_keydb_constructor)
|
5
|
+
|
6
|
+
super(app)
|
7
|
+
@host = options[:host] || Socket.gethostname
|
8
|
+
|
9
|
+
@escher_options = options[:options] || {}
|
10
|
+
@escher_credential_scope = options[:credential_scope] || raise(ArgumentError,'missing escher credential scope!')
|
11
|
+
@escher_keydb_constructor = escher_keydb_constructor || raise(ArgumentError,'missing escher constructor')
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class FaradayMiddleware::Escher::RequestSigner < FaradayMiddleware::Escher::BaseMiddleware
|
2
|
+
|
3
|
+
def call(env)
|
4
|
+
|
5
|
+
escher = Escher::Auth.new(@escher_credential_scope, @escher_options)
|
6
|
+
|
7
|
+
uri_path = env[:url].path
|
8
|
+
endpoint = uri_path.empty? ? '/' : uri_path
|
9
|
+
|
10
|
+
request_data = {
|
11
|
+
uri: endpoint,
|
12
|
+
method: env[:method].to_s.upcase,
|
13
|
+
headers: env[:request_headers].map{|k,v| [k,v] }.push(['host',@host])
|
14
|
+
}
|
15
|
+
|
16
|
+
request_data[:body]= env[:body] unless env[:body].nil?
|
17
|
+
|
18
|
+
escher.sign!(
|
19
|
+
request_data,
|
20
|
+
@escher_keydb_constructor.call,
|
21
|
+
request_data[:headers].map{|ary| ary[0] }
|
22
|
+
)
|
23
|
+
|
24
|
+
env[:request_headers].merge!(request_data[:headers])
|
25
|
+
|
26
|
+
return @app.call(env)
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
describe FaradayMiddleware::Escher::BaseMiddleware do
|
3
|
+
|
4
|
+
let(:app){double('app')}
|
5
|
+
|
6
|
+
let(:host){'localhost'}
|
7
|
+
let(:api_key){'superKey'}
|
8
|
+
let(:api_secret){'superSecret'}
|
9
|
+
|
10
|
+
let(:options){
|
11
|
+
|
12
|
+
{
|
13
|
+
api_key: api_key,
|
14
|
+
api_secret: api_secret,
|
15
|
+
host: host
|
16
|
+
}
|
17
|
+
|
18
|
+
}
|
19
|
+
|
20
|
+
subject{ self.described_class.new(app,options) }
|
21
|
+
|
22
|
+
before do
|
23
|
+
allow(Socket).to receive(:gethostname).and_return(host)
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#initialize' do
|
27
|
+
describe 'init take an options hash after app, and catch some of its value' do
|
28
|
+
|
29
|
+
%W[ host api_key api_secret ].each do |element|
|
30
|
+
it "should save #{element} hast key's value under @#{element}" do
|
31
|
+
expect(subject.instance_variable_get("@#{element}")).to eq options[element.to_sym]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should cache @app from the first argument' do
|
36
|
+
expect(subject.instance_variable_get('@app')).to eq app
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should child of the Faraday::Middleware class' do
|
42
|
+
expect(subject).to be_a(Faraday::Middleware)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
describe FaradayMiddleware::Escher::RequestSigner do
|
3
|
+
describe 'this is a faraday middleware witch instance will response to call method' do
|
4
|
+
|
5
|
+
let(:api_key) { 'superKey' }
|
6
|
+
let(:api_secret) { 'superSecret' }
|
7
|
+
let(:host) { 'localhost' }
|
8
|
+
|
9
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
|
10
|
+
|
11
|
+
subject do
|
12
|
+
|
13
|
+
Faraday.new do |builder|
|
14
|
+
|
15
|
+
builder.use FaradayMiddleware::Escher::RequestSigner,
|
16
|
+
api_key: api_key,
|
17
|
+
api_secret: api_secret,
|
18
|
+
host: host
|
19
|
+
|
20
|
+
builder.adapter :test, stubs do |stub|
|
21
|
+
stub.get('/ebi') { |env| [200, {}, 'shrimp'] }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:connection) do
|
29
|
+
Faraday.new do |builder|
|
30
|
+
builder.use FaradayMiddleware::Escher::RequestSigner,
|
31
|
+
api_key: api_key,
|
32
|
+
api_secret: api_secret,
|
33
|
+
host: host
|
34
|
+
|
35
|
+
builder.use FaradayMiddleware::FollowRedirects
|
36
|
+
builder.adapter Faraday.default_adapter
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
let(:target_url) { "http://api-with-escher.example.com" }
|
41
|
+
|
42
|
+
describe '#call' do
|
43
|
+
|
44
|
+
it ''
|
45
|
+
|
46
|
+
it "redirects" do
|
47
|
+
|
48
|
+
# stubs.get('/uni') {[ 200, {}, 'urchin' ]}
|
49
|
+
|
50
|
+
request_mock = stub_request(:get, target_url)
|
51
|
+
|
52
|
+
puts request_mock.methods - Object.methods
|
53
|
+
|
54
|
+
stub_request(:get, "http://facebook.com").to_return(
|
55
|
+
:status => 302,
|
56
|
+
:headers => {"Location" => "http://www.facebook.com/"})
|
57
|
+
|
58
|
+
stub_request(:get, "http://www.facebook.com/").to_return(
|
59
|
+
:status => 302,
|
60
|
+
:headers => {"Location" => "https://www.facebook.com/"})
|
61
|
+
|
62
|
+
stub_request(:get, "https://www.facebook.com/")
|
63
|
+
|
64
|
+
response = connection.get "http://facebook.com"
|
65
|
+
|
66
|
+
expect(response.env[:url].to_s).to eq("https://www.facebook.com/")
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
gem_root_dir = File.join(File.dirname(__FILE__),'..')
|
2
|
+
$LOAD_PATH.unshift(File.join(gem_root_dir,'lib'))
|
3
|
+
|
4
|
+
|
5
|
+
require 'rspec'
|
6
|
+
require 'webmock/rspec'
|
7
|
+
require 'faraday_middleware/escher'
|
8
|
+
|
9
|
+
module EnvCompatibility
|
10
|
+
def faraday_env(env)
|
11
|
+
if defined?(Faraday::Env)
|
12
|
+
Faraday::Env.from(env)
|
13
|
+
else
|
14
|
+
env
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ResponseMiddlewareExampleGroup
|
20
|
+
def self.included(base)
|
21
|
+
base.let(:options) { Hash.new }
|
22
|
+
base.let(:headers) { Hash.new }
|
23
|
+
base.let(:middleware) {
|
24
|
+
described_class.new(lambda {|env|
|
25
|
+
Faraday::Response.new(env)
|
26
|
+
}, options)
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def process(body, content_type = nil, options = {})
|
31
|
+
env = {
|
32
|
+
:body => body, :request => options,
|
33
|
+
:request_headers => Faraday::Utils::Headers.new,
|
34
|
+
:response_headers => Faraday::Utils::Headers.new(headers)
|
35
|
+
}
|
36
|
+
env[:response_headers]['content-type'] = content_type if content_type
|
37
|
+
yield(env) if block_given?
|
38
|
+
middleware.call(faraday_env(env))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
RSpec.configure do |config|
|
43
|
+
config.include EnvCompatibility
|
44
|
+
config.include ResponseMiddlewareExampleGroup, :type => :response
|
45
|
+
config.expect_with :rspec do |c|
|
46
|
+
c.syntax = :expect
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware-escher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -38,6 +38,76 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: faraday_middleware
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: escher
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
41
111
|
description: WIP!!! escher sign and validation for faraday http rest client
|
42
112
|
email:
|
43
113
|
- adamluzsi@gmail.com
|
@@ -51,9 +121,15 @@ files:
|
|
51
121
|
- LICENSE.txt
|
52
122
|
- README.md
|
53
123
|
- Rakefile
|
124
|
+
- VERSION
|
54
125
|
- faraday_middleware-escher.gemspec
|
55
126
|
- lib/faraday_middleware/escher.rb
|
56
|
-
- lib/faraday_middleware/escher/
|
127
|
+
- lib/faraday_middleware/escher/base_middleware.rb
|
128
|
+
- lib/faraday_middleware/escher/request_signer.rb
|
129
|
+
- lib/faraday_middleware/escher/response_validator.rb
|
130
|
+
- spec/escher/base_middleware_spec.rb
|
131
|
+
- spec/escher/request_signer_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
57
133
|
homepage: https://github.com/adamluzsi/faraday_middleware-escher
|
58
134
|
licenses:
|
59
135
|
- MIT
|
@@ -78,4 +154,7 @@ rubygems_version: 2.2.2
|
|
78
154
|
signing_key:
|
79
155
|
specification_version: 4
|
80
156
|
summary: escher sign and validation for faraday http rest client
|
81
|
-
test_files:
|
157
|
+
test_files:
|
158
|
+
- spec/escher/base_middleware_spec.rb
|
159
|
+
- spec/escher/request_signer_spec.rb
|
160
|
+
- spec/spec_helper.rb
|