useless-doc 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/lib/useless/doc/client.rb +62 -0
  2. data/lib/useless/doc/core/api.rb +6 -2
  3. data/lib/useless/doc/core/domain.rb +34 -0
  4. data/lib/useless/doc/dsl.rb +30 -0
  5. data/lib/useless/doc/rack/{stylesheet.rb → css.rb} +8 -8
  6. data/lib/useless/doc/rack/html.rb +26 -0
  7. data/lib/useless/doc/rack/subject.rb +40 -0
  8. data/lib/useless/doc/rack/ui.rb +12 -11
  9. data/lib/useless/doc/rack/url.rb +33 -0
  10. data/lib/useless/doc/rack.rb +46 -0
  11. data/lib/useless/doc/router.rb +62 -0
  12. data/lib/useless/doc/serialization/dump.rb +17 -0
  13. data/lib/useless/doc/serialization/load.rb +35 -0
  14. data/lib/useless/doc/sinatra.rb +4 -3
  15. data/lib/useless/doc/ui/godel/api.mustache +24 -24
  16. data/lib/useless/doc/ui/godel/domain.mustache +26 -0
  17. data/lib/useless/doc/ui/godel/resource.mustache +151 -155
  18. data/lib/useless/doc/ui/godel/stylesheet.css +156 -1
  19. data/lib/useless/doc/ui/godel.rb +54 -12
  20. data/lib/useless/doc/ui.rb +24 -0
  21. data/lib/useless/doc/version.rb +1 -1
  22. data/lib/useless/doc.rb +20 -2
  23. data/spec/config.ru +19 -0
  24. data/spec/documents/api.json +1 -0
  25. data/spec/documents/domain.json +17 -0
  26. data/spec/useless/doc/client_spec.rb +59 -0
  27. data/spec/useless/doc/dsl_spec.rb +28 -2
  28. data/spec/useless/doc/rack/{stylesheet_spec.rb → css_spec.rb} +3 -9
  29. data/spec/useless/doc/rack/html_spec.rb +39 -0
  30. data/spec/useless/doc/rack/subject_spec.rb +44 -0
  31. data/spec/useless/doc/rack/ui_spec.rb +1 -1
  32. data/spec/useless/doc/rack/url_spec.rb +35 -0
  33. data/spec/useless/doc/{rack/application_spec.rb → rack_spec.rb} +22 -11
  34. data/spec/useless/doc/router_spec.rb +38 -0
  35. data/spec/useless/doc/serialization/dump_spec.rb +29 -0
  36. data/spec/useless/doc/serialization/load_spec.rb +37 -0
  37. data/spec/useless/doc/sinatra_spec.rb +3 -1
  38. data/spec/useless/doc/ui/godel_spec.rb +136 -96
  39. data/useless-doc.gemspec +1 -1
  40. metadata +34 -21
  41. data/lib/useless/doc/rack/application.rb +0 -47
  42. data/lib/useless/doc/rack/proxy.rb +0 -62
  43. data/lib/useless/doc/rack/retriever.rb +0 -68
  44. data/lib/useless/doc/rack/transform.rb +0 -46
  45. data/spec/useless/doc/rack/proxy_spec.rb +0 -56
  46. data/spec/useless/doc/rack/retriever_spec.rb +0 -82
  47. data/spec/useless/doc/rack/transform_spec.rb +0 -57
@@ -1,56 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
-
3
- require 'rack/test'
4
- require 'useless/doc/rack/proxy'
5
-
6
- describe Useless::Doc::Rack::Proxy do
7
- include Rack::Test::Methods
8
-
9
- def app
10
- @app ||= lambda do |env|
11
- env['useless.doc.retriever'] = Useless::Doc::Rack::Retriever::Stub
12
- Useless::Doc::Rack::Proxy.new('some-api').call(env)
13
- end
14
- end
15
-
16
- it 'should proxy GET requests as an OPTIONS request to the corresponding API resource.' do
17
- Useless::Doc::Rack::Retriever::Stub.should_receive(:retrieve).
18
- with('http://some-api.granmal.com/some/resource').
19
- and_return('{ "some": "response" }')
20
-
21
- get 'http://some-api.doc.granmal.com/some/resource'
22
- last_response.should be_ok
23
- end
24
-
25
- it 'should return a 404 if the proxy call returns nothing' do
26
- Useless::Doc::Rack::Retriever::Stub.should_receive(:retrieve).
27
- with('http://some-api.granmal.com/some/non-existant/resource').
28
- and_return(nil)
29
-
30
- get 'http://some-api.doc.granmal.com/some/non-existant/resource'
31
- last_response.should be_not_found
32
- end
33
-
34
- it 'should return a 404 if the subdomain is unknown' do
35
- get 'http://some-other-api.doc.granmal.com/some/non-existant/resource'
36
- last_response.should be_not_found
37
- end
38
-
39
- it 'should raise an error if a retriever is not specified' do
40
- app = Useless::Doc::Rack::Proxy.new('some-api')
41
- request = Rack::MockRequest.new(app)
42
- lambda { request.get('http://some-api.granmal.com/some/non-existant/resource') }.should raise_error(RuntimeError, 'No retriever specified.')
43
- end
44
-
45
- describe '.transform_url' do
46
- it 'should drop the \'doc\' subdomain from the url for the \'io\' TLD' do
47
- url = Useless::Doc::Rack::Proxy.transform_url 'http://some-api.doc.useless.io/some/resource'
48
- url.should == 'http://some-api.useless.io/some/resource'
49
- end
50
-
51
- it 'should drop the \'doc\' subdomain from the url for the \'dev\' TLD' do
52
- url = Useless::Doc::Rack::Proxy.transform_url 'http://some-api.doc.useless.dev/some/resource'
53
- url.should == 'http://some-api.useless.dev/some/resource'
54
- end
55
- end
56
- end
@@ -1,82 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
-
3
- require 'time'
4
- require 'rack/test'
5
- require 'useless/doc/rack/retriever'
6
-
7
- describe Useless::Doc::Rack::Retriever do
8
- include Rack::Test::Methods
9
-
10
- def app
11
- @app ||= begin
12
- base_app = lambda do |env|
13
- [200, {'Content-Type' => 'text/plain'}, [env['useless.doc.retriever'].name]]
14
- end
15
-
16
- Useless::Doc::Rack::Retriever.new(base_app)
17
- end
18
- end
19
-
20
- it 'should set \'useless.doc.retriever\' to an instance of `Rack::Retriever::Standard` if the RACK_ENV is production' do
21
- begin
22
- ENV['RACK_ENV'] = 'production'
23
- get 'http://some-api.doc.granmal.com/some/resource'
24
- last_response.body.should == 'Useless::Doc::Rack::Retriever::Standard'
25
- ensure
26
- ENV['RACK_ENV'] = 'test'
27
- end
28
- end
29
-
30
- it 'should set \'useless.doc.retriever\' to an instance of `Rack::Retriever::Stub` if the RACK_ENV is development' do
31
- begin
32
- ENV['RACK_ENV'] = 'development'
33
- get 'http://some-api.doc.granmal.com/some/resource'
34
- last_response.body.should == 'Useless::Doc::Rack::Retriever::Stub'
35
- ensure
36
- ENV['RACK_ENV'] = 'test'
37
- end
38
- end
39
-
40
- it 'should set \'useless.doc.retriever\' to an instance of `Rack::Retriever::Stub` if the RACK_ENV is test' do
41
- get 'http://some-api.doc.granmal.com/some/resource'
42
- last_response.body.should == 'Useless::Doc::Rack::Retriever::Stub'
43
- end
44
-
45
- describe Useless::Doc::Rack::Retriever::Standard do
46
- before(:each) do
47
- Useless::Doc::Rack::Retriever::Standard.instance_variable_set(:@cache, {})
48
- end
49
-
50
- it 'should make a normal request if the cache is empty.' do
51
- Typhoeus.should_receive(:options).
52
- with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json' }).
53
- and_return(mock(:response, response_code: 200, response_body: '{ "some": "json" }'))
54
-
55
- response = Useless::Doc::Rack::Retriever::Standard.retrieve('http://some-api.granmal.com/some/resource')
56
- response.should == '{ "some": "json" }'
57
- end
58
-
59
- it 'should make a request with a cache control header if there is a cache hit.' do
60
- now = Time.now
61
- Time.should_receive(:now).once.and_return(now)
62
-
63
- Typhoeus.should_receive(:options).once.
64
- with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json' }).
65
- and_return(mock(:response, response_code: 200, response_body: '{ "some": "json" }'))
66
-
67
- Typhoeus.should_receive(:options).once.
68
- with('http://some-api.granmal.com/some/resource', headers: { 'Accept' => 'application/json', 'If-Modified-Since' => now.httpdate}).
69
- and_return(mock(:response, response_code: 304, response_body: ''))
70
-
71
- Useless::Doc::Rack::Retriever::Standard.retrieve('http://some-api.granmal.com/some/resource')
72
- response = Useless::Doc::Rack::Retriever::Standard.retrieve('http://some-api.granmal.com/some/resource')
73
- response.should == '{ "some": "json" }'
74
- end
75
- end
76
-
77
- describe Useless::Doc::Rack::Retriever::Stub do
78
- it 'should respond to `retrieve`' do
79
- Useless::Doc::Rack::Retriever::Stub.should respond_to(:retrieve)
80
- end
81
- end
82
- end
@@ -1,57 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
-
3
- require 'rack'
4
- require 'useless/doc/rack/transform'
5
- require 'useless/doc/ui/godel'
6
-
7
- describe Useless::Doc::Rack::Transform do
8
- def api_base
9
- @api_base ||= lambda do |env|
10
- [200, {'Content-Type' => 'application/json'}, [load_document('api.json')]]
11
- end
12
- end
13
-
14
- def resource_base
15
- @resource_base ||= lambda do |env|
16
- [200, {'Content-Type' => 'application/json'}, [load_document('twonk.json')]]
17
- end
18
- end
19
-
20
- def app(base = nil)
21
- lambda do |env|
22
- env['useless.doc.ui'] = Useless::Doc::UI::Godel
23
- Useless::Doc::Rack::Transform.new(base).call(env)
24
- end
25
- end
26
-
27
- it 'should raise an error if a UI is not specified' do
28
- app = Useless::Doc::Rack::Transform.new(resource_base)
29
- request = Rack::MockRequest.new(app)
30
- lambda { request.get('http://some-api.doc.granmal.com/some/resource') }.should raise_error(RuntimeError, 'No UI specified.')
31
- end
32
-
33
- it 'should return a 502 if the returned JSON cannot be parsed' do
34
- bad_base = lambda { |env| [200, {'Content-Type' => 'application/json'}, ['invalid-json']] }
35
- response = Rack::MockRequest.new(app(bad_base)).get('http://some-api.doc.granmal.com/some/resource')
36
- response.status.should == 502
37
- end
38
-
39
- it 'should proxy through if the downstream app returns a non-200 response' do
40
- bad_base = lambda { |env| [404, {'Content-Type' => 'text/plain'}, ['Not Found.']] }
41
- response = Rack::MockRequest.new(app(bad_base)).get('http://some-api.doc.granmal.com/some/resource')
42
- response.status.should == 404
43
- response.body.should == 'Not Found.'
44
- end
45
-
46
- it 'should transform the API JSON returned by the downstream app to HTML, if the request path is root' do
47
- response = Rack::MockRequest.new(app(api_base)).get('http://twonk.useless.io/')
48
- response['Content-Type'].should == 'text/html'
49
- response.body.should =~ /<h1>twonk\.useless\.io<\/h1>/
50
- end
51
-
52
- it 'should transform the resource JSON returned by the downstream app to HTML' do
53
- response = Rack::MockRequest.new(app(resource_base)).get('http://some-api.doc.granmal.com/some/resource')
54
- response['Content-Type'].should == 'text/html'
55
- response.body.should =~ /<h1>\/twonks\/:id<\/h1>/
56
- end
57
- end