swaggable 0.5.3 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0ba12b2dc9c54ce2f6920b7d6fd6c45fe056ff5
4
- data.tar.gz: 27874e95a2cfcdd88f67c3ef2bbd024660c979a3
3
+ metadata.gz: bf18bc08944dd205e9633c7e85a69ac1e0fe9908
4
+ data.tar.gz: 10a0e3fb8a88842000cd28b29838609106b7c7a4
5
5
  SHA512:
6
- metadata.gz: bf44a0e08c55b0f6e948f73d711d7c758b77ae00a90dad0007451c1102265f0c44a928c868827be588504f380797f6c548826af5ab68c260bfcae7e4014712e2
7
- data.tar.gz: faab31754af448942d0e7f9012c233a28e004c718a750c2adbd450082ecdd5c1f3b40fa1c3921e199ed6b6a20b5f52537a6c7953b0e6c689c50d08b820088cbd
6
+ metadata.gz: d5e0e8fdd32d892ffe06e0b5d801db1d7286a9e67dd64d3edbbc339636b8e3ab652d8cbf5b12ace2e9e9587feb288bbe93dee5089a4e7590939cb79514c68a03
7
+ data.tar.gz: 42f21abe51eb383ff37bbdc8c018d19f2246a2f78d59a9b66fb045d9a48c3d6f93283a1e5799c2c0d55aae4fd413179c41e47de9083ed201a2b8c49a60c3ef56
data/lib/swaggable.rb CHANGED
@@ -16,4 +16,5 @@ module Swaggable
16
16
  autoload :EnumerableAttributes, 'swaggable/enumerable_attributes'
17
17
  autoload :SchemaDefinition, 'swaggable/schema_definition'
18
18
  autoload :AttributeDefinition, 'swaggable/attribute_definition'
19
+ autoload :RackRedirect, 'swaggable/rack_redirect'
19
20
  end
@@ -0,0 +1,47 @@
1
+ module Swaggable
2
+ class RackRedirect
3
+ attr_accessor(
4
+ :to,
5
+ :from,
6
+ :next_app,
7
+ )
8
+
9
+ def initialize(first_arg, second_arg = nil)
10
+ if second_arg
11
+ next_app = first_arg
12
+ options = second_arg.dup
13
+ else
14
+ next_app = nil
15
+ options = first_arg.dup
16
+ end
17
+
18
+ @next_app = next_app
19
+ @from = options.delete(:from)
20
+ @to = options.delete(:to) || raise(ArgumentError.new(":to option is mandatory"))
21
+
22
+ if options.any?
23
+ raise ArgumentError.new "Unsupported options #{options.keys.inspect}"
24
+ end
25
+ end
26
+
27
+ def call env
28
+ if from.nil?
29
+ redirect
30
+ elsif from.is_a?(String) && env['PATH_INFO'] == from
31
+ redirect
32
+ elsif from.is_a?(Regexp) && env['PATH_INFO'] =~ from
33
+ redirect
34
+ elsif next_app.nil?
35
+ raise('No application defined to forward the request to')
36
+ else
37
+ next_app.call env
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def redirect
44
+ [301, {'Location' => to}, []]
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Swaggable
2
- VERSION = '0.5.3'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -0,0 +1,81 @@
1
+ require_relative '../spec_helper'
2
+ require 'rack/test'
3
+
4
+ RSpec.describe 'Swaggable::RackRedirect' do
5
+ include Rack::Test::Methods
6
+
7
+ let(:subject_class) { Swaggable::RackRedirect }
8
+ let(:subject_instance) { Swaggable::RackRedirect.new }
9
+ subject { subject_instance }
10
+
11
+ let(:next_app) { double('next_app', call: [200, {}, []]) }
12
+
13
+ it 'fails if not expected options are given' do
14
+ expect { Swaggable::RackRedirect.new to: '/', unexpected: nil }.to raise_error ArgumentError
15
+ end
16
+
17
+ it 'fails if :to is not given' do
18
+ expect { Swaggable::RackRedirect.new from: '/'}.to raise_error ArgumentError
19
+ end
20
+
21
+ describe 'within map' do
22
+ def app
23
+ @app ||= Rack::Builder.new do
24
+ map('/origin') { run Swaggable::RackRedirect.new to: '/destination' }
25
+ end
26
+ end
27
+
28
+ it 'redirects /origin to /destination' do
29
+ get '/origin'
30
+ expect(last_response.status).to eq 301
31
+ expect(last_response.headers['Location']).to eq '/destination'
32
+ end
33
+ end
34
+
35
+ describe 'with use' do
36
+ describe 'with /origin as a string' do
37
+ def app
38
+ @app ||= Rack::Builder.new.tap do |r|
39
+ r.use Swaggable::RackRedirect, from: '/origin', to: '/destination'
40
+ r.run next_app
41
+ end
42
+ end
43
+
44
+ it 'redirects /origin to /destination' do
45
+ get '/origin'
46
+ expect(last_response.status).to eq 301
47
+ expect(last_response.headers['Location']).to eq '/destination'
48
+ end
49
+
50
+ it 'forwards the request to the next app if path is not /origin' do
51
+ expect(next_app).to receive :call
52
+ get '/other'
53
+ end
54
+
55
+ it 'forwards the request to the next app if path is /origin/something_else' do
56
+ expect(next_app).to receive :call
57
+ get '/origin/something_else'
58
+ end
59
+ end
60
+
61
+ describe 'with /origin as a regex' do
62
+ def app
63
+ @app ||= Rack::Builder.new.tap do |r|
64
+ r.use Swaggable::RackRedirect, from: /origin/, to: '/destination'
65
+ r.run next_app
66
+ end
67
+ end
68
+
69
+ it 'redirects /origin to /destination if the regex matches' do
70
+ get '/match_origin'
71
+ expect(last_response.status).to eq 301
72
+ expect(last_response.headers['Location']).to eq '/destination'
73
+ end
74
+
75
+ it 'forwards the request to the next app if the regex doesn\'t match' do
76
+ expect(next_app).to receive :call
77
+ get '/no_match'
78
+ end
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swaggable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Morales
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-13 00:00:00.000000000 Z
11
+ date: 2015-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forwarding_dsl
@@ -117,6 +117,7 @@ files:
117
117
  - lib/swaggable/grape_entity_translator.rb
118
118
  - lib/swaggable/parameter_definition.rb
119
119
  - lib/swaggable/rack_app.rb
120
+ - lib/swaggable/rack_redirect.rb
120
121
  - lib/swaggable/response_definition.rb
121
122
  - lib/swaggable/schema_definition.rb
122
123
  - lib/swaggable/swagger_2_serializer.rb
@@ -133,6 +134,7 @@ files:
133
134
  - spec/swaggable/integration_spec.rb
134
135
  - spec/swaggable/parameter_definition_spec.rb
135
136
  - spec/swaggable/rack_app_spec.rb
137
+ - spec/swaggable/rack_redirect_spec.rb
136
138
  - spec/swaggable/response_definition_spec.rb
137
139
  - spec/swaggable/schema_definition_spec.rb
138
140
  - spec/swaggable/swagger_2_serializer_spec.rb
@@ -176,6 +178,7 @@ test_files:
176
178
  - spec/swaggable/integration_spec.rb
177
179
  - spec/swaggable/parameter_definition_spec.rb
178
180
  - spec/swaggable/rack_app_spec.rb
181
+ - spec/swaggable/rack_redirect_spec.rb
179
182
  - spec/swaggable/response_definition_spec.rb
180
183
  - spec/swaggable/schema_definition_spec.rb
181
184
  - spec/swaggable/swagger_2_serializer_spec.rb