api_docs 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,7 +30,7 @@ Documentation is automatically generated into yaml files from tests you create t
30
30
  class UsersTest < ActionDispatch::IntegrationTest
31
31
  def test_get_user
32
32
  user = users(:default)
33
- api_call(:get, '/users/:id', :id => user.to_param) do |doc|
33
+ api_call(:get, '/users/:id', :id => user.to_param, :format => 'json') do |doc|
34
34
  assert_response :success
35
35
  assert_equal ({
36
36
  'id' => user.id,
@@ -40,7 +40,7 @@ class UsersTest < ActionDispatch::IntegrationTest
40
40
  end
41
41
 
42
42
  def test_get_user_failure
43
- api_call(:get, '/users/:id', :id => 'invalid') do |doc|
43
+ api_call(:get, '/users/:id', :id => 'invalid', :format => 'json') do |doc|
44
44
  doc.description = 'When bad user id is passed'
45
45
  assert_response :not_found
46
46
  assert_equal ({
@@ -60,6 +60,7 @@ show:
60
60
  path: /users/:id
61
61
  params:
62
62
  id: 12345
63
+ format: json
63
64
  status: 200
64
65
  body:
65
66
  id: 12345
@@ -70,6 +71,7 @@ show:
70
71
  path: /users/:id
71
72
  params:
72
73
  id: invalid
74
+ format: json
73
75
  status: 404
74
76
  body:
75
77
  message: User not found
@@ -101,4 +103,4 @@ end
101
103
 
102
104
  ---
103
105
 
104
- Copyright 2012 Oleg Khabarov, Jack Neto, [The Working Group, Inc](http://twg.ca)
106
+ Copyright 2012 Oleg Khabarov, Jack Neto, [The Working Group, Inc](http://twg.ca)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
data/api_docs.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "api_docs"
8
- s.version = "1.0.4"
8
+ s.version = "1.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oleg Khabarov", "Jack Neto", "The Working Group Inc."]
12
- s.date = "2012-10-12"
12
+ s.date = "2013-02-28"
13
13
  s.description = "Generate API documentation using integration tests in Ruby on Rails 3"
14
14
  s.email = "jack@twg.ca"
15
15
  s.extra_rdoc_files = [
@@ -35,7 +35,9 @@
35
35
 
36
36
  <tr>
37
37
  <th>Body</th>
38
- <td><pre><%= JSON.pretty_generate(action['body']) %></pre></td>
38
+ <td>
39
+ <pre><%= JSON.pretty_generate(JSON.parse(action['body'])) rescue action['body']%></pre>
40
+ </td>
39
41
  </tr>
40
42
 
41
43
  </table>
@@ -6,7 +6,7 @@ module ApiDocs::TestHelper
6
6
  # doc.description = 'Something for the docs'
7
7
  # ... regular test code
8
8
  # end
9
- def api_call(method, path, params = { }, &block)
9
+ def api_call(method, path, params = { })
10
10
  parsed_params = params.dup
11
11
  parsed_path = path.dup
12
12
 
@@ -18,7 +18,8 @@ module ApiDocs::TestHelper
18
18
  # get '/users/12345'
19
19
  doc = OpenStruct.new
20
20
  send(method, parsed_path, parsed_params)
21
- yield doc
21
+
22
+ yield doc if block_given?
22
23
 
23
24
  # Assertions inside test block didn't fail. Preparing file
24
25
  # content to be written
@@ -27,11 +28,10 @@ module ApiDocs::TestHelper
27
28
 
28
29
  file_path = File.expand_path("#{c.gsub('/', ':')}.yml", ApiDocs.config.docs_path)
29
30
  params = api_deep_clean_params(params)
30
- body = JSON.parse(response.body)
31
31
 
32
32
  # Marking response as an unique
33
33
  key = 'ID-' + Digest::MD5.hexdigest("
34
- #{method}#{path}#{params}#{response.status}#{api_deep_clean_params(body, :as_response)}
34
+ #{method}#{path}#{doc.description}#{params}#{response.status}}
35
35
  ")
36
36
 
37
37
  data = if File.exists?(file_path)
@@ -47,7 +47,7 @@ module ApiDocs::TestHelper
47
47
  'path' => path,
48
48
  'params' => api_deep_clean_params(params),
49
49
  'status' => response.status,
50
- 'body' => body
50
+ 'body' => response.body
51
51
  }
52
52
  FileUtils.mkdir_p(File.dirname(file_path))
53
53
  File.open(file_path, 'w'){|f| f.write(data.to_yaml)}
@@ -55,18 +55,14 @@ module ApiDocs::TestHelper
55
55
 
56
56
  # Cleans up params. Removes things like File object handlers
57
57
  # Sets up ignored values so we don't generate new keys for same data
58
- def api_deep_clean_params(params, as_response = false)
58
+ def api_deep_clean_params(params)
59
59
  case params
60
60
  when Hash
61
61
  params.each_with_object({}) do |(key, value), res|
62
- if as_response && ApiDocs.config.ignored_attributes.include?(key.to_s)
63
- res[key.to_s] = 'IGNORED'
64
- else
65
- res[key.to_s] = api_deep_clean_params(value, as_response)
66
- end
62
+ res[key.to_s] = api_deep_clean_params(value)
67
63
  end
68
64
  when Array
69
- params.collect{|value| api_deep_clean_params(value, as_response)}
65
+ params.collect{|value| api_deep_clean_params(value)}
70
66
  else
71
67
  case params
72
68
  when Rack::Test::UploadedFile
@@ -2,11 +2,22 @@ class ApplicationController < ActionController::Base
2
2
  protect_from_forgery
3
3
 
4
4
  def index
5
- render :text => [{
6
- :id => 1,
7
- :name => 'Test User',
8
- :created_at => 1.day.ago
9
- }].to_json
5
+ respond_to do |format|
6
+ format.json {
7
+ render :text => [{
8
+ :id => 1,
9
+ :name => 'Test User',
10
+ :created_at => 1.day.ago
11
+ }].to_json
12
+ }
13
+ format.xml {
14
+ render :text => [{
15
+ :id => 1,
16
+ :name => 'Test User',
17
+ :created_at => 1.day.ago
18
+ }].to_xml(:root => 'users')
19
+ }
20
+ end
10
21
  end
11
22
 
12
23
  def show
@@ -20,6 +31,14 @@ class ApplicationController < ActionController::Base
20
31
  end
21
32
 
22
33
  response.merge!(:created_at => rand.days.ago) if params[:random]
23
- render :text => response.to_json, :status => status
34
+
35
+ respond_to do |format|
36
+ format.json do
37
+ render :text => response.to_json, :status => status
38
+ end
39
+ format.xml do
40
+ render :text => response.to_xml(:root => 'user'), :status => status
41
+ end
42
+ end
24
43
  end
25
44
  end
@@ -28,17 +28,8 @@ class TestHelperTest < ActionDispatch::IntegrationTest
28
28
  api_deep_clean_params({:a => Rack::Test::UploadedFile.new(__FILE__)})
29
29
  end
30
30
 
31
- def test_api_deep_clean_params_with_ignored_params
32
- ApiDocs.config.ignored_attributes = ['c']
33
- assert_equal ({'c' => 'IGNORED'}),
34
- api_deep_clean_params({:c => 'c'}, :as_response)
35
-
36
- assert_equal ({'a' => [{'b' => 'c', 'c' => 'IGNORED'}]}),
37
- api_deep_clean_params({:a => [:b => 'c', :c => 'd']}, :as_response)
38
- end
39
-
40
31
  def test_api_call
41
- api_call(:get, '/users/:id', :id => 12345) do |doc|
32
+ api_call(:get, '/users/:id', :id => 12345, :format => 'json') do |doc|
42
33
  assert_response :success
43
34
  assert_equal ({
44
35
  'id' => 12345,
@@ -46,7 +37,7 @@ class TestHelperTest < ActionDispatch::IntegrationTest
46
37
  }), JSON.parse(response.body)
47
38
  end
48
39
 
49
- api_call(:get, '/users/:id', :id => 'invalid') do |doc|
40
+ api_call(:get, '/users/:id', :id => 'invalid', :format => 'json') do |doc|
50
41
  doc.description = 'Invalid user id'
51
42
  assert_response :not_found
52
43
  assert_equal ({
@@ -68,27 +59,27 @@ class TestHelperTest < ActionDispatch::IntegrationTest
68
59
  object = output['show'][output['show'].keys.first]
69
60
  assert_equal 'GET', object['method']
70
61
  assert_equal '/users/:id', object['path']
71
- assert_equal ({'id' => '12345'}), object['params']
62
+ assert_equal ({'id' => '12345', 'format' => 'json'}), object['params']
72
63
  assert_equal 200, object['status']
73
- assert_equal ({'id' => 12345, 'name' => 'Test User'}), object['body']
64
+ assert_equal ({'id' => 12345, 'name' => 'Test User'}), JSON.parse(object['body'])
74
65
 
75
66
  object = output['show'][output['show'].keys.last]
76
- assert_equal 'GET', object['method']
77
- assert_equal '/users/:id', object['path']
78
- assert_equal ({'id' => 'invalid'}), object['params']
79
- assert_equal 404, object['status']
80
- assert_equal ({'message' => 'User not found'}), object['body']
67
+ assert_equal 'GET', object['method']
68
+ assert_equal '/users/:id', object['path']
69
+ assert_equal ({'id' => 'invalid', 'format' => 'json'}), object['params']
70
+ assert_equal 404, object['status']
71
+ assert_equal ({'message' => 'User not found'}), JSON.parse(object['body'])
81
72
  end
82
73
 
83
74
  def test_api_call_with_ignored_attribute
84
- api_call(:get, '/users/:id', :id => 12345) do
75
+ api_call(:get, '/users/:id', :id => 12345, :format => 'json') do
85
76
  assert_response :success
86
77
  end
87
78
  output = YAML.load_file(ApiDocs.config.docs_path.join('application.yml'))
88
79
  assert_equal 1, output['show'].keys.size
89
80
 
90
81
  ApiDocs.config.ignored_attributes << 'random'
91
- api_call(:get, '/users/:id', :id => 12345, :random => 1) do
82
+ api_call(:get, '/users/:id', :id => 12345, :random => 1, :format => 'json') do
92
83
  assert_response :success
93
84
  end
94
85
  output = YAML.load_file(ApiDocs.config.docs_path.join('application.yml'))
@@ -96,11 +87,27 @@ class TestHelperTest < ActionDispatch::IntegrationTest
96
87
  object = output['show'][output['show'].keys.last]
97
88
  assert_not_equal 'IGNORED', object['body']['random']
98
89
 
99
- api_call(:get, '/users/:id', :id => 12345, :random => 1) do
90
+ api_call(:get, '/users/:id', :id => 12345, :random => 1, :format => 'json') do
100
91
  assert_response :success
101
92
  end
102
93
  output = YAML.load_file(ApiDocs.config.docs_path.join('application.yml'))
103
94
  assert_equal 2, output['show'].keys.size
104
95
  end
96
+
97
+ def test_api_call_with_xml
98
+ api_call(:get, '/users/:id', :id => 12345, :format => 'xml') do
99
+ assert_response :success
100
+ xml_response = <<-eoxml
101
+ <?xml version="1.0" encoding="UTF-8"?>
102
+ <user>
103
+ <id type="integer">12345</id>
104
+ <name>Test User</name>
105
+ </user>
106
+ eoxml
107
+ assert_equal xml_response, response.body
108
+ end
109
+ output = YAML.load_file(ApiDocs.config.docs_path.join('application.yml'))
110
+ assert_equal 1, output['show'].keys.size
111
+ end
105
112
 
106
113
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_docs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-10-12 00:00:00.000000000 Z
14
+ date: 2013-02-28 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: 4166833951884593024
146
+ hash: -620307920657419595
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: