Skip to content

Commit

Permalink
Merge pull request #40 from funbox/changed_middlewares_params
Browse files Browse the repository at this point in the history
Added any params from tomograph
  • Loading branch information
tuwilof authored Jan 11, 2021
2 parents 3514dbe + d5b3233 commit 3c3980c
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 74 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
- name: Set up Ruby
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
# change this to (see https://github.com/ruby/setup-ruby#versioning):
# uses: ruby/setup-ruby@v1
uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
uses: ruby/setup-ruby@v1
# uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
with:
ruby-version: 2.6
ruby-version: 2.7
- name: Install dependencies
run: bundle install
- name: Run tests
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

### 1.7.0 - 2020-12-22

* improvements
* changed middleware arguments

### 1.6.0 - 2020-10-12

* features
Expand Down
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ $ gem install esplanade
`config/application.rb`:

```ruby
config.middleware.use Esplanade::SafeMiddleware, apib_path: 'doc.apib'
config.middleware.use Esplanade::SafeMiddleware, drafter_yaml_path: 'doc.yaml'
```

## Middlewares
Expand All @@ -72,17 +72,17 @@ It throws errors, so you should add your own middleware for processing.

```ruby
config.middleware.use YourMiddleware
config.middleware.use Esplanade::DangerousMiddleware, apib_path: 'doc.apib'
config.middleware.use Esplanade::DangerousMiddleware, drafter_yaml_path: 'doc.yaml'
```

### Esplanade::CheckCustomResponseMiddleware

Use it if you want to be sure that you have documented new custom responses.

```ruby
config.middleware.use Esplanade::CheckCustomResponseMiddleware, apib_path: 'doc.apib'
config.middleware.use Esplanade::CheckCustomResponseMiddleware, drafter_yaml_path: 'doc.yaml'
config.middleware.use YourMiddleware
config.middleware.use Esplanade::DangerousMiddleware, apib_path: 'doc.apib'
config.middleware.use Esplanade::DangerousMiddleware, drafter_yaml_path: 'doc.yaml'
```

## Esplanade::Error
Expand All @@ -95,7 +95,7 @@ Parent class for those described below. Inherited from `Esplanade::Error`.

#### Esplanade::Request::PrefixNotMatch

Error message format:
Error message format:

```ruby
{
Expand Down Expand Up @@ -169,7 +169,7 @@ Parent class for those described below. Inherited from `Esplanade::Error`.

#### Esplanade::Response::PrefixNotMatch

Error message format:
Error message format:

```ruby
{
Expand Down Expand Up @@ -233,17 +233,7 @@ Error message format:

## Middleware args

### `apib_path`

Path to API Blueprint documentation. There must be an installed [drafter](https://github.com/apiaryio/drafter) to parse it.

### `drafter_yaml_path`

Path to API Blueprint documentation pre-parsed with `drafter` and saved to a YAML file.

### `prefix`

Prefix for API requests. Example: `'/api'`. The prefix is added to the requests in the documentation.
Support any [tomograph constructor-params](https://github.com/funbox/tomograph/tree/master#constructor-params)

## License

Expand Down
6 changes: 2 additions & 4 deletions lib/esplanade/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
module Esplanade
class Configuration
attr_accessor :apib_path,
:drafter_yaml_path,
:prefix
attr_accessor :params

def initialize
@prefix = ''
@params = {}
end
end
end
13 changes: 2 additions & 11 deletions lib/esplanade/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

module Esplanade
class Middleware
def initialize(
app,
prefix: Esplanade.configuration.prefix,
apib_path: Esplanade.configuration.apib_path,
drafter_yaml_path: Esplanade.configuration.drafter_yaml_path
)
def initialize(app, **params)
@app = app
@documentation = Tomograph::Tomogram.new(
prefix: prefix,
apib_path: apib_path,
drafter_yaml_path: drafter_yaml_path
)
@documentation = Tomograph::Tomogram.new(Esplanade.configuration.params.merge(params))
end

def call(env)
Expand Down
13 changes: 2 additions & 11 deletions lib/esplanade/middlewares/check_custom_response_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

module Esplanade
class CheckCustomResponseMiddleware
def initialize(
app,
prefix: Esplanade.configuration.prefix,
apib_path: Esplanade.configuration.apib_path,
drafter_yaml_path: Esplanade.configuration.drafter_yaml_path
)
def initialize(app, **params)
@app = app
@documentation = Tomograph::Tomogram.new(
prefix: prefix,
apib_path: apib_path,
drafter_yaml_path: drafter_yaml_path
)
@documentation = Tomograph::Tomogram.new(Esplanade.configuration.params.merge(params))
end

def call(env)
Expand Down
13 changes: 2 additions & 11 deletions lib/esplanade/middlewares/dangerous_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

module Esplanade
class DangerousMiddleware
def initialize(
app,
prefix: Esplanade.configuration.prefix,
apib_path: Esplanade.configuration.apib_path,
drafter_yaml_path: Esplanade.configuration.drafter_yaml_path
)
def initialize(app, **params)
@app = app
@documentation = Tomograph::Tomogram.new(
prefix: prefix,
apib_path: apib_path,
drafter_yaml_path: drafter_yaml_path
)
@documentation = Tomograph::Tomogram.new(Esplanade.configuration.params.merge(params))
end

def call(env)
Expand Down
13 changes: 2 additions & 11 deletions lib/esplanade/middlewares/safe_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

module Esplanade
class SafeMiddleware
def initialize(
app,
prefix: Esplanade.configuration.prefix,
apib_path: Esplanade.configuration.apib_path,
drafter_yaml_path: Esplanade.configuration.drafter_yaml_path
)
def initialize(app, **params)
@app = app
@documentation = Tomograph::Tomogram.new(
prefix: prefix,
apib_path: apib_path,
drafter_yaml_path: drafter_yaml_path
)
@documentation = Tomograph::Tomogram.new(Esplanade.configuration.params.merge(params))
end

def call(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/esplanade/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Esplanade
VERSION = '1.6.0'.freeze
VERSION = '1.7.0'.freeze
end
4 changes: 2 additions & 2 deletions spec/lib/esplanade/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Esplanade::Middleware do
describe '#call' do
subject { described_class.new(app) }
subject { described_class.new(app, drafter_yaml_path: 'doc.yml') }

let(:app) { double(call: [status, headers, body]) }
let(:status) { double }
Expand All @@ -11,7 +11,7 @@
let(:env) { double }

before do
allow(Tomograph::Tomogram).to receive(:new)
allow(Tomograph::Tomogram).to receive(:new).with(drafter_yaml_path: 'doc.yml')
end

it 'returns response' do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/esplanade/response/doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
context 'prefix not match' do
let(:request) { double(doc: double(responses: []), raw: double(method: 'method', path: 'path', raw_path: 'path')) }
let(:raw_status) { 'status' }
let(:message) { {:request=>{:method=>"method", :path=>"path", :raw_path=>"path"}, :status=>"status"} }
let(:message) { { request: { method: 'method', path: 'path', raw_path: 'path' }, status: 'status' } }

before { allow(request).to receive_message_chain(:doc, :responses).and_raise(Esplanade::Request::PrefixNotMatch) }

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/esplanade_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
context 'if not default' do
it 'makes settings' do
Esplanade.configure do |config|
config.drafter_yaml_path = 'doc/api.yaml'
config.params = { prefix: 'doc/api.yaml' }
end
end
end

context 'if default' do
it 'makes settings' do
Esplanade.configure do |config|
config.prefix = '/api/v1'
config.params = { prefix: '/api/v1' }
end
end
end
Expand Down

0 comments on commit 3c3980c

Please sign in to comment.