useless-doc 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module Useless
2
+ module Doc
3
+ module Core
4
+
5
+ # Documentation for an entire API.
6
+ #
7
+ # @!attribute [r] description
8
+ # @return [String] a description of the API.
9
+ #
10
+ # @!attribute [r] resources
11
+ # @return [Array<Resource>] the resources included in the API.
12
+ #
13
+ class API
14
+
15
+ attr_accessor :url, :description, :resources
16
+
17
+ # @param [Hash] attrs corresponds to the class's instance attributes.
18
+ #
19
+ def initialize(attrs = {})
20
+ @url = attrs[:url]
21
+ @description = attrs[:description]
22
+ @resources = attrs[:resources]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -38,7 +38,7 @@ module Useless
38
38
 
39
39
  request = ::Rack::Request.new(env)
40
40
 
41
- if true or valid_subdomain?(request.url)
41
+ if valid_subdomain?(request.url)
42
42
  url = Proxy.transform_url(request.url)
43
43
 
44
44
  if json = env['useless.doc.retriever'].retrieve(url)
@@ -18,9 +18,24 @@ module Useless
18
18
  hash.is_a?(String) ? hash : Oj.dump(hash)
19
19
  end
20
20
 
21
- # Converts +Doc::Resource+ instance to a JSON representation.
21
+ # Converts +Core::API+ instance to a JSON representation.
22
22
  #
23
- # @param [Doc::Resource] resource the resource to be converted to JSON.
23
+ # @param [Core::API] api the API to be converted to JSON.
24
+ #
25
+ # @return [String] a JSON representation of the specified API.
26
+ #
27
+ def self.api(api)
28
+ if api
29
+ hash_to_json \
30
+ 'url' => api.url,
31
+ 'description' => api.description,
32
+ 'resources' => api.resources.map { |resource| resource(resource) }
33
+ end
34
+ end
35
+
36
+ # Converts a +Core::Resource+ instance to a JSON representation.
37
+ #
38
+ # @param [Core::Resource] resource the resource to be converted to JSON.
24
39
  #
25
40
  # @return [String] a JSON representation of the specified resource.
26
41
  #
@@ -1,5 +1,6 @@
1
1
  require 'oj'
2
2
 
3
+ require 'useless/doc/core/api'
3
4
  require 'useless/doc/core/body'
4
5
  require 'useless/doc/core/header'
5
6
  require 'useless/doc/core/request'
@@ -23,12 +24,33 @@ module Useless
23
24
  json.is_a?(Hash) ? json : Oj.load(json)
24
25
  end
25
26
 
26
- # Converts a JSON represntation to an instance of +Doc::Resource+
27
+ # Converts a JSON represntation to an instance of +Core::API+
28
+ #
29
+ # @param [String, Hash] json the JSON representation to be converted to
30
+ # an API.
31
+ #
32
+ # @return [Core::API] the API corresponding to the specified
33
+ # JSON.
34
+ #
35
+ def self.api(json)
36
+ hash = json_to_hash json
37
+
38
+ resources = (hash['resources'] || []).map do |json|
39
+ resource json
40
+ end
41
+
42
+ Useless::Doc::Core::API.new \
43
+ url: hash['url'],
44
+ description: hash['description'],
45
+ resources: resources
46
+ end
47
+
48
+ # Converts a JSON represntation to an instance of +Core::Resource+
27
49
  #
28
50
  # @param [String, Hash] json the JSON representation to be converted to
29
51
  # a resource.
30
52
  #
31
- # @return [Doc::Resource] the resource corresponding to the specified
53
+ # @return [Core::Resource] the resource corresponding to the specified
32
54
  # JSON.
33
55
  #
34
56
  def self.resource(json)
@@ -1,5 +1,5 @@
1
1
  module Useless
2
2
  module Doc
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -0,0 +1,11 @@
1
+ {
2
+ "url": "twonk.useless.io",
3
+ "description": "Twonk information. Duh.",
4
+ "resources": [
5
+ {
6
+ "path": "/twonks/:id",
7
+ "description": "The most critical aspect.",
8
+ "requests": []
9
+ }
10
+ ]
11
+ }
@@ -1,5 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../../spec_helper'
2
2
 
3
+ require 'useless/doc/core/api'
3
4
  require 'useless/doc/core/body'
4
5
  require 'useless/doc/core/header'
5
6
  require 'useless/doc/core/request'
@@ -21,8 +22,31 @@ describe Useless::Doc::Serialization::Dump do
21
22
  end
22
23
  end
23
24
 
25
+ describe '.api' do
26
+ it 'should convert the specified Core::API instance to JSON' do
27
+ resource = Useless::Doc::Core::Resource.new \
28
+ path: '/twiddles',
29
+ description: 'The full lot of twiddles.',
30
+ requests: []
31
+
32
+ api = Useless::Doc::Core::API.new \
33
+ url: 'twiddles.useless.io',
34
+ description: 'Pretty much, like, everything you\'re looking for',
35
+ resources: [resource]
36
+
37
+ json = Useless::Doc::Serialization::Dump.api(api)
38
+ hash = Useless::Doc::Serialization::Load.json_to_hash(json)
39
+ hash['url'].should == 'twiddles.useless.io'
40
+ hash['description'].should == 'Pretty much, like, everything you\'re looking for'
41
+
42
+ resource_hash = Useless::Doc::Serialization::Load.json_to_hash(hash['resources'][0])
43
+ resource_hash['path'].should == '/twiddles'
44
+ resource_hash['description'].should == 'The full lot of twiddles.'
45
+ end
46
+ end
47
+
24
48
  describe '.resource' do
25
- it 'should convert the specified Doc::Resource instance into JSON' do
49
+ it 'should convert the specified Core::Resource instance into JSON' do
26
50
  get_response_header = Useless::Doc::Core::Header.new \
27
51
  key: 'Type',
28
52
  description: 'The response type.'
@@ -16,6 +16,17 @@ describe Useless::Doc::Serialization::Load do
16
16
  end
17
17
  end
18
18
 
19
+ describe '.api' do
20
+ it 'should parse the specified JSON into an API instance' do
21
+ document = load_document('api.json')
22
+ api = Useless::Doc::Serialization::Load.api document.read
23
+ api.url.should == 'twonk.useless.io'
24
+ api.description.should == 'Twonk information. Duh.'
25
+ api.resources.first.path.should == '/twonks/:id'
26
+ api.resources.first.description.should == 'The most critical aspect.'
27
+ end
28
+ end
29
+
19
30
  describe '.resource' do
20
31
  it 'should parse the specified JSON into a model hierarchy' do
21
32
  document = load_document('twonk.json')
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.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-08 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oj
@@ -152,6 +152,7 @@ files:
152
152
  - README.md
153
153
  - Rakefile
154
154
  - lib/useless/doc.rb
155
+ - lib/useless/doc/core/api.rb
155
156
  - lib/useless/doc/core/body.rb
156
157
  - lib/useless/doc/core/header.rb
157
158
  - lib/useless/doc/core/request.rb
@@ -172,6 +173,7 @@ files:
172
173
  - lib/useless/doc/ui/godel/stylesheet.css
173
174
  - lib/useless/doc/ui/godel/template.mustache
174
175
  - lib/useless/doc/version.rb
176
+ - spec/documents/api.json
175
177
  - spec/documents/twonk.json
176
178
  - spec/spec_helper.rb
177
179
  - spec/useless/doc/dsl_spec.rb
@@ -210,6 +212,7 @@ signing_key:
210
212
  specification_version: 3
211
213
  summary: For parsing and serving Useless documentation.
212
214
  test_files:
215
+ - spec/documents/api.json
213
216
  - spec/documents/twonk.json
214
217
  - spec/spec_helper.rb
215
218
  - spec/useless/doc/dsl_spec.rb