useless-doc 0.1.0 → 0.1.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.
@@ -12,18 +12,32 @@ require 'useless/doc/rack/ui'
12
12
  module Useless
13
13
  module Doc
14
14
  module Rack
15
- module Application
16
- def self.call(env)
17
- ::Rack::Builder.app do
18
- use Low::Rack::RackErrors
19
- use Low::Rack::RequestLogger
20
- use ::Rack::CommonLogger
21
- use Useless::Doc::Rack::UI
22
- use Useless::Doc::Rack::Stylesheet
23
- use Useless::Doc::Rack::Transform
15
+ class Application
16
+ def initialize(*subdomains)
17
+ @subdomains = subdomains
18
+ end
19
+
20
+ def call(env)
21
+ app.call(env)
22
+ end
23
+
24
+ private
25
+
26
+ def app
27
+ @app ||= begin
28
+ subdomains = @subdomains
29
+
30
+ ::Rack::Builder.app do
31
+ use Low::Rack::RackErrors
32
+ use Low::Rack::RequestLogger
33
+ use ::Rack::CommonLogger
34
+ use Useless::Doc::Rack::UI
35
+ use Useless::Doc::Rack::Stylesheet
36
+ use Useless::Doc::Rack::Transform
24
37
 
25
- run Useless::Doc::Rack::Proxy.new
26
- end.call(env)
38
+ run Useless::Doc::Rack::Proxy.new(subdomains)
39
+ end
40
+ end
27
41
  end
28
42
  end
29
43
  end
@@ -16,10 +16,16 @@ module Useless
16
16
  # OPTIONS request to some-api.useless.io/some/resource.
17
17
  #
18
18
  # If there is no corresponding endpoint, the proxy will respond with a
19
- # 404.
19
+ # 404. If the requested subdomain is not in the list of supported
20
+ # subdomains specified at initialization, the proxy will also respond
21
+ # with a 404.
20
22
  #
21
23
  class Proxy
22
24
 
25
+ def initialize(subdomains)
26
+ @subdomains = subdomains
27
+ end
28
+
23
29
  def self.transform_url(url)
24
30
  uri = URI(url)
25
31
  new_host = uri.host.gsub(/\.doc\./, '.')
@@ -28,20 +34,31 @@ module Useless
28
34
 
29
35
  def call(env)
30
36
  request = ::Rack::Request.new(env)
31
- url = Proxy.transform_url(request.url)
32
37
 
33
- if json = retrieve_resource(url)
34
- [200, {'Content-Type' => 'application/json'}, [json]]
38
+ if valid_subdomain?(request.url)
39
+ url = Proxy.transform_url(request.url)
40
+
41
+ if json = retrieve_resource(url)
42
+ [200, {'Content-Type' => 'application/json'}, [json]]
43
+ else
44
+ [404, {'Content-Type' => 'text/plain'}, ['Documentation JSON is missing.']]
45
+ end
35
46
  else
36
- [404, {'Content-Type' => 'text/plain'}, ['Documentation JSON is missing.']]
47
+ [404, {'Content-Type' => 'text/plain'}, ['Unknown subdomain.']]
37
48
  end
38
49
  end
39
50
 
51
+ private
52
+
40
53
  def retrieve_resource(url)
41
54
  response = Typhoeus.options url, headers: { 'Accept' => 'application/json' }
42
55
  response.response_body
43
56
  end
44
57
 
58
+ def valid_subdomain?(url)
59
+ @subdomains.include?(url[/((?:\w|\-)+)\.doc\./, 1])
60
+ end
61
+
45
62
  # +Proxy::Stub+ retrieves JSON from the spec/documents directory for
46
63
  # easy UI testing.
47
64
  #
@@ -1,5 +1,5 @@
1
1
  module Useless
2
2
  module Doc
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: useless-doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -151,7 +151,6 @@ files:
151
151
  - LICENSE.txt
152
152
  - README.md
153
153
  - Rakefile
154
- - config.ru
155
154
  - lib/useless/doc.rb
156
155
  - lib/useless/doc/action.rb
157
156
  - lib/useless/doc/body.rb
data/config.ru DELETED
@@ -1,4 +0,0 @@
1
- $:.push File.dirname(__FILE__) + '/lib'
2
- require 'useless/doc/rack/application'
3
-
4
- run Useless::Doc::Rack::Application