gorilla-io 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +14 -0
- data/gorilla-io.gemspec +29 -0
- data/lib/gorilla/client.rb +12 -0
- data/lib/gorilla/error.rb +23 -0
- data/lib/gorilla/middleware/api_version.rb +24 -0
- data/lib/gorilla/middleware/http_exceptions.rb +36 -0
- data/lib/gorilla/middleware/signature_auth.rb +40 -0
- data/lib/gorilla/middleware/user_agent.rb +15 -0
- data/lib/gorilla/vanilla_client.rb +38 -0
- data/lib/gorilla/version.rb +3 -0
- data/lib/gorilla-io.rb +1 -0
- data/lib/gorilla.rb +48 -0
- data/spec/lib/gorilla/middleware/api_version_spec.rb +12 -0
- data/spec/lib/gorilla/middleware/http_exceptions_spec.rb +29 -0
- data/spec/lib/gorilla/middleware/signature_auth_spec.rb +54 -0
- data/spec/lib/gorilla/middleware/user_agent_spec.rb +11 -0
- data/spec/lib/gorilla/vanilla_client_spec.rb +75 -0
- data/spec/spec_helper.rb +39 -0
- data/spec/support/faraday_helpers.rb +49 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 631312539a8eadcd08066f2a0be7f2443e61ddac
|
4
|
+
data.tar.gz: a01f8253981639d6463576169752f5bf5f887a2b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b97db6e2d1260c3e60ec0cda94f027c2bd9aaa83fb19529fb90fcf5c6ba92d7f4011f6180deb202eb6ede9e4fe96776152689ddf5a22a42bb6b2735a80069de
|
7
|
+
data.tar.gz: 1f2c9a37fcafcce87b1c61f6447b415c69c97dfac672fd1c32b0374b8127184270423f1e34e9b1839f91735f73d18ee3cca17f72b8ada88e83fe4cd461512625
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gorilla-ruby
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Braden Schaeffer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Gorilla.io API client for Ruby
|
2
|
+
|
3
|
+
[](https://travis-ci.org/thinkmechanic/gorilla-ruby)
|
4
|
+
|
5
|
+
A Ruby wrapper for the [Gorilla.io](http://gorilla.io) API.
|
6
|
+
|
7
|
+
* [Gorilla.io API Docs](http://docs.gorilla.io/)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'gorilla-io'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install gorilla-io
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
Options | Default | Description
|
28
|
+
--------|---------|-------------
|
29
|
+
`api.url` | `https://api.gorilla.io` | The Gorilla API url (probably won't change this).
|
30
|
+
`api.version` | `1` | The desired Gorilla API version.
|
31
|
+
`api.key` | `ENV['GORILLA_API_KEY']` | Your Gorilla API key.
|
32
|
+
`api.secret` | `ENV['GORILLA_API_SECRET']` | Your Gorilla API secret.
|
33
|
+
`api.token_duration` | `300` (5 minutes) | Amount in seconds generated signatures are good for.
|
34
|
+
`user_agent` | `Gorilla Ruby Client/{version}` | The user agent for the client.
|
35
|
+
`client_adapter` | `Faraday.default_adapter` | Enables easily changing the adapter in a test enviornment.
|
36
|
+
|
37
|
+
#### Configuring Clients
|
38
|
+
|
39
|
+
You can configure all clients globally, or you can configure any `api` option
|
40
|
+
from above on a per-client basis:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
Gorilla.configure do |c|
|
44
|
+
c.api.key 'your-key'
|
45
|
+
c.api.secret 'your-secret'
|
46
|
+
end
|
47
|
+
|
48
|
+
# Uses the global config
|
49
|
+
client = Gorilla::Client.new
|
50
|
+
|
51
|
+
# Uses the global config, overrides key & secret
|
52
|
+
client = Gorilla::Client.new({
|
53
|
+
key: 'other-key',
|
54
|
+
secret: 'other-secret'
|
55
|
+
})
|
56
|
+
```
|
57
|
+
|
58
|
+
You can also customize the Faraday middleware stack by passing a block to the
|
59
|
+
client that will be passed a `Faraday::Connection` and a hash of the current
|
60
|
+
`api` options. In fact, [`Gorilla::Client`](lib/gorilla/client.rb) is just a
|
61
|
+
[`Gorilla::VanillaClient`](lib/gorilla/vanilla_client.rb) that does just that.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
gorilla_client = Gorilla::VanillaClient.new do |conn, options|
|
65
|
+
conn.request :api_version, options[:version]
|
66
|
+
conn.request :signature_auth, options
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/[my-github-username]/gorilla-client/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/gorilla-io.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gorilla/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'gorilla-io'
|
8
|
+
spec.version = Gorilla::VERSION
|
9
|
+
spec.authors = ['Gorilla.io']
|
10
|
+
spec.email = ['braden@gorilla.io']
|
11
|
+
spec.summary = %q{Ruby wrapper for the Gorilla.io API}
|
12
|
+
spec.description = %q{Ruby wrapper for the Gorilla.io API}
|
13
|
+
spec.homepage = 'https://github.com/thinkmechanic/gorilla-ruby'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
|
25
|
+
spec.add_dependency 'faraday', '~> 0.9'
|
26
|
+
spec.add_dependency 'faraday_middleware', '~> 0.9'
|
27
|
+
spec.add_dependency 'configurations', '~> 1.4'
|
28
|
+
spec.add_dependency 'jwt', '~> 1.2'
|
29
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Gorilla
|
2
|
+
class Client < VanillaClient
|
3
|
+
|
4
|
+
def initialize(opts={}, &block)
|
5
|
+
super(opts) do |conn, options|
|
6
|
+
conn.request :api_version, options[:version]
|
7
|
+
conn.request :signature_auth, options
|
8
|
+
yield(conn) if block_given?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Gorilla
|
2
|
+
# Top-level error for rescuing all Gorilla Client errors
|
3
|
+
class Error < StandardError
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
# Raised when Gorilla.io returns the HTTP status code 400
|
10
|
+
class BadRequest < Error; end
|
11
|
+
|
12
|
+
# Raised when Gorilla.io returns the HTTP status code 401
|
13
|
+
class Unauthorized < Error; end
|
14
|
+
|
15
|
+
# Raised when Gorilla.io returns the HTTP status code 404
|
16
|
+
class NotFound < Error; end
|
17
|
+
|
18
|
+
# Raised when Gorilla.io returns the HTTP status code 422
|
19
|
+
class ValidationError < Error; end
|
20
|
+
|
21
|
+
# Raised when Gorilla.io returns the HTTP status code 50x
|
22
|
+
class ServerError < Error; end
|
23
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Gorilla
|
2
|
+
module Middleware
|
3
|
+
class ApiVersion < Faraday::Middleware
|
4
|
+
|
5
|
+
VERSION_HEADER = 'application/vnd.gorilla.v%s+json'.freeze
|
6
|
+
|
7
|
+
def initialize(app, version)
|
8
|
+
super(app)
|
9
|
+
@version = version
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
env[:request_headers]['Accept'] = accept_header
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def accept_header
|
20
|
+
VERSION_HEADER % @version
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Gorilla
|
2
|
+
module Middleware
|
3
|
+
class HttpExceptions < Faraday::Middleware
|
4
|
+
|
5
|
+
def call(env)
|
6
|
+
@app.call(env).on_complete do |response|
|
7
|
+
case response[:status].to_s.strip
|
8
|
+
when '400'
|
9
|
+
raise_error Gorilla::BadRequest, response
|
10
|
+
when '401'
|
11
|
+
raise_error Gorilla::Unauthorized, response
|
12
|
+
when '404'
|
13
|
+
raise_error Gorilla::NotFound, response
|
14
|
+
when '422'
|
15
|
+
raise_error Gorilla::ValidationError, response
|
16
|
+
when /50\d/
|
17
|
+
raise_error Gorilla::ServerError, response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def raise_error(klass, response)
|
25
|
+
msg = if response[:body].kind_of?(Hash)
|
26
|
+
error = response[:body]['error']
|
27
|
+
error['code'] + ' - ' + error['message']
|
28
|
+
else
|
29
|
+
response[:body]
|
30
|
+
end
|
31
|
+
|
32
|
+
raise klass.new(response), [response[:status], msg].compact.join(': ')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Gorilla
|
2
|
+
module Middleware
|
3
|
+
class SignatureAuth < Faraday::Middleware
|
4
|
+
|
5
|
+
SIGNATURE_METHOD = 'Signature'.freeze
|
6
|
+
SIGNATURE_ALGO = 'HS256'.freeze
|
7
|
+
|
8
|
+
def initialize(app, opts={})
|
9
|
+
[:key, :secret].each do |key|
|
10
|
+
raise ArgumentError, "#{key.inspect} is required" if !opts[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
opts[:token_duration] ||= 5 * 60
|
14
|
+
|
15
|
+
super(app)
|
16
|
+
@opts = opts
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
env[:request_headers]['Authorization'] = build_auth_header(env)
|
21
|
+
@app.call(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def build_auth_header(env)
|
27
|
+
token = build_token(env)
|
28
|
+
"#{SIGNATURE_METHOD} #{@opts[:key]} #{token}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_token(env)
|
32
|
+
JWT.encode({
|
33
|
+
exp: Time.now.utc.to_i + @opts[:token_duration].to_i,
|
34
|
+
method: env[:method].to_s.upcase,
|
35
|
+
path: env[:url].path.split('?').first
|
36
|
+
}, @opts[:secret], SIGNATURE_ALGO)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Gorilla
|
2
|
+
module Middleware
|
3
|
+
class UserAgent < Faraday::Middleware
|
4
|
+
def initialize(app, agent_string)
|
5
|
+
super(app)
|
6
|
+
@agent_string = agent_string
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
env[:request_headers]['User-Agent'] = @agent_string
|
11
|
+
@app.call(env)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Gorilla
|
2
|
+
class VanillaClient
|
3
|
+
|
4
|
+
attr_reader :connection
|
5
|
+
|
6
|
+
def initialize(opts={}, &block)
|
7
|
+
options = config.api.to_h.merge(opts)
|
8
|
+
|
9
|
+
@connection = Faraday.new(options[:url]) do |conn|
|
10
|
+
conn.request :user_agent, config.user_agent
|
11
|
+
conn.request :json
|
12
|
+
yield(conn, options) if block_given?
|
13
|
+
conn.response :json, content_type: /\bjson$/
|
14
|
+
conn.adapter config.client_adapter
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(path, params={})
|
19
|
+
connection.get(path, params)
|
20
|
+
end
|
21
|
+
|
22
|
+
def post(path, params={})
|
23
|
+
connection.post(path, params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def put(path, params={})
|
27
|
+
connection.put(path, params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def delete(path, params={})
|
31
|
+
connection.delete(path, params)
|
32
|
+
end
|
33
|
+
|
34
|
+
def config
|
35
|
+
Gorilla.configuration
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/gorilla-io.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'gorilla'
|
data/lib/gorilla.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'configurations'
|
4
|
+
require 'jwt'
|
5
|
+
|
6
|
+
require 'gorilla/version'
|
7
|
+
|
8
|
+
require 'gorilla/error'
|
9
|
+
require 'gorilla/middleware/api_version'
|
10
|
+
require 'gorilla/middleware/user_agent'
|
11
|
+
require 'gorilla/middleware/signature_auth'
|
12
|
+
require 'gorilla/middleware/http_exceptions'
|
13
|
+
|
14
|
+
require 'gorilla/vanilla_client'
|
15
|
+
require 'gorilla/client'
|
16
|
+
|
17
|
+
module Gorilla
|
18
|
+
include Configurations
|
19
|
+
|
20
|
+
configurable api: %i{url version key secret token_duration}
|
21
|
+
configurable :user_agent
|
22
|
+
configurable :client_adapter
|
23
|
+
|
24
|
+
configuration_defaults do |c|
|
25
|
+
c.api.url = 'https://api.gorilla.io/'
|
26
|
+
c.api.version = 1
|
27
|
+
c.api.key = ENV['GORILLA_API_KEY']
|
28
|
+
c.api.secret = ENV['GORILLA_API_SECRET']
|
29
|
+
c.api.token_duration = 5 * 60
|
30
|
+
|
31
|
+
c.user_agent = "Gorilla Client/#{VERSION}"
|
32
|
+
c.client_adapter = Faraday.default_adapter
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.testing!
|
36
|
+
stub = Faraday::Adapter::Test::Stubs.new
|
37
|
+
configuration.client_adapter = stub
|
38
|
+
stub
|
39
|
+
end
|
40
|
+
|
41
|
+
Faraday::Request.register_middleware \
|
42
|
+
user_agent: Gorilla::Middleware::UserAgent,
|
43
|
+
api_version: Gorilla::Middleware::ApiVersion,
|
44
|
+
signature_auth: Gorilla::Middleware::SignatureAuth
|
45
|
+
|
46
|
+
Faraday::Response.register_middleware \
|
47
|
+
http_exceptions: Gorilla::Middleware::HttpExceptions
|
48
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe_request_middleware Gorilla::Middleware::ApiVersion do
|
4
|
+
let(:version) { 1 }
|
5
|
+
let(:args) { [version] }
|
6
|
+
|
7
|
+
it "sets the Accept header" do
|
8
|
+
result = process(nil)
|
9
|
+
accept = result[:request_headers]['accept']
|
10
|
+
expect(accept).to eq("application/vnd.gorilla.v#{version}+json")
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe_response_middleware Gorilla::Middleware::HttpExceptions do
|
4
|
+
|
5
|
+
{
|
6
|
+
400 => Gorilla::BadRequest,
|
7
|
+
401 => Gorilla::Unauthorized,
|
8
|
+
404 => Gorilla::NotFound,
|
9
|
+
422 => Gorilla::ValidationError,
|
10
|
+
500 => Gorilla::ServerError,
|
11
|
+
503 => Gorilla::ServerError
|
12
|
+
}.each do |status, error|
|
13
|
+
it "raises a #{error.name} for HTTP status code #{status}" do
|
14
|
+
expect { process(nil, status) }.to raise_error(error)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises a Gorilla::Error with the correct message from a Hash' do
|
19
|
+
expect {
|
20
|
+
process({'error' => {'code' => 'code', 'message' => 'message'}}, 400)
|
21
|
+
}.to raise_error(Gorilla::Error, '400: code - message')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'raises a Gorilla::Error with the correct message from a String' do
|
25
|
+
expect {
|
26
|
+
process('This is a string', 400)
|
27
|
+
}.to raise_error(Gorilla::Error, '400: This is a string')
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe_request_middleware Gorilla::Middleware::SignatureAuth do
|
4
|
+
let(:api_key) { 'api-key' }
|
5
|
+
let(:api_secret) { 'api-secret' }
|
6
|
+
let(:token_duration) { 5 * 60 }
|
7
|
+
|
8
|
+
let(:args) { [{
|
9
|
+
key: api_key,
|
10
|
+
secret: api_secret,
|
11
|
+
token_duration: token_duration
|
12
|
+
}] }
|
13
|
+
|
14
|
+
let(:test_time) { Time.mktime(2015, 1, 1) }
|
15
|
+
before { allow(Time).to receive(:now).and_return(test_time) }
|
16
|
+
|
17
|
+
let(:token) do
|
18
|
+
JWT.encode({
|
19
|
+
exp: test_time.utc.to_i + token_duration.to_i,
|
20
|
+
method: 'GET',
|
21
|
+
path: '/forms'
|
22
|
+
}, api_secret, 'HS256')
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:result) do
|
26
|
+
process(nil) do |env|
|
27
|
+
env[:method] = :get
|
28
|
+
env[:url] = URI.parse('https://api.gorilla.io/forms')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'missing an :api_key' do
|
33
|
+
let(:api_key) { nil }
|
34
|
+
|
35
|
+
it 'raises an ArgumentError' do
|
36
|
+
expect { result }.to raise_error(ArgumentError, ':key is required')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'missing an :api_secret' do
|
41
|
+
let(:api_secret) { nil }
|
42
|
+
|
43
|
+
it 'raises an ArgumentError' do
|
44
|
+
expect { result }.to raise_error(ArgumentError, ':secret is required')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'with valid credentials' do
|
49
|
+
it 'sets the Authorization header' do
|
50
|
+
auth = result[:request_headers]['authorization']
|
51
|
+
expect(auth).to eq("Signature #{api_key} #{token}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe_request_middleware Gorilla::Middleware::UserAgent do
|
4
|
+
let(:args) { ['Gorilla Agent'] }
|
5
|
+
|
6
|
+
it "sets the User-Agent header" do
|
7
|
+
result = process(nil)
|
8
|
+
agent = result[:request_headers]['user-agent']
|
9
|
+
expect(agent).to eq('Gorilla Agent')
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Gorilla::VanillaClient do
|
4
|
+
|
5
|
+
let(:client) { Gorilla::VanillaClient.new }
|
6
|
+
|
7
|
+
describe 'initializing' do
|
8
|
+
it 'uses the url from Gorilla.configuration given no :url option' do
|
9
|
+
expect(Faraday).to receive(:new).with(Gorilla.configuration.api.url)
|
10
|
+
Gorilla::VanillaClient.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'overrides the url given a :url options' do
|
14
|
+
expect(Faraday).to receive(:new).with('http://test.com/')
|
15
|
+
Gorilla::VanillaClient.new(url: 'http://test.com/')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'yields correctly' do
|
19
|
+
Gorilla::VanillaClient.new do |conn, options|
|
20
|
+
expect(conn).to be_a(Faraday::Connection)
|
21
|
+
expect(options).to eq(Gorilla.configuration.api.to_h)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'overrides the default :api configuration options' do
|
26
|
+
Gorilla::VanillaClient.new(key: 'test-key') do |conn, options|
|
27
|
+
expect(options[:key]).to eq('test-key')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'builds the default stack' do
|
32
|
+
conn_stub = double('Faraday::Connection', {
|
33
|
+
adapter: nil,
|
34
|
+
request: nil,
|
35
|
+
response: nil
|
36
|
+
})
|
37
|
+
|
38
|
+
expect(Faraday).to receive(:new).and_yield(conn_stub)
|
39
|
+
|
40
|
+
expect_stack conn_stub,
|
41
|
+
[:request, :user_agent, Gorilla.configuration.user_agent],
|
42
|
+
[:request, :json],
|
43
|
+
[:response, :json, content_type: /\bjson$/],
|
44
|
+
[:adapter, Gorilla.configuration.client_adapter]
|
45
|
+
|
46
|
+
Gorilla::VanillaClient.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
[:get, :post, :put, :delete].each do |verb|
|
51
|
+
describe "##{verb}" do
|
52
|
+
it 'delegates to connection' do
|
53
|
+
expect(client.connection).to receive(verb)
|
54
|
+
.with('/path', {param: 'value'})
|
55
|
+
.and_return(nil)
|
56
|
+
|
57
|
+
client.send(verb, '/path', {param: 'value'})
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#connection' do
|
63
|
+
subject { client.connection }
|
64
|
+
it { is_expected.to be_a(Faraday::Connection) }
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def expect_stack(conn, *stacks)
|
70
|
+
stacks.each do |stack|
|
71
|
+
method, *args = stack
|
72
|
+
expect(conn).to receive(method).with(*args)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'gorilla-io'
|
5
|
+
|
6
|
+
# Requires supporting ruby files
|
7
|
+
Dir["spec/support/**/*.rb"].each { |f| require File.expand_path("./#{f}") }
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
if config.files_to_run.one?
|
11
|
+
config.default_formatter = 'doc'
|
12
|
+
end
|
13
|
+
|
14
|
+
config.order = :random
|
15
|
+
Kernel.srand config.seed
|
16
|
+
|
17
|
+
config.expect_with :rspec do |expectations|
|
18
|
+
expectations.syntax = :expect
|
19
|
+
end
|
20
|
+
|
21
|
+
config.mock_with :rspec do |mocks|
|
22
|
+
mocks.syntax = :expect
|
23
|
+
mocks.verify_partial_doubles = true
|
24
|
+
end
|
25
|
+
|
26
|
+
# Example groups
|
27
|
+
config.alias_example_group_to :describe_request_middleware, type: :request
|
28
|
+
config.alias_example_group_to :describe_response_middleware, type: :response
|
29
|
+
|
30
|
+
# Reset API Key configuration
|
31
|
+
Gorilla.configure do |c|
|
32
|
+
c.api.key = 'api-key'
|
33
|
+
c.api.secret = 'api-secret'
|
34
|
+
end
|
35
|
+
|
36
|
+
config.include FaradayHelpers
|
37
|
+
config.include RequestMiddlewareContext, type: :request
|
38
|
+
config.include ResponseMiddlewareContext, type: :response
|
39
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module FaradayHelpers
|
2
|
+
def faraday_env(env)
|
3
|
+
Faraday::Env.from(env)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
module RequestMiddlewareContext
|
8
|
+
def self.included(base)
|
9
|
+
base.let(:args) { [] }
|
10
|
+
base.let(:middleware) do
|
11
|
+
described_class.new(lambda{|env| env}, *args)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def process(body, &block)
|
16
|
+
env = {
|
17
|
+
body: body,
|
18
|
+
request_headers: Faraday::Utils::Headers.new
|
19
|
+
}
|
20
|
+
|
21
|
+
yield env if block_given?
|
22
|
+
middleware.call(faraday_env(env))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module ResponseMiddlewareContext
|
27
|
+
def self.included(base)
|
28
|
+
base.let(:args) { [] }
|
29
|
+
base.let(:headers) { Hash.new }
|
30
|
+
base.let(:middleware) {
|
31
|
+
described_class.new(lambda {|env|
|
32
|
+
Faraday::Response.new(env)
|
33
|
+
}, *args)
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def process(body, status, options = {})
|
38
|
+
env = {
|
39
|
+
body: body,
|
40
|
+
status: status,
|
41
|
+
request: options,
|
42
|
+
request_headers: Faraday::Utils::Headers.new,
|
43
|
+
response_headers: Faraday::Utils::Headers.new(headers)
|
44
|
+
}
|
45
|
+
|
46
|
+
yield env if block_given?
|
47
|
+
middleware.call(faraday_env(env))
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gorilla-io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gorilla.io
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday_middleware
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: configurations
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.4'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.4'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: jwt
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.2'
|
111
|
+
description: Ruby wrapper for the Gorilla.io API
|
112
|
+
email:
|
113
|
+
- braden@gorilla.io
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".ruby-gemset"
|
121
|
+
- ".ruby-version"
|
122
|
+
- ".travis.yml"
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- gorilla-io.gemspec
|
128
|
+
- lib/gorilla-io.rb
|
129
|
+
- lib/gorilla.rb
|
130
|
+
- lib/gorilla/client.rb
|
131
|
+
- lib/gorilla/error.rb
|
132
|
+
- lib/gorilla/middleware/api_version.rb
|
133
|
+
- lib/gorilla/middleware/http_exceptions.rb
|
134
|
+
- lib/gorilla/middleware/signature_auth.rb
|
135
|
+
- lib/gorilla/middleware/user_agent.rb
|
136
|
+
- lib/gorilla/vanilla_client.rb
|
137
|
+
- lib/gorilla/version.rb
|
138
|
+
- spec/lib/gorilla/middleware/api_version_spec.rb
|
139
|
+
- spec/lib/gorilla/middleware/http_exceptions_spec.rb
|
140
|
+
- spec/lib/gorilla/middleware/signature_auth_spec.rb
|
141
|
+
- spec/lib/gorilla/middleware/user_agent_spec.rb
|
142
|
+
- spec/lib/gorilla/vanilla_client_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/support/faraday_helpers.rb
|
145
|
+
homepage: https://github.com/thinkmechanic/gorilla-ruby
|
146
|
+
licenses:
|
147
|
+
- MIT
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - ">="
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.4.3
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Ruby wrapper for the Gorilla.io API
|
169
|
+
test_files:
|
170
|
+
- spec/lib/gorilla/middleware/api_version_spec.rb
|
171
|
+
- spec/lib/gorilla/middleware/http_exceptions_spec.rb
|
172
|
+
- spec/lib/gorilla/middleware/signature_auth_spec.rb
|
173
|
+
- spec/lib/gorilla/middleware/user_agent_spec.rb
|
174
|
+
- spec/lib/gorilla/vanilla_client_spec.rb
|
175
|
+
- spec/spec_helper.rb
|
176
|
+
- spec/support/faraday_helpers.rb
|