api_sketch 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +31 -24
- data/lib/api_sketch/examples_server.rb +8 -4
- data/lib/api_sketch/generators.rb +39 -29
- data/lib/api_sketch/model.rb +24 -7
- data/lib/api_sketch/renderers.rb +13 -5
- data/lib/api_sketch/runner.rb +1 -2
- data/lib/api_sketch/templates/bootstrap/resource.html.erb +3 -3
- data/lib/api_sketch/version.rb +1 -1
- data/spec/lib/{dsl_spec.rb → api_sketch/dsl_spec.rb} +76 -4
- data/spec/lib/api_sketch/renderers_spec.rb +138 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c2b16335a821956300f19937d9c5b64d4b2685d
|
4
|
+
data.tar.gz: 152e15df7c7030de612807127bc4ff3d3dcfa654
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2a10207ada23e6e82545cf29fa8483b179b29b12960ccf1baf41c5a358476fc2a2e185769ddfb48313aaf19fc624f11138d233cd8b146794ecda998e1b776ae
|
7
|
+
data.tar.gz: 4fa27b22d8c7dbf51ef822378bbba4fc5ed868cb5e3474c341c4a74797e7a64caf120b75951dab3305fdc36714f3bf9c22579833a9a6c1094e0480b30f6c7881
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# 0.1.0 / 2015-05-03
|
2
|
+
|
3
|
+
- Bootstrap template: put all generated pages data into `{output_folder}/docs` directory. Left `assets` directory outside since it may clash with generated files/folders names.
|
4
|
+
- Add endpoint search by conjuction of request `path` & `http_method` for API examples server (Works like normal api server now).
|
5
|
+
- Validate HTTP uniquness of method with path, http method and action unique composition and presence.
|
6
|
+
- Add `api_sketch_response_array_elements_count` parameter for responses server. It generates responses with provided array elements counts. If array contains different type values than each type of these elements would be placed to response multiple times.
|
7
|
+
|
1
8
|
# 0.1.0 / 2015-04-12
|
2
9
|
|
3
10
|
- Add documentation
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Api Sketch
|
2
|
-
------------
|
3
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/api_sketch.svg)](http://badge.fury.io/rb/api_sketch)
|
4
|
+
|
5
|
+
---
|
4
6
|
api_sketch gem provides you with DSL to describe and create API documentation.
|
5
7
|
|
6
8
|
It consists of three main parts:
|
@@ -63,31 +65,40 @@ $ api_sketch -s -i definitions
|
|
63
65
|
After this server was started response example may be accessed with this kind url:
|
64
66
|
|
65
67
|
```
|
66
|
-
http://localhost:3127/.json?api_sketch_resource_id=users/update&api_sketch_response_context=Success
|
68
|
+
http://localhost:3127/.json?api_sketch_resource_id=users/update&api_sketch_response_context=Success&api_sketch_response_array_elements_count=1
|
67
69
|
```
|
68
70
|
|
69
71
|
```json
|
70
|
-
{
|
71
|
-
"user":{
|
72
|
-
"id":345,
|
73
|
-
"email":"user44@email.com",
|
74
|
-
"first_name":"First name 20",
|
75
|
-
"last_name":"Last name 96",
|
76
|
-
"country":{
|
77
|
-
"name":"Ukraine",
|
78
|
-
"id":"UA"
|
79
|
-
},
|
80
|
-
"authentications":[
|
81
|
-
{
|
82
|
-
"uid":"6668-5565-3835-6085-8727",
|
83
|
-
"provider":"facebook"
|
84
|
-
}
|
85
|
-
]
|
86
|
-
}
|
72
|
+
{
|
73
|
+
"user":{
|
74
|
+
"id":345,
|
75
|
+
"email":"user44@email.com",
|
76
|
+
"first_name":"First name 20",
|
77
|
+
"last_name":"Last name 96",
|
78
|
+
"country":{
|
79
|
+
"name":"Ukraine",
|
80
|
+
"id":"UA"
|
81
|
+
},
|
82
|
+
"authentications":[
|
83
|
+
{
|
84
|
+
"uid":"6668-5565-3835-6085-8727",
|
85
|
+
"provider":"facebook"
|
86
|
+
}
|
87
|
+
]
|
88
|
+
}
|
87
89
|
}
|
88
90
|
```
|
89
91
|
|
90
|
-
`api_sketch_resource_id` and `api_sketch_response_context` parameters are used to determine which response should be returned.
|
92
|
+
`api_sketch_resource_id` and `api_sketch_response_context` parameters are used to determine which response should be returned. If `api_sketch_response_context` is omitted than it would take first available response.
|
93
|
+
|
94
|
+
`api_sketch_response_array_elements_count` - optional parameter that determines amount of elements in generated response arrays. Default value is 3.
|
95
|
+
|
96
|
+
Also server supports finding responses by http request method and path.
|
97
|
+
|
98
|
+
```
|
99
|
+
curl -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{ "api_sketch_response_context" : "Success", "api_sketch_response_array_elements_count" : 3}' http://localhost:3127/api/users/me.json
|
100
|
+
{"user":{"id":509,"email":"user63@email.com", ... }}
|
101
|
+
```
|
91
102
|
|
92
103
|
Definitions
|
93
104
|
-----------
|
@@ -368,17 +379,13 @@ TODO
|
|
368
379
|
- Add API index page similar to [Foursquare API docs](https://developer.foursquare.com/docs/)
|
369
380
|
- Improve documentation, add more examples
|
370
381
|
- Add more documentation templates. For example PDF, curl, some other html styles, etc. It should be configurable as api_sketch command line option. This might be made as some separate extensions gem. There also could be generators for some specific framework api controllers structure scaffold generators.
|
371
|
-
- Put all generated pages data into `{output_folder}/docs` directory. Left `assets` directory outside since it may clash with generated files/folders names.
|
372
|
-
- API examples server also should support endpoint search by request `path` & `http_method` like normal api server does.
|
373
382
|
- Deal with query body at responses (For example redirects may have query body)
|
374
|
-
- Validate HTTP method with path, http method and action unique composition
|
375
383
|
- Add other request/response types like plaintext, xml, etc (should be supprted both at generator and server)
|
376
384
|
- Add realtime viewable page with log for this api examples server application to let client side developers see what data they have sent and how server received it
|
377
385
|
- Add more validations to models.
|
378
386
|
- Add more specs and tests.
|
379
387
|
- Add `shared_block "shared block name"` (definition keyword) search keyword by it's to blocks. Maybe `uses_shared_block "shared block name"`. Maybe shared blocks should be placed into special directory at definitions to be loaded before all examples
|
380
388
|
- Add more complex example values autogeneration for API examples server. Derive values from key names. For example string "email" should have some email value as response example.
|
381
|
-
- Add `api_sketch_response_array_elements_count` for responses server. It should generate responses with provided array elements counts. If array contains different type values than each type of these elements should be placed to response multiple times.
|
382
389
|
- rDoc documentation for code.
|
383
390
|
|
384
391
|
Contributing
|
@@ -24,10 +24,11 @@ class ApiSketch::ExamplesServer
|
|
24
24
|
response['Content-Type'] = 'application/json'
|
25
25
|
|
26
26
|
response.status = Rack::Utils.status_code(api_response.http_status)
|
27
|
-
|
27
|
+
|
28
|
+
response.write(ApiSketch::ResponseRenderer.new(api_response.parameters.body, api_response.parameters.body_container_type, get_elements_count).to_json)
|
28
29
|
end
|
29
30
|
else
|
30
|
-
api_sketch_message("No any responses defined for this resource", 404)
|
31
|
+
api_sketch_message("No any responses defined for this resource and context", 404)
|
31
32
|
end
|
32
33
|
else
|
33
34
|
api_sketch_message("Resource is not Found", 404)
|
@@ -39,7 +40,7 @@ class ApiSketch::ExamplesServer
|
|
39
40
|
@api_resource = if @request.params["api_sketch_resource_id"]
|
40
41
|
ApiSketch::Model::Resource.find(@request.params["api_sketch_resource_id"])
|
41
42
|
else
|
42
|
-
ApiSketch::Model::Resource.
|
43
|
+
ApiSketch::Model::Resource.find_by_http_method_and_path(@request.request_method, @request.path)
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
@@ -51,4 +52,7 @@ class ApiSketch::ExamplesServer
|
|
51
52
|
Rack::Response.new({"api_sketch" => message}.to_json, 404)
|
52
53
|
end
|
53
54
|
|
54
|
-
|
55
|
+
def get_elements_count
|
56
|
+
@request.params["api_sketch_response_array_elements_count"].to_i
|
57
|
+
end
|
58
|
+
end
|
@@ -1,48 +1,52 @@
|
|
1
1
|
module ApiSketch::Generators
|
2
2
|
|
3
|
-
|
3
|
+
class Base
|
4
4
|
|
5
|
-
|
5
|
+
attr_accessor :definitions_dir, :documentation_dir
|
6
6
|
|
7
|
-
|
7
|
+
attr_reader :templates_folder
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
# TODO: Add here some validations for folders existance, etc
|
10
|
+
def initialize(options = {})
|
11
|
+
self.definitions_dir = options[:definitions_dir]
|
12
|
+
self.documentation_dir = options[:documentation_dir]
|
13
|
+
@templates_folder = File.expand_path("templates/#{self.class.name.split("::").last.downcase}", File.dirname(__FILE__))
|
14
|
+
end
|
15
15
|
|
16
|
-
|
16
|
+
def generate!
|
17
17
|
puts_info("Load definitions")
|
18
|
-
|
18
|
+
load_definitions
|
19
19
|
puts_info("Create documentation directory")
|
20
20
|
puts_info("\t path: #{self.documentation_dir}")
|
21
|
-
|
21
|
+
create_documentation_directory
|
22
22
|
puts_info("Create documentation files")
|
23
|
-
|
24
|
-
|
23
|
+
create_documentation_files
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
private
|
27
|
+
def create_documentation_directory
|
28
28
|
FileUtils.rm_r(self.documentation_dir, :force => true)
|
29
|
-
|
30
|
-
|
29
|
+
FileUtils.mkdir_p(self.documentation_dir)
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
# TODO: This is unfinished sample file generator it should be more complex at some other generators
|
33
|
+
# Other generors should inherit from this class and implement this method
|
34
|
+
def create_documentation_files
|
35
|
+
raise "This method should be implemented at child class who inherits from ApiSketch::Generators::Base"
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
def load_definitions
|
39
39
|
ApiSketch::Model::Resource.reload!(self.definitions_dir)
|
40
|
-
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
41
43
|
|
42
|
-
end
|
43
44
|
|
45
|
+
class Bootstrap < ApiSketch::Generators::Base
|
44
46
|
|
45
|
-
|
47
|
+
# Generated folders structure is
|
48
|
+
# docs - html folders and files
|
49
|
+
# assets - js, css and images. Html styling
|
46
50
|
|
47
51
|
def initialize(options = {})
|
48
52
|
super(options)
|
@@ -62,6 +66,12 @@ module ApiSketch::Generators
|
|
62
66
|
FileUtils.copy_entry(source, target)
|
63
67
|
end
|
64
68
|
|
69
|
+
def docs_folder
|
70
|
+
# Left assets directory outside since it may clash with generated files/folders names.
|
71
|
+
# Put generated html pages to 'docs' folder
|
72
|
+
"#{self.documentation_dir}/docs"
|
73
|
+
end
|
74
|
+
|
65
75
|
def create_documentation_files
|
66
76
|
@generator = self
|
67
77
|
copy_assets
|
@@ -69,7 +79,7 @@ module ApiSketch::Generators
|
|
69
79
|
@resources.each do |resource|
|
70
80
|
@resource = resource
|
71
81
|
|
72
|
-
filename = File.join(
|
82
|
+
filename = File.join(docs_folder, filename_for(@resource))
|
73
83
|
html_data = @resource_template.result(binding)
|
74
84
|
|
75
85
|
dir = File.dirname(filename)
|
@@ -84,4 +94,4 @@ module ApiSketch::Generators
|
|
84
94
|
end
|
85
95
|
end
|
86
96
|
|
87
|
-
end
|
97
|
+
end
|
data/lib/api_sketch/model.rb
CHANGED
@@ -155,6 +155,10 @@ module ApiSketch::Model
|
|
155
155
|
self.all.find { |res| res.id == id }
|
156
156
|
end
|
157
157
|
|
158
|
+
def find_by_http_method_and_path(http_method, path)
|
159
|
+
self.all.find { |res| res.http_method == http_method && res.path == path }
|
160
|
+
end
|
161
|
+
|
158
162
|
def first
|
159
163
|
self.all.first
|
160
164
|
end
|
@@ -180,17 +184,30 @@ module ApiSketch::Model
|
|
180
184
|
}
|
181
185
|
end
|
182
186
|
|
187
|
+
def error_message(message)
|
188
|
+
# puts_error(message)
|
189
|
+
raise ::ApiSketch::Error, message
|
190
|
+
end
|
191
|
+
|
183
192
|
def run_validations!
|
184
193
|
unless self.action =~ /\A\w*\z/
|
185
|
-
|
186
|
-
# puts_error(message)
|
187
|
-
raise ::ApiSketch::Error, message
|
194
|
+
error_message("'#{self.action}' is invalid action value")
|
188
195
|
end
|
189
196
|
|
190
197
|
if self.class.find(self.id)
|
191
|
-
|
192
|
-
|
193
|
-
|
198
|
+
error_message("'#{self.id}' is not unique id. Change values of 'namespace' and/or 'action' attributes")
|
199
|
+
end
|
200
|
+
|
201
|
+
if self.http_method.nil? || self.http_method.empty?
|
202
|
+
error_message("request http_method can't be blank")
|
203
|
+
end
|
204
|
+
|
205
|
+
if self.path.nil? || self.path.empty?
|
206
|
+
error_message("request path can't be blank")
|
207
|
+
end
|
208
|
+
|
209
|
+
if self.class.find_by_http_method_and_path(self.http_method, self.path)
|
210
|
+
error_message("Route '#{self.http_method} #{self.path}' should be unique")
|
194
211
|
end
|
195
212
|
end
|
196
213
|
|
@@ -224,4 +241,4 @@ module ApiSketch::Model
|
|
224
241
|
end
|
225
242
|
end
|
226
243
|
|
227
|
-
end
|
244
|
+
end
|
data/lib/api_sketch/renderers.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
class ApiSketch::ResponseRenderer
|
2
2
|
|
3
|
-
attr_reader :params, :container_type
|
3
|
+
attr_reader :params, :container_type, :elements_count
|
4
4
|
|
5
|
-
def initialize(params, container_type)
|
6
|
-
@params = params
|
5
|
+
def initialize(params, container_type, elements_count)
|
6
|
+
@params = params || {}
|
7
7
|
@container_type = container_type
|
8
|
+
@elements_count = elements_count > 0 ? elements_count : 3
|
8
9
|
end
|
9
10
|
|
10
11
|
def to_h
|
@@ -28,7 +29,10 @@ class ApiSketch::ResponseRenderer
|
|
28
29
|
end
|
29
30
|
|
30
31
|
items.each do |param, index|
|
31
|
-
value =
|
32
|
+
value = if param.data_type == :array && param.content
|
33
|
+
# Some crazy tricks to get 'elements_count' random elements
|
34
|
+
elements_count.times.inject([]) { |a, _| a += render_content(param.content, param.data_type) }
|
35
|
+
elsif param.data_type == :document && param.content
|
32
36
|
render_content(param.content, param.data_type)
|
33
37
|
else
|
34
38
|
param.example_value(true)
|
@@ -36,7 +40,11 @@ class ApiSketch::ResponseRenderer
|
|
36
40
|
|
37
41
|
case placeholder_type
|
38
42
|
when :array
|
39
|
-
|
43
|
+
if param.data_type == :array && param.content
|
44
|
+
placeholder += value
|
45
|
+
else
|
46
|
+
placeholder << value
|
47
|
+
end
|
40
48
|
when :document
|
41
49
|
placeholder[param.name] = value
|
42
50
|
end
|
data/lib/api_sketch/runner.rb
CHANGED
@@ -67,7 +67,6 @@ class ApiSketch::Runner
|
|
67
67
|
if config[:generate] || config[:examples_server]
|
68
68
|
raise ApiSketch::Error, "Definitions parameter should be a directory" unless File.directory?(config[:definitions_dir])
|
69
69
|
config[:project_name] = File.basename(config[:definitions_dir]).gsub("_", " ").gsub(/\w+/, &:capitalize) if (config[:project_name].nil? || config[:project_name].empty?)
|
70
|
-
puts config[:project_name]
|
71
70
|
end
|
72
71
|
|
73
72
|
::ApiSketch::Config.merge!(config)
|
@@ -89,4 +88,4 @@ class ApiSketch::Runner
|
|
89
88
|
end
|
90
89
|
end
|
91
90
|
|
92
|
-
end
|
91
|
+
end
|
@@ -45,7 +45,7 @@
|
|
45
45
|
|
46
46
|
<%
|
47
47
|
# Relative reference urls nesting
|
48
|
-
nesting_path_prefix = "
|
48
|
+
nesting_path_prefix = "./../" + "../" * File.dirname(@generator.filename_for(resource)).split("/").count
|
49
49
|
%>
|
50
50
|
|
51
51
|
<!DOCTYPE html>
|
@@ -58,7 +58,7 @@
|
|
58
58
|
<meta name="author" content="">
|
59
59
|
<link rel="icon" href="<%= nesting_path_prefix %>assets/images/favicon.ico">
|
60
60
|
|
61
|
-
<title
|
61
|
+
<title><%= ApiSketch::Config[:project_name] %> API documentation</title>
|
62
62
|
|
63
63
|
<!-- Bootstrap core CSS -->
|
64
64
|
<link href="<%= nesting_path_prefix %>assets/stylesheets/bootstrap.min.css" rel="stylesheet">
|
@@ -95,7 +95,7 @@
|
|
95
95
|
<div class="col-sm-3 col-md-2 sidebar">
|
96
96
|
<ul class="nav nav-sidebar">
|
97
97
|
<% @resources.each do |resource| %>
|
98
|
-
<li class="<%= 'active' if resource.id == @resource.id %>"><a href="<%= nesting_path_prefix
|
98
|
+
<li class="<%= 'active' if resource.id == @resource.id %>"><a href="<%= nesting_path_prefix %>docs/<%= @generator.filename_for(resource) %>"><%= resource.name %></a></li>
|
99
99
|
<% end %>
|
100
100
|
</ul>
|
101
101
|
</div>
|
data/lib/api_sketch/version.rb
CHANGED
@@ -2,7 +2,7 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe ApiSketch::DSL do
|
4
4
|
context "document" do
|
5
|
-
|
5
|
+
context "when all data has key names" do
|
6
6
|
before do
|
7
7
|
@block = lambda do
|
8
8
|
document do
|
@@ -14,7 +14,7 @@ describe ApiSketch::DSL do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
it "should successfully create objects" do
|
18
18
|
attributes = ApiSketch::DSL::Attributes.new(:array, &@block).to_a
|
19
19
|
attribute = attributes.first
|
20
20
|
expect(attribute.data_type).to eql :document
|
@@ -22,7 +22,7 @@ describe ApiSketch::DSL do
|
|
22
22
|
expect(string_key.data_type).to eql :string
|
23
23
|
expect(string_key.name).to eql "test_key"
|
24
24
|
end
|
25
|
-
|
25
|
+
end
|
26
26
|
|
27
27
|
context "when data doesn't have key name" do
|
28
28
|
before do
|
@@ -90,6 +90,78 @@ describe ApiSketch::DSL do
|
|
90
90
|
end
|
91
91
|
|
92
92
|
context "incorrect data" do
|
93
|
+
context "request HTTP method presence validation" do
|
94
|
+
before do
|
95
|
+
@invalid_block = Proc.new do
|
96
|
+
resource "API endpoint name" do
|
97
|
+
action "show"
|
98
|
+
namespace "endpoints"
|
99
|
+
path "/api/endpoint/link.json"
|
100
|
+
http_method ""
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should return error" do
|
106
|
+
expect { ApiSketch::DSL.new.instance_eval(&@invalid_block) }.to raise_error(::ApiSketch::Error, "request http_method can't be blank")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "request path method presence validation" do
|
111
|
+
before do
|
112
|
+
@invalid_block = Proc.new do
|
113
|
+
resource "API endpoint name" do
|
114
|
+
action "show"
|
115
|
+
namespace "endpoints"
|
116
|
+
http_method "GET"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should return error" do
|
122
|
+
expect { ApiSketch::DSL.new.instance_eval(&@invalid_block) }.to raise_error(::ApiSketch::Error, "request path can't be blank")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "Combination of http_method and path is not unique" do
|
127
|
+
before do
|
128
|
+
@block = Proc.new do
|
129
|
+
resource "API endpoint name" do
|
130
|
+
action "show"
|
131
|
+
namespace "endpoints"
|
132
|
+
path "/api/endpoint/link.json"
|
133
|
+
http_method "GET"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
@other_block = Proc.new do
|
138
|
+
resource "Create API endpoint name" do
|
139
|
+
action "create"
|
140
|
+
namespace "endpoints"
|
141
|
+
path "/api/endpoint/link.json"
|
142
|
+
http_method "POST"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
@invalid_block = Proc.new do
|
148
|
+
resource "API some other endpoint name" do
|
149
|
+
action "other_show"
|
150
|
+
namespace "endpoints"
|
151
|
+
path "/api/endpoint/link.json"
|
152
|
+
http_method "GET"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should return error" do
|
158
|
+
ApiSketch::DSL.new.instance_eval(&@block)
|
159
|
+
ApiSketch::DSL.new.instance_eval(&@other_block)
|
160
|
+
expect { ApiSketch::DSL.new.instance_eval(&@invalid_block) }.to raise_error(::ApiSketch::Error, "Route 'GET /api/endpoint/link.json' should be unique")
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
93
165
|
context "empty key name at root document" do
|
94
166
|
before do
|
95
167
|
@block = Proc.new do
|
@@ -471,4 +543,4 @@ describe ApiSketch::DSL do
|
|
471
543
|
|
472
544
|
end
|
473
545
|
end
|
474
|
-
end
|
546
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ApiSketch::ResponseRenderer do
|
4
|
+
|
5
|
+
before do
|
6
|
+
# Ensure that we have empty resources set for each test
|
7
|
+
ApiSketch::Model::Resource.reset!
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:simple_resource) {
|
11
|
+
Proc.new do
|
12
|
+
resource "Simple data" do
|
13
|
+
action "show"
|
14
|
+
namespace "simple_data"
|
15
|
+
path "/api/test_data.json"
|
16
|
+
http_method "GET"
|
17
|
+
format "json"
|
18
|
+
|
19
|
+
responses do
|
20
|
+
context "Success" do
|
21
|
+
http_status :ok # 200
|
22
|
+
|
23
|
+
parameters do
|
24
|
+
body :document do
|
25
|
+
string "key" do
|
26
|
+
example { "value" }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
}
|
35
|
+
|
36
|
+
let(:resource_example_definition) {
|
37
|
+
Proc.new do
|
38
|
+
resource "Get test data" do
|
39
|
+
action "index"
|
40
|
+
namespace "test_endpoints"
|
41
|
+
path "/api/test_data.json"
|
42
|
+
http_method "GET"
|
43
|
+
format "json"
|
44
|
+
|
45
|
+
responses do
|
46
|
+
context "Success" do
|
47
|
+
http_status :ok # 200
|
48
|
+
|
49
|
+
parameters do
|
50
|
+
body :document do
|
51
|
+
document "user" do
|
52
|
+
content do
|
53
|
+
integer "id" do
|
54
|
+
description "User's ID"
|
55
|
+
example { 123 }
|
56
|
+
end
|
57
|
+
string "email" do
|
58
|
+
description "user's email value"
|
59
|
+
example { "user_test@email.com" }
|
60
|
+
end
|
61
|
+
|
62
|
+
array "same_data" do
|
63
|
+
content do
|
64
|
+
integer do
|
65
|
+
example { 42 }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
array "values" do
|
71
|
+
content do
|
72
|
+
string do
|
73
|
+
example { "Pizza" }
|
74
|
+
end
|
75
|
+
integer do
|
76
|
+
example { 100500 }
|
77
|
+
end
|
78
|
+
document do
|
79
|
+
content do
|
80
|
+
string "key" do
|
81
|
+
example { "Text" }
|
82
|
+
end
|
83
|
+
timestamp "time" do
|
84
|
+
example { 1430761823 }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
}
|
99
|
+
|
100
|
+
context "instance methods" do
|
101
|
+
|
102
|
+
describe "#to_h" do
|
103
|
+
it "should render data properly" do
|
104
|
+
ApiSketch::DSL.new.instance_eval(&resource_example_definition)
|
105
|
+
response = ApiSketch::Model::Resource.find("test_endpoints/index").responses.find { |rsp| rsp.name == "Success" }
|
106
|
+
result = ApiSketch::ResponseRenderer.new(response.parameters.body, response.parameters.body_container_type, 3).to_h
|
107
|
+
|
108
|
+
expected_hash = {
|
109
|
+
"user" =>
|
110
|
+
{
|
111
|
+
"id" => 123,
|
112
|
+
"email" => "user_test@email.com",
|
113
|
+
"same_data" => [42, 42, 42],
|
114
|
+
"values" =>
|
115
|
+
["Pizza", 100500, {"key"=>"Text", "time"=>1430761823},
|
116
|
+
"Pizza", 100500, {"key"=>"Text", "time"=>1430761823},
|
117
|
+
"Pizza", 100500, {"key"=>"Text", "time"=>1430761823}]
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
expect(result).to eql expected_hash
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
describe "#to_json" do
|
127
|
+
it "should return proper json" do
|
128
|
+
ApiSketch::DSL.new.instance_eval(&simple_resource)
|
129
|
+
response = ApiSketch::Model::Resource.find("simple_data/show").responses.find { |rsp| rsp.name == "Success" }
|
130
|
+
result = ApiSketch::ResponseRenderer.new(response.parameters.body, response.parameters.body_container_type, 1).to_json
|
131
|
+
|
132
|
+
expected_json = {"key" => "value"}.to_json
|
133
|
+
expect(result).to eql expected_json
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_sketch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey Suhoviy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mixlib-cli
|
@@ -156,7 +156,8 @@ files:
|
|
156
156
|
- lib/api_sketch/templates/bootstrap/assets/stylesheets/dashboard.css
|
157
157
|
- lib/api_sketch/templates/bootstrap/resource.html.erb
|
158
158
|
- lib/api_sketch/version.rb
|
159
|
-
- spec/lib/dsl_spec.rb
|
159
|
+
- spec/lib/api_sketch/dsl_spec.rb
|
160
|
+
- spec/lib/api_sketch/renderers_spec.rb
|
160
161
|
- spec/spec_helper.rb
|
161
162
|
homepage: https://github.com/suhovius/api_sketch
|
162
163
|
licenses:
|
@@ -183,5 +184,6 @@ signing_key:
|
|
183
184
|
specification_version: 4
|
184
185
|
summary: API Prototyping and API Documentation Tool
|
185
186
|
test_files:
|
186
|
-
- spec/lib/dsl_spec.rb
|
187
|
+
- spec/lib/api_sketch/dsl_spec.rb
|
188
|
+
- spec/lib/api_sketch/renderers_spec.rb
|
187
189
|
- spec/spec_helper.rb
|