betterdocs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +5 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +9 -0
  6. data/COPYING +202 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE +202 -0
  9. data/README.md +124 -0
  10. data/Rakefile +25 -0
  11. data/VERSION +1 -0
  12. data/betterdocs.gemspec +48 -0
  13. data/lib/betterdocs.rb +27 -0
  14. data/lib/betterdocs/controller_collector.rb +50 -0
  15. data/lib/betterdocs/dsl.rb +9 -0
  16. data/lib/betterdocs/dsl/common.rb +26 -0
  17. data/lib/betterdocs/dsl/controller.rb +9 -0
  18. data/lib/betterdocs/dsl/controller/action.rb +126 -0
  19. data/lib/betterdocs/dsl/controller/action/param.rb +25 -0
  20. data/lib/betterdocs/dsl/controller/action/response.rb +47 -0
  21. data/lib/betterdocs/dsl/controller/controller.rb +31 -0
  22. data/lib/betterdocs/dsl/controller/controller_base.rb +21 -0
  23. data/lib/betterdocs/dsl/json_params.rb +8 -0
  24. data/lib/betterdocs/dsl/json_params/param.rb +31 -0
  25. data/lib/betterdocs/dsl/json_type_mapper.rb +27 -0
  26. data/lib/betterdocs/dsl/naming.rb +32 -0
  27. data/lib/betterdocs/dsl/representer.rb +29 -0
  28. data/lib/betterdocs/dsl/result.rb +10 -0
  29. data/lib/betterdocs/dsl/result/collection_property.rb +9 -0
  30. data/lib/betterdocs/dsl/result/link.rb +37 -0
  31. data/lib/betterdocs/dsl/result/property.rb +53 -0
  32. data/lib/betterdocs/generator/config_shortcuts.rb +28 -0
  33. data/lib/betterdocs/generator/markdown.rb +151 -0
  34. data/lib/betterdocs/generator/markdown/templates/README.md.erb +9 -0
  35. data/lib/betterdocs/generator/markdown/templates/section.md.erb +132 -0
  36. data/lib/betterdocs/global.rb +143 -0
  37. data/lib/betterdocs/json_params_representer.rb +37 -0
  38. data/lib/betterdocs/json_params_representer_collector.rb +48 -0
  39. data/lib/betterdocs/mix_into_controller.rb +19 -0
  40. data/lib/betterdocs/rake_tasks.rb +5 -0
  41. data/lib/betterdocs/representer.rb +42 -0
  42. data/lib/betterdocs/result_representer.rb +68 -0
  43. data/lib/betterdocs/result_representer_collector.rb +82 -0
  44. data/lib/betterdocs/section.rb +6 -0
  45. data/lib/betterdocs/tasks/doc.rake +55 -0
  46. data/lib/betterdocs/version.rb +8 -0
  47. data/spec/controller_dsl_spec.rb +143 -0
  48. data/spec/generator/markdown_spec.rb +5 -0
  49. data/spec/json_params_representer_spec.rb +79 -0
  50. data/spec/json_type_mapper_spec.rb +33 -0
  51. data/spec/result_representer_dsl_spec.rb +183 -0
  52. data/spec/result_representer_spec.rb +182 -0
  53. data/spec/spec_helper.rb +19 -0
  54. metadata +234 -0
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Betterdocs::ResultRepresenter do
4
+ module MySubRepresenter
5
+ include Betterdocs::ResultRepresenter
6
+
7
+ property :some_property
8
+
9
+ link 'url3' do
10
+ url { 'some_url' }
11
+ end
12
+ end
13
+
14
+ module MyResult
15
+ include Betterdocs::ResultRepresenter
16
+
17
+ property :simple
18
+
19
+ property :derived
20
+
21
+ property :complex do
22
+ represent_with MySubRepresenter
23
+ end
24
+
25
+ property :simple_old_name, as: :simple_new_name
26
+
27
+ property :derived_old_name, as: :derived_new_name
28
+
29
+ property :complex_old_name, as: :complex_new_name do
30
+ represent_with MySubRepresenter
31
+ end
32
+
33
+ collection :thingies do
34
+ represent_with MySubRepresenter
35
+ end
36
+
37
+ collection :other_thingies, as: :thingies_new_name do
38
+ represent_with MySubRepresenter
39
+ end
40
+
41
+ property :if_no, if: -> { no }
42
+
43
+ property :unless_yes, unless: -> { yes }
44
+
45
+ property :if_predicate, if: -> { simple != 'simple property' }
46
+
47
+ property :unless_predicate, unless: -> { simple == 'simple property' }
48
+
49
+ link 'url1' do
50
+ url { 'an_url' }
51
+ end
52
+
53
+ link 'url2' do
54
+ url { 'another_url' }
55
+ end
56
+
57
+ link 'template' do
58
+ url { 'an_url_with{placeholder}' }
59
+ templated true
60
+ end
61
+
62
+ def derived
63
+ 'derived property'
64
+ end
65
+
66
+ def derived_old_name
67
+ 'derived old property'
68
+ end
69
+ end
70
+
71
+ let :object do
72
+ OpenStruct.new.tap do |o|
73
+ o.simple = 'simple property'
74
+ o.complex = OpenStruct.new
75
+ o.complex.some_property = 'some property'
76
+ o.simple_old_name = 'simple old property'
77
+ o.complex_old_name = OpenStruct.new
78
+ o.complex_old_name.some_property = 'some old property'
79
+ o.thingies = [
80
+ OpenStruct.new.tap { |t| t.some_property = 'first' },
81
+ OpenStruct.new.tap { |t| t.some_property = 'second' },
82
+ ]
83
+ o.other_thingies = [
84
+ OpenStruct.new.tap { |t| t.some_property = 'first other' },
85
+ ]
86
+ o.if_no = :no
87
+ o.unless_yes = :yes
88
+ o.if_predicate = true
89
+ o.unless_predicate = true
90
+ MyResult.apply(o)
91
+ end
92
+ end
93
+
94
+ let :parsed_object do
95
+ JSON(object.to_json)
96
+ end
97
+
98
+ it 'can be converted into a Hash instance' do
99
+ expect(object.as_json).to be_a Hash
100
+ expect(object.as_json).not_to be_empty
101
+ end
102
+
103
+ it 'it can be turned into valid json' do
104
+ expect(JSON.parse(object.to_json)).to be_a Hash
105
+ end
106
+
107
+ it 'can output pretty json' do
108
+ expect(JSON.pretty_generate(object)).to include "\n"
109
+ end
110
+
111
+ it 'can output ugly json' do
112
+ expect(JSON.generate(object)).to_not include "\n"
113
+ end
114
+
115
+ it 'supports simple properties' do
116
+ expect(parsed_object['simple']).to eq 'simple property'
117
+ end
118
+
119
+ it 'supports derived properties' do
120
+ expect(parsed_object['derived']).to eq 'derived property'
121
+ end
122
+
123
+ it 'supports complex properties' do
124
+ expect(parsed_object['complex']['some_property']).to eq 'some property'
125
+ end
126
+
127
+ it 'supports renaming simple properties' do
128
+ expect(parsed_object['simple_new_name']).to eq 'simple old property'
129
+ end
130
+
131
+ it 'supports renaming derived properties' do
132
+ expect(parsed_object['derived_new_name']).to eq 'derived old property'
133
+ end
134
+
135
+ it 'supports renaming complex properties' do
136
+ expect(parsed_object['complex_new_name']['some_property']).to eq 'some old property'
137
+ end
138
+
139
+ it 'supports collections' do
140
+ expect(parsed_object['thingies'].size).to eq 2
141
+ expect(parsed_object['thingies'][0]['some_property']).to eq 'first'
142
+ expect(parsed_object['thingies'][1]['some_property']).to eq 'second'
143
+ end
144
+
145
+ it 'supports renaming collections' do
146
+ expect(parsed_object['thingies_new_name'].size).to eq 1
147
+ expect(parsed_object['thingies_new_name'][0]['some_property']).to eq 'first other'
148
+ end
149
+
150
+ it "doesn't crash missing collections" do
151
+ object.thingies = nil
152
+ expect(parsed_object['thingies']).to eq []
153
+ end
154
+
155
+ it 'supports links' do
156
+ links = parsed_object['links']
157
+ expect(links.size).to eq 3
158
+ expect(links[0]['rel']).to eq 'url1'
159
+ expect(links[0]['href']).to eq 'an_url'
160
+ expect(links[0]).to_not have_key 'templated'
161
+ expect(links[1]['rel']).to eq 'url2'
162
+ expect(links[1]['href']).to eq 'another_url'
163
+ expect(links[1]).to_not have_key 'templated'
164
+ expect(links[2]['rel']).to eq 'template'
165
+ expect(links[2]['href']).to eq 'an_url_with{placeholder}'
166
+ expect(links[2]).to have_key 'templated'
167
+ expect(links[2]['templated']).to eq true
168
+ end
169
+
170
+ it "doesn't have to have links" do
171
+ representer = Module.new { include Betterdocs::ResultRepresenter }
172
+ parsed_object = JSON(representer.apply(object).to_json)
173
+ expect(parsed_object['links']).to eq []
174
+ end
175
+
176
+ it 'supports conditional properties' do
177
+ expect(parsed_object['if_no']).to be_nil
178
+ expect(parsed_object['unless_yes']).to be_nil
179
+ expect(parsed_object['if_predicate']).to be_nil
180
+ expect(parsed_object['unless_predicate']).to be_nil
181
+ end
182
+ end
@@ -0,0 +1,19 @@
1
+ require 'simplecov'
2
+ require "codeclimate-test-reporter"
3
+ if ENV['START_SIMPLECOV'].to_i == 1
4
+ SimpleCov.start do
5
+ add_filter "#{File.basename(File.dirname(__FILE__))}/"
6
+ end
7
+ end
8
+ if ENV['CODECLIMATE_REPO_TOKEN']
9
+ if ENV['START_SIMPLECOV']
10
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
11
+ SimpleCov::Formatter::HTMLFormatter,
12
+ CodeClimate::TestReporter::Formatter
13
+ ]
14
+ end
15
+ CodeClimate::TestReporter.start
16
+ end
17
+ require 'rspec'
18
+ require 'byebug'
19
+ require 'betterdocs'
metadata ADDED
@@ -0,0 +1,234 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: betterdocs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - betterplace Developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gem_hadar
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tins
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.5
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.3'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.5
75
+ - !ruby/object:Gem::Dependency
76
+ name: rails
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '3'
82
+ - - "<"
83
+ - !ruby/object:Gem::Version
84
+ version: '5'
85
+ type: :runtime
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '3'
92
+ - - "<"
93
+ - !ruby/object:Gem::Version
94
+ version: '5'
95
+ - !ruby/object:Gem::Dependency
96
+ name: term-ansicolor
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '1.3'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '1.3'
109
+ description: This library provides tools to generate API documention for a web site's
110
+ REST-ful JSON API.
111
+ email: developers@betterplace.org
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - README.md
116
+ - lib/betterdocs.rb
117
+ - lib/betterdocs/controller_collector.rb
118
+ - lib/betterdocs/dsl.rb
119
+ - lib/betterdocs/dsl/common.rb
120
+ - lib/betterdocs/dsl/controller.rb
121
+ - lib/betterdocs/dsl/controller/action.rb
122
+ - lib/betterdocs/dsl/controller/action/param.rb
123
+ - lib/betterdocs/dsl/controller/action/response.rb
124
+ - lib/betterdocs/dsl/controller/controller.rb
125
+ - lib/betterdocs/dsl/controller/controller_base.rb
126
+ - lib/betterdocs/dsl/json_params.rb
127
+ - lib/betterdocs/dsl/json_params/param.rb
128
+ - lib/betterdocs/dsl/json_type_mapper.rb
129
+ - lib/betterdocs/dsl/naming.rb
130
+ - lib/betterdocs/dsl/representer.rb
131
+ - lib/betterdocs/dsl/result.rb
132
+ - lib/betterdocs/dsl/result/collection_property.rb
133
+ - lib/betterdocs/dsl/result/link.rb
134
+ - lib/betterdocs/dsl/result/property.rb
135
+ - lib/betterdocs/generator/config_shortcuts.rb
136
+ - lib/betterdocs/generator/markdown.rb
137
+ - lib/betterdocs/global.rb
138
+ - lib/betterdocs/json_params_representer.rb
139
+ - lib/betterdocs/json_params_representer_collector.rb
140
+ - lib/betterdocs/mix_into_controller.rb
141
+ - lib/betterdocs/rake_tasks.rb
142
+ - lib/betterdocs/representer.rb
143
+ - lib/betterdocs/result_representer.rb
144
+ - lib/betterdocs/result_representer_collector.rb
145
+ - lib/betterdocs/section.rb
146
+ - lib/betterdocs/version.rb
147
+ files:
148
+ - ".codeclimate.yml"
149
+ - ".gitignore"
150
+ - ".rspec"
151
+ - ".travis.yml"
152
+ - COPYING
153
+ - Gemfile
154
+ - LICENSE
155
+ - README.md
156
+ - Rakefile
157
+ - VERSION
158
+ - betterdocs.gemspec
159
+ - lib/betterdocs.rb
160
+ - lib/betterdocs/controller_collector.rb
161
+ - lib/betterdocs/dsl.rb
162
+ - lib/betterdocs/dsl/common.rb
163
+ - lib/betterdocs/dsl/controller.rb
164
+ - lib/betterdocs/dsl/controller/action.rb
165
+ - lib/betterdocs/dsl/controller/action/param.rb
166
+ - lib/betterdocs/dsl/controller/action/response.rb
167
+ - lib/betterdocs/dsl/controller/controller.rb
168
+ - lib/betterdocs/dsl/controller/controller_base.rb
169
+ - lib/betterdocs/dsl/json_params.rb
170
+ - lib/betterdocs/dsl/json_params/param.rb
171
+ - lib/betterdocs/dsl/json_type_mapper.rb
172
+ - lib/betterdocs/dsl/naming.rb
173
+ - lib/betterdocs/dsl/representer.rb
174
+ - lib/betterdocs/dsl/result.rb
175
+ - lib/betterdocs/dsl/result/collection_property.rb
176
+ - lib/betterdocs/dsl/result/link.rb
177
+ - lib/betterdocs/dsl/result/property.rb
178
+ - lib/betterdocs/generator/config_shortcuts.rb
179
+ - lib/betterdocs/generator/markdown.rb
180
+ - lib/betterdocs/generator/markdown/templates/README.md.erb
181
+ - lib/betterdocs/generator/markdown/templates/section.md.erb
182
+ - lib/betterdocs/global.rb
183
+ - lib/betterdocs/json_params_representer.rb
184
+ - lib/betterdocs/json_params_representer_collector.rb
185
+ - lib/betterdocs/mix_into_controller.rb
186
+ - lib/betterdocs/rake_tasks.rb
187
+ - lib/betterdocs/representer.rb
188
+ - lib/betterdocs/result_representer.rb
189
+ - lib/betterdocs/result_representer_collector.rb
190
+ - lib/betterdocs/section.rb
191
+ - lib/betterdocs/tasks/doc.rake
192
+ - lib/betterdocs/version.rb
193
+ - spec/controller_dsl_spec.rb
194
+ - spec/generator/markdown_spec.rb
195
+ - spec/json_params_representer_spec.rb
196
+ - spec/json_type_mapper_spec.rb
197
+ - spec/result_representer_dsl_spec.rb
198
+ - spec/result_representer_spec.rb
199
+ - spec/spec_helper.rb
200
+ homepage: http://github.com/betterplace/betterdocs
201
+ licenses: []
202
+ metadata: {}
203
+ post_install_message:
204
+ rdoc_options:
205
+ - "--title"
206
+ - 'Betterdocs -- '
207
+ - "--main"
208
+ - README.md
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.5.1
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Betterplace API documentation library
227
+ test_files:
228
+ - spec/controller_dsl_spec.rb
229
+ - spec/generator/markdown_spec.rb
230
+ - spec/json_params_representer_spec.rb
231
+ - spec/json_type_mapper_spec.rb
232
+ - spec/result_representer_dsl_spec.rb
233
+ - spec/result_representer_spec.rb
234
+ - spec/spec_helper.rb