webspicy 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/bin/webspicy +6 -1
  3. data/doc/1-black-box-scene.md +109 -0
  4. data/doc/2-black-box-testing.md +27 -0
  5. data/doc/3-specification-importance.md +41 -0
  6. data/doc/4-sequence-diagram.md +82 -0
  7. data/lib/webspicy.rb +17 -7
  8. data/lib/webspicy/configuration.rb +48 -8
  9. data/lib/webspicy/configuration/scope.rb +22 -8
  10. data/lib/webspicy/formaldoc.fio +5 -5
  11. data/lib/webspicy/specification.rb +2 -0
  12. data/lib/webspicy/specification/condition.rb +23 -0
  13. data/lib/webspicy/specification/errcondition.rb +1 -0
  14. data/lib/webspicy/specification/postcondition.rb +1 -0
  15. data/lib/webspicy/specification/precondition.rb +1 -0
  16. data/lib/webspicy/specification/service.rb +11 -6
  17. data/lib/webspicy/specification/test_case.rb +8 -9
  18. data/lib/webspicy/support.rb +21 -0
  19. data/lib/webspicy/support/hooks.rb +65 -0
  20. data/lib/webspicy/support/world.rb +47 -0
  21. data/lib/webspicy/tester.rb +136 -3
  22. data/lib/webspicy/tester/client.rb +22 -42
  23. data/lib/webspicy/tester/file_checker.rb +22 -0
  24. data/lib/webspicy/tester/invocation.rb +15 -48
  25. data/lib/webspicy/tester/reporter.rb +85 -0
  26. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  27. data/lib/webspicy/tester/reporter/documentation.rb +67 -0
  28. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  29. data/lib/webspicy/tester/reporter/exceptions.rb +60 -0
  30. data/lib/webspicy/tester/reporter/file_progress.rb +22 -0
  31. data/lib/webspicy/tester/reporter/file_summary.rb +42 -0
  32. data/lib/webspicy/tester/reporter/progress.rb +28 -0
  33. data/lib/webspicy/tester/reporter/summary.rb +62 -0
  34. data/lib/webspicy/tester/result.rb +139 -0
  35. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  36. data/lib/webspicy/tester/result/check.rb +33 -0
  37. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  38. data/lib/webspicy/tester/result/error_schema_met.rb +24 -0
  39. data/lib/webspicy/tester/result/output_schema_met.rb +24 -0
  40. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  41. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  42. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  43. data/lib/webspicy/version.rb +1 -1
  44. data/lib/webspicy/web.rb +4 -0
  45. data/lib/webspicy/web/client.rb +15 -0
  46. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  47. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  48. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  49. data/lib/webspicy/web/invocation.rb +65 -0
  50. data/lib/webspicy/web/mocker.rb +90 -0
  51. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  52. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  53. data/lib/webspicy/web/openapi/generator.rb +129 -0
  54. data/spec/unit/specification/test_condition.rb +26 -0
  55. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  56. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  57. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  58. data/spec/unit/support/world/fixtures/array.json +8 -0
  59. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  60. data/spec/unit/support/world/fixtures/single.json +11 -0
  61. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  62. data/spec/unit/support/world/test_world.rb +56 -0
  63. data/spec/unit/test_configuration.rb +49 -0
  64. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  65. data/spec/unit/web/openapi/test_generator.rb +31 -0
  66. metadata +61 -59
  67. data/examples/restful/Gemfile +0 -5
  68. data/examples/restful/Rakefile +0 -25
  69. data/examples/restful/app.rb +0 -180
  70. data/examples/restful/webspicy/config.rb +0 -24
  71. data/examples/restful/webspicy/formaldef/todo/_one/delete.yml +0 -55
  72. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +0 -46
  73. data/examples/restful/webspicy/formaldef/todo/_one/get.yml +0 -50
  74. data/examples/restful/webspicy/formaldef/todo/_one/patch.yml +0 -66
  75. data/examples/restful/webspicy/formaldef/todo/_one/put.yml +0 -65
  76. data/examples/restful/webspicy/formaldef/todo/get.yml +0 -36
  77. data/examples/restful/webspicy/formaldef/todo/options.yml +0 -32
  78. data/examples/restful/webspicy/formaldef/todo/post.csv.yml +0 -43
  79. data/examples/restful/webspicy/formaldef/todo/post.file.yml +0 -40
  80. data/examples/restful/webspicy/formaldef/todo/post.yml +0 -51
  81. data/examples/restful/webspicy/formaldef/todo/todos.csv +0 -4
  82. data/examples/restful/webspicy/rack.rb +0 -7
  83. data/examples/restful/webspicy/real.rb +0 -8
  84. data/examples/restful/webspicy/schema.fio +0 -20
  85. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  86. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  87. data/examples/restful/webspicy/support/todo_not_removed.rb +0 -21
  88. data/examples/restful/webspicy/support/todo_removed.rb +0 -20
  89. data/examples/single_spec/spec.yml +0 -59
  90. data/examples/website/config.rb +0 -2
  91. data/examples/website/schema.fio +0 -1
  92. data/examples/website/specification/get-http.yml +0 -30
  93. data/examples/website/specification/get-https.yml +0 -30
  94. data/lib/webspicy/checker.rb +0 -10
  95. data/lib/webspicy/mocker.rb +0 -88
  96. data/lib/webspicy/openapi/generator.rb +0 -127
  97. data/lib/webspicy/rspec/checker.rb +0 -2
  98. data/lib/webspicy/rspec/checker/rspec_checker.rb +0 -24
  99. data/lib/webspicy/rspec/support/rspec_runnable.rb +0 -27
  100. data/lib/webspicy/rspec/tester.rb +0 -4
  101. data/lib/webspicy/rspec/tester/rspec_asserter.rb +0 -121
  102. data/lib/webspicy/rspec/tester/rspec_matchers.rb +0 -114
  103. data/lib/webspicy/rspec/tester/rspec_tester.rb +0 -63
  104. data/spec/unit/mocker/test_mocker.rb +0 -32
  105. data/spec/unit/openapi/test_generator.rb +0 -28
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+ require 'webspicy/web'
4
+ require 'webspicy/web/mocker'
5
+ module Webspicy
6
+ module Web
7
+ describe Mocker do
8
+ include Rack::Test::Methods
9
+
10
+ let(:app) {
11
+ Mocker.new(Configuration.dress(restful_folder))
12
+ }
13
+
14
+ it 'works as expected' do
15
+ get '/todo/'
16
+ expect(last_response.status).to eql(200)
17
+ expect(last_response.body).not_to be_empty
18
+ end
19
+
20
+ it 'supports CORS on OPTIONS' do
21
+ options '/todo/'
22
+ expect(last_response.status).to eql(204)
23
+ expect(last_response.body).to be_empty
24
+ end
25
+
26
+ it 'returns the correct status code, taken from first example' do
27
+ delete '/todo/1'
28
+ expect(last_response.status).to eql(204)
29
+ expect(last_response.body).to be_empty
30
+ expect(last_response["Content-Type"]).to be_nil
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'webspicy/web'
3
+ require 'webspicy/web/openapi'
4
+ require 'openapi3_parser'
5
+ module Webspicy
6
+ module Web
7
+ module Openapi
8
+ describe Generator do
9
+
10
+ let(:config) {
11
+ Configuration.new(restful_folder)
12
+ }
13
+
14
+ subject {
15
+ ruby_objs = Generator.new(config).call
16
+ JSON.parse(ruby_objs.to_json)
17
+ }
18
+
19
+ it 'works fine' do
20
+ document = Openapi3Parser.load(subject)
21
+ #puts JSON.pretty_generate(subject)
22
+ document.errors.each do |err|
23
+ puts err.inspect
24
+ end unless document.errors.empty?
25
+ expect(document.errors).to be_empty
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webspicy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-23 00:00:00.000000000 Z
11
+ date: 2021-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -45,7 +45,7 @@ dependencies:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.10'
48
- type: :runtime
48
+ type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.4.1
62
- type: :runtime
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
@@ -126,22 +126,22 @@ dependencies:
126
126
  name: rack-robustness
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '1.1'
132
129
  - - ">="
133
130
  - !ruby/object:Gem::Version
134
131
  version: 1.1.0
132
+ - - "~>"
133
+ - !ruby/object:Gem::Version
134
+ version: '1.1'
135
135
  type: :runtime
136
136
  prerelease: false
137
137
  version_requirements: !ruby/object:Gem::Requirement
138
138
  requirements:
139
- - - "~>"
140
- - !ruby/object:Gem::Version
141
- version: '1.1'
142
139
  - - ">="
143
140
  - !ruby/object:Gem::Version
144
141
  version: 1.1.0
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '1.1'
145
145
  - !ruby/object:Gem::Dependency
146
146
  name: mustermann
147
147
  requirement: !ruby/object:Gem::Requirement
@@ -209,53 +209,19 @@ files:
209
209
  - README.md
210
210
  - Rakefile
211
211
  - bin/webspicy
212
- - examples/restful/Gemfile
213
- - examples/restful/Rakefile
214
- - examples/restful/app.rb
215
- - examples/restful/webspicy/config.rb
216
- - examples/restful/webspicy/formaldef/todo/_one/delete.yml
217
- - examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml
218
- - examples/restful/webspicy/formaldef/todo/_one/get.yml
219
- - examples/restful/webspicy/formaldef/todo/_one/patch.yml
220
- - examples/restful/webspicy/formaldef/todo/_one/put.yml
221
- - examples/restful/webspicy/formaldef/todo/get.yml
222
- - examples/restful/webspicy/formaldef/todo/options.yml
223
- - examples/restful/webspicy/formaldef/todo/post.csv.yml
224
- - examples/restful/webspicy/formaldef/todo/post.file.yml
225
- - examples/restful/webspicy/formaldef/todo/post.yml
226
- - examples/restful/webspicy/formaldef/todo/todos.csv
227
- - examples/restful/webspicy/rack.rb
228
- - examples/restful/webspicy/real.rb
229
- - examples/restful/webspicy/schema.fio
230
- - examples/restful/webspicy/support/must_be_an_admin.rb
231
- - examples/restful/webspicy/support/must_be_authenticated.rb
232
- - examples/restful/webspicy/support/todo_not_removed.rb
233
- - examples/restful/webspicy/support/todo_removed.rb
234
- - examples/single_spec/spec.yml
235
- - examples/website/config.rb
236
- - examples/website/schema.fio
237
- - examples/website/specification/get-http.yml
238
- - examples/website/specification/get-https.yml
212
+ - doc/1-black-box-scene.md
213
+ - doc/2-black-box-testing.md
214
+ - doc/3-specification-importance.md
215
+ - doc/4-sequence-diagram.md
239
216
  - lib/finitio/webspicy/scalars.fio
240
217
  - lib/webspicy.rb
241
- - lib/webspicy/checker.rb
242
218
  - lib/webspicy/configuration.rb
243
219
  - lib/webspicy/configuration/scope.rb
244
220
  - lib/webspicy/configuration/single_url.rb
245
221
  - lib/webspicy/configuration/single_yml_file.rb
246
222
  - lib/webspicy/formaldoc.fio
247
- - lib/webspicy/mocker.rb
248
- - lib/webspicy/mocker/config.ru
249
- - lib/webspicy/openapi.rb
250
- - lib/webspicy/openapi/generator.rb
251
- - lib/webspicy/rspec/checker.rb
252
- - lib/webspicy/rspec/checker/rspec_checker.rb
253
- - lib/webspicy/rspec/support/rspec_runnable.rb
254
- - lib/webspicy/rspec/tester.rb
255
- - lib/webspicy/rspec/tester/rspec_asserter.rb
256
- - lib/webspicy/rspec/tester/rspec_matchers.rb
257
- - lib/webspicy/rspec/tester/rspec_tester.rb
258
223
  - lib/webspicy/specification.rb
224
+ - lib/webspicy/specification/condition.rb
259
225
  - lib/webspicy/specification/errcondition.rb
260
226
  - lib/webspicy/specification/file_upload.rb
261
227
  - lib/webspicy/specification/postcondition.rb
@@ -267,42 +233,78 @@ files:
267
233
  - lib/webspicy/support.rb
268
234
  - lib/webspicy/support/colorize.rb
269
235
  - lib/webspicy/support/data_object.rb
236
+ - lib/webspicy/support/hooks.rb
270
237
  - lib/webspicy/support/status_range.rb
238
+ - lib/webspicy/support/world.rb
271
239
  - lib/webspicy/tester.rb
272
240
  - lib/webspicy/tester/asserter.rb
273
241
  - lib/webspicy/tester/assertions.rb
274
242
  - lib/webspicy/tester/client.rb
275
- - lib/webspicy/tester/client/http_client.rb
276
- - lib/webspicy/tester/client/rack_test_client.rb
277
- - lib/webspicy/tester/client/support.rb
278
243
  - lib/webspicy/tester/failure.rb
244
+ - lib/webspicy/tester/file_checker.rb
279
245
  - lib/webspicy/tester/invocation.rb
246
+ - lib/webspicy/tester/reporter.rb
247
+ - lib/webspicy/tester/reporter/composite.rb
248
+ - lib/webspicy/tester/reporter/documentation.rb
249
+ - lib/webspicy/tester/reporter/error_count.rb
250
+ - lib/webspicy/tester/reporter/exceptions.rb
251
+ - lib/webspicy/tester/reporter/file_progress.rb
252
+ - lib/webspicy/tester/reporter/file_summary.rb
253
+ - lib/webspicy/tester/reporter/progress.rb
254
+ - lib/webspicy/tester/reporter/summary.rb
255
+ - lib/webspicy/tester/result.rb
256
+ - lib/webspicy/tester/result/assert_met.rb
257
+ - lib/webspicy/tester/result/check.rb
258
+ - lib/webspicy/tester/result/errcondition_met.rb
259
+ - lib/webspicy/tester/result/error_schema_met.rb
260
+ - lib/webspicy/tester/result/output_schema_met.rb
261
+ - lib/webspicy/tester/result/postcondition_met.rb
262
+ - lib/webspicy/tester/result/response_header_met.rb
263
+ - lib/webspicy/tester/result/response_status_met.rb
280
264
  - lib/webspicy/version.rb
265
+ - lib/webspicy/web.rb
266
+ - lib/webspicy/web/client.rb
267
+ - lib/webspicy/web/client/http_client.rb
268
+ - lib/webspicy/web/client/rack_test_client.rb
269
+ - lib/webspicy/web/client/support.rb
270
+ - lib/webspicy/web/invocation.rb
271
+ - lib/webspicy/web/mocker.rb
272
+ - lib/webspicy/web/mocker/config.ru
273
+ - lib/webspicy/web/openapi.rb
274
+ - lib/webspicy/web/openapi/generator.rb
281
275
  - spec/spec_helper.rb
282
276
  - spec/unit/configuration/config.rb
283
277
  - spec/unit/configuration/scope/test_each_service.rb
284
278
  - spec/unit/configuration/scope/test_each_specification.rb
285
279
  - spec/unit/configuration/scope/test_expand_example.rb
286
280
  - spec/unit/configuration/scope/test_to_real_url.rb
287
- - spec/unit/mocker/test_mocker.rb
288
- - spec/unit/openapi/test_generator.rb
289
281
  - spec/unit/specification/precondition/test_global_request_headers.rb
290
282
  - spec/unit/specification/service/test_dress_params.rb
291
283
  - spec/unit/specification/test_case/test_mutate.rb
284
+ - spec/unit/specification/test_condition.rb
292
285
  - spec/unit/specification/test_instantiate_url.rb
293
286
  - spec/unit/specification/test_url_placeholders.rb
287
+ - spec/unit/support/hooks/test_fire_after_each.rb
288
+ - spec/unit/support/hooks/test_fire_around.rb
289
+ - spec/unit/support/hooks/test_fire_before_each.rb
294
290
  - spec/unit/support/test_status_range.rb
291
+ - spec/unit/support/world/fixtures/array.json
292
+ - spec/unit/support/world/fixtures/queue.rb
293
+ - spec/unit/support/world/fixtures/single.json
294
+ - spec/unit/support/world/fixtures/yaml.yml
295
+ - spec/unit/support/world/test_world.rb
295
296
  - spec/unit/test_configuration.rb
296
- - spec/unit/tester/client/test_around.rb
297
297
  - spec/unit/tester/test_asserter.rb
298
298
  - spec/unit/tester/test_assertions.rb
299
+ - spec/unit/web/mocker/test_mocker.rb
300
+ - spec/unit/web/openapi/test_generator.rb
299
301
  - tasks/gem.rake
300
302
  - tasks/test.rake
301
303
  homepage: http://github.com/enspirit/webspicy
302
304
  licenses:
303
305
  - MIT
304
306
  metadata: {}
305
- post_install_message:
307
+ post_install_message:
306
308
  rdoc_options: []
307
309
  require_paths:
308
310
  - lib
@@ -317,8 +319,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
317
319
  - !ruby/object:Gem::Version
318
320
  version: '0'
319
321
  requirements: []
320
- rubygems_version: 3.1.2
321
- signing_key:
322
+ rubygems_version: 3.0.8
323
+ signing_key:
322
324
  specification_version: 4
323
325
  summary: Webspicy helps testing web services as software operation black boxes!
324
326
  test_files: []
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem "rake", "~> 12"
4
- gem 'sinatra', "~> 2"
5
- gem "webspicy", path: "../.."
@@ -1,25 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path("..", __FILE__)
2
- require 'app'
3
-
4
- desc "Checks all .yml definition files"
5
- task :check do
6
- Webspicy::Checker.new(Path.dir/'webspicy').call
7
- end
8
-
9
- namespace :test do
10
- desc "Run all tests directly on Sinatra application using rack/test"
11
- task :rack do
12
- Webspicy::Tester.new(Path.dir/'webspicy/rack.rb').call
13
- end
14
-
15
- desc "Runs all tests on the real web server (must be launched previously)"
16
- task :real do
17
- Webspicy::Tester.new(Path.dir/'webspicy/real.rb').call
18
- end
19
- end
20
-
21
- desc "Runs all checks and tests"
22
- task :test => :check
23
- task :test => :"test:rack"
24
-
25
- task :default => :test
@@ -1,180 +0,0 @@
1
- require 'webspicy'
2
- require 'sinatra'
3
- require 'json'
4
- require 'path'
5
- require 'finitio'
6
- require 'rack/robustness'
7
- require 'csv'
8
-
9
- SCHEMA = Finitio::DEFAULT_SYSTEM.parse (Path.dir/('webspicy/schema.fio')).read
10
-
11
- TODOLIST = [
12
- {
13
- id: 1,
14
- description: "Refactor the framework"
15
- },
16
- {
17
- id: 2,
18
- description: "Write documentation"
19
- }
20
- ]
21
-
22
- disable :show_exceptions
23
- enable :raise_errors
24
-
25
- set :todolist, TODOLIST.dup
26
-
27
- set(:auth) do |role|
28
- condition do
29
- token = env['HTTP_AUTHORIZATION']
30
- case token
31
- when NilClass
32
- halt [
33
- 401,
34
- {'Content-Type' => 'application/json'},
35
- [{ error: "Please log in first" }.to_json]
36
- ]
37
- when "Bearer #{role}"
38
- true
39
- else
40
- halt [
41
- 401,
42
- {'Content-Type' => 'application/json'},
43
- [{ error: "#{role.capitalize} required" }.to_json]
44
- ]
45
- end
46
- end
47
- end
48
-
49
- use Rack::Robustness do |g|
50
- g.no_catch_all
51
- g.status 400
52
- g.content_type 'application/json'
53
- g.body{|ex| { error: ex.message }.to_json }
54
- g.on(Finitio::TypeError)
55
- end
56
-
57
- options '*' do
58
- status 204
59
- ""
60
- end
61
-
62
- post '/reset' do
63
- settings.todolist = TODOLIST.dup
64
- status 200
65
- ""
66
- end
67
-
68
- get '/todo/' do
69
- raise unless request.env['HTTP_ACCEPT'] == 'application/json'
70
- content_type :json
71
- settings.todolist.to_json
72
- end
73
-
74
- post '/todo/', :auth => :user do
75
- content_type :json
76
- case todos = loaded_body
77
- when Array
78
- n = 0
79
- todos.each do |todo|
80
- settings.todolist << todo
81
- n += 1
82
- end
83
- status 201
84
- { count: n }.to_json
85
- when Hash
86
- todo = SCHEMA["Todo"].dress(todos)
87
- if settings.todolist.find{|t| t[:id] == todo[:id] }
88
- status 409
89
- {error: "Identifier already in use"}.to_json
90
- else
91
- settings.todolist << todo
92
- status 201
93
- todo.to_json
94
- end
95
- end
96
- end
97
-
98
- get '/todo/:id' do |id|
99
- raise unless request.env['HTTP_ACCEPT'] == 'application/json'
100
- content_type :json
101
- todo = settings.todolist.find{|todo| todo[:id] == Integer(id) }
102
- if todo.nil?
103
- status 404
104
- {error: "No such todo"}.to_json
105
- else
106
- todo.to_json
107
- end
108
- end
109
-
110
- patch '/todo/:id', :auth => :user do |id|
111
- content_type :json
112
- todo = settings.todolist.find{|todo| todo[:id] == Integer(id) }
113
- if todo.nil?
114
- status 404
115
- {error: "No such todo"}.to_json
116
- else
117
- patch = SCHEMA["TodoPatch"].dress(loaded_body.merge("id" => Integer(id)))
118
- patched = todo.merge(patch)
119
- settings.todolist = settings.todolist.reject{|todo| todo[:id] == Integer(id) }
120
- settings.todolist << patched
121
- patched.to_json
122
- end
123
- end
124
-
125
- put '/todo/:id', :auth => :user do |id|
126
- content_type :json
127
- todo = settings.todolist.find{|todo| todo[:id] == Integer(id) }
128
- if todo.nil?
129
- status 404
130
- {error: "No such todo"}.to_json
131
- else
132
- put = SCHEMA["TodoPut"].dress(loaded_body.merge(id: Integer(id)))
133
- updated = todo.merge(put)
134
- settings.todolist = settings.todolist.reject{|todo| todo[:id] == Integer(id) }
135
- settings.todolist << updated
136
- updated.to_json
137
- end
138
- end
139
-
140
- delete '/todo/:id', :auth => :admin do |id|
141
- content_type :json
142
- todo = settings.todolist.find{|todo| todo[:id] == Integer(id) }
143
- if todo.nil?
144
- status 404
145
- {error: "No such todo"}.to_json
146
- else
147
- settings.todolist = settings.todolist.reject{|todo| todo[:id] == Integer(id) }
148
- status 204
149
- content_type "text/plain"
150
- end
151
- end
152
-
153
- ###
154
-
155
- def loaded_body
156
- case ctype = request.content_type
157
- when /json/
158
- JSON.load(request.body.read)
159
- when /csv/
160
- csv = ::CSV.new(request.body.read, :headers => true, :header_converters => :symbol)
161
- csv.map {|row| row.to_hash }
162
- when /multipart\/form-data/
163
- file_body params[:file]
164
- else
165
- halt [415,{},["Unsupported content type: `#{ctype}`"]]
166
- end
167
- end
168
-
169
- def file_body(file)
170
- ctype = Path(file[:filename] || file["filename"]).extname
171
- ctype = (file[:type] || file["type"]) if ctype.nil? or ctype.empty?
172
- case ctype
173
- when /csv/
174
- str = file[:tempfile].read
175
- csv = ::CSV.new(str, :headers => true, :header_converters => :symbol)
176
- csv.map {|row| row.to_hash }
177
- else
178
- halt [415,{},["Unsupported content type: `#{ctype}`\n#{file.inspect}"]]
179
- end
180
- end