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.
- data/lib/useless/doc/rack/application.rb +25 -11
- data/lib/useless/doc/rack/proxy.rb +22 -5
- data/lib/useless/doc/version.rb +1 -1
- metadata +1 -2
- data/config.ru +0 -4
@@ -12,18 +12,32 @@ require 'useless/doc/rack/ui'
|
|
12
12
|
module Useless
|
13
13
|
module Doc
|
14
14
|
module Rack
|
15
|
-
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
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
|
34
|
-
|
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'}, ['
|
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
|
#
|
data/lib/useless/doc/version.rb
CHANGED
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.
|
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