barker 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/.simplecov +7 -0
  6. data/Gemfile +6 -0
  7. data/Gemfile.lock +50 -0
  8. data/LICENSE +22 -0
  9. data/README.md +204 -0
  10. data/Rakefile +12 -0
  11. data/barker.gemspec +29 -0
  12. data/ci/run +20 -0
  13. data/lib/barker/answer.rb +11 -0
  14. data/lib/barker/api/base.rb +145 -0
  15. data/lib/barker/api/response/base.rb +25 -0
  16. data/lib/barker/api/response/candidate.rb +38 -0
  17. data/lib/barker/api/response/candidate_answer.rb +23 -0
  18. data/lib/barker/api/response/candidate_joker.rb +21 -0
  19. data/lib/barker/api/response/candidate_question.rb +51 -0
  20. data/lib/barker/api/response/error.rb +17 -0
  21. data/lib/barker/api/response/game_show.rb +43 -0
  22. data/lib/barker/api/response/round.rb +21 -0
  23. data/lib/barker/api/response.rb +15 -0
  24. data/lib/barker/api.rb +7 -0
  25. data/lib/barker/candidate.rb +52 -0
  26. data/lib/barker/candidate_answer.rb +42 -0
  27. data/lib/barker/candidate_joker.rb +30 -0
  28. data/lib/barker/candidate_question.rb +116 -0
  29. data/lib/barker/candidate_round.rb +32 -0
  30. data/lib/barker/entity.rb +15 -0
  31. data/lib/barker/game_show.rb +176 -0
  32. data/lib/barker/guideline/base.rb +33 -0
  33. data/lib/barker/guideline/llmcquiz/questions.yaml +300 -0
  34. data/lib/barker/guideline/llmcquiz.rb +28 -0
  35. data/lib/barker/guideline/mock.rb +17 -0
  36. data/lib/barker/guideline.rb +8 -0
  37. data/lib/barker/import/yaml.rb +22 -0
  38. data/lib/barker/import.rb +6 -0
  39. data/lib/barker/joker.rb +28 -0
  40. data/lib/barker/question.rb +12 -0
  41. data/lib/barker/quiz.rb +19 -0
  42. data/lib/barker/repo/memory/game_show.rb +38 -0
  43. data/lib/barker/repo/memory.rb +8 -0
  44. data/lib/barker/repo.rb +6 -0
  45. data/lib/barker/round.rb +68 -0
  46. data/lib/barker/stage.rb +38 -0
  47. data/lib/barker/store/memory.rb +158 -0
  48. data/lib/barker/store.rb +6 -0
  49. data/lib/barker/validations.rb +14 -0
  50. data/lib/barker/version.rb +3 -0
  51. data/lib/barker.rb +45 -0
  52. data/test/factories/answer.rb +21 -0
  53. data/test/factories/candidate.rb +23 -0
  54. data/test/factories/candidate_answer.rb +12 -0
  55. data/test/factories/candidate_joker.rb +8 -0
  56. data/test/factories/candidate_question.rb +16 -0
  57. data/test/factories/candidate_round.rb +8 -0
  58. data/test/factories/game_show.rb +12 -0
  59. data/test/factories/joker.rb +5 -0
  60. data/test/factories/question.rb +13 -0
  61. data/test/factories/quiz.rb +11 -0
  62. data/test/factories/round.rb +12 -0
  63. data/test/factories/stage.rb +12 -0
  64. data/test/fixtures/questions.yaml +20 -0
  65. data/test/test_helper.rb +72 -0
  66. data/test/test_mocks.rb +21 -0
  67. data/test/unit/answer_test.rb +49 -0
  68. data/test/unit/api/base_test.rb +207 -0
  69. data/test/unit/api/response/base_test.rb +35 -0
  70. data/test/unit/api/response/candidate_answer_test.rb +64 -0
  71. data/test/unit/api/response/candidate_joker_test.rb +60 -0
  72. data/test/unit/api/response/candidate_question_test.rb +99 -0
  73. data/test/unit/api/response/candidate_test.rb +82 -0
  74. data/test/unit/api/response/game_show_test.rb +105 -0
  75. data/test/unit/api/response/round_test.rb +49 -0
  76. data/test/unit/candidate_answer_test.rb +66 -0
  77. data/test/unit/candidate_question_test.rb +277 -0
  78. data/test/unit/candidate_round_test.rb +64 -0
  79. data/test/unit/candidate_test.rb +76 -0
  80. data/test/unit/game_show_test.rb +372 -0
  81. data/test/unit/guideline/base_test.rb +34 -0
  82. data/test/unit/guideline/llmcquiz_test.rb +35 -0
  83. data/test/unit/guideline/mock_test.rb +33 -0
  84. data/test/unit/import/yaml_test.rb +41 -0
  85. data/test/unit/joker_test.rb +32 -0
  86. data/test/unit/question_test.rb +55 -0
  87. data/test/unit/quiz_test.rb +37 -0
  88. data/test/unit/repo/memory/game_show_test.rb +54 -0
  89. data/test/unit/round_test.rb +167 -0
  90. data/test/unit/stage_test.rb +53 -0
  91. metadata +283 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 984796895fe9bc71b1d2ce82ea679d7fe75f78ae
4
+ data.tar.gz: c987d3c1af4e2662bfd0b416610bfcb24426bc0f
5
+ SHA512:
6
+ metadata.gz: 3c0da2c756ae07411f8a08151a6e733f88aedddd82d4015746cff8c08276a784da35769191fcf54b63ce69bb247384c9301fcb080d3798db22db6872d2206f9e
7
+ data.tar.gz: 64de935ceff6389621d30888bc389e4520f0212f800f42975cd395f35dad9d8726a638ed943b51d72f6aaf3a27ffca256d6912eaa3b3e5d66485ce0a2e5194ab
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.swp
2
+ pkg/
3
+ tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ barker
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/.simplecov ADDED
@@ -0,0 +1,7 @@
1
+ require 'simplecov-rcov'
2
+
3
+ SimpleCov.coverage_dir "tmp/coverage"
4
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter if ENV['CI']
5
+ SimpleCov.start do
6
+ add_filter '/test/'
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'simplecov', '~> 0.6', :require => false, :group => :test
6
+ gem 'simplecov-rcov', '~> 0.2', :require => false, :group => :test
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ barker (0.2.2)
5
+ activemodel
6
+ activesupport
7
+ thread_safe
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activemodel (3.2.13)
13
+ activesupport (= 3.2.13)
14
+ builder (~> 3.0.0)
15
+ activesupport (3.2.13)
16
+ i18n (= 0.6.1)
17
+ multi_json (~> 1.0)
18
+ atomic (1.1.10)
19
+ builder (3.0.4)
20
+ factory_girl (4.2.0)
21
+ activesupport (>= 3.0.0)
22
+ i18n (0.6.1)
23
+ metaclass (0.0.1)
24
+ minitest (5.0.2)
25
+ mocha (0.14.0)
26
+ metaclass (~> 0.0.1)
27
+ multi_json (1.7.3)
28
+ rack (1.5.2)
29
+ rake (10.0.4)
30
+ simplecov (0.7.1)
31
+ multi_json (~> 1.0)
32
+ simplecov-html (~> 0.7.1)
33
+ simplecov-html (0.7.1)
34
+ simplecov-rcov (0.2.3)
35
+ simplecov (>= 0.4.1)
36
+ thread_safe (0.1.0)
37
+ atomic
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ barker!
44
+ factory_girl
45
+ minitest
46
+ mocha
47
+ rack
48
+ rake
49
+ simplecov (~> 0.6)
50
+ simplecov-rcov (~> 0.2)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jan Owiesniak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # barker
2
+
3
+ Barker game show engine.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'barker'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install barker
18
+
19
+ ## Usage
20
+
21
+ ### Guidelines
22
+
23
+ Barker let you define project specific guidelines that will be called during the game show life cycle.
24
+ The default behaviour is implemented in Barker::Guideline::Base.
25
+
26
+ Example of a project specific guideline.
27
+
28
+ ```ruby
29
+ module Barker
30
+ module Guideline
31
+
32
+ class Llmcquiz < Base
33
+ include Errors
34
+
35
+ def quizzes
36
+ [Quiz.new(questions.sample(5))]
37
+ end
38
+
39
+ def jokers
40
+ [:fifty_fifty]
41
+ end
42
+
43
+ def questions
44
+ Barker::Import::Yaml.process(path)
45
+ end
46
+
47
+ def round_time
48
+ 20
49
+ end
50
+
51
+ def path
52
+ File.expand_path "#{File.dirname(__FILE__)}/../../../lib/barker/guideline/llmcquiz/questions.yaml"
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+ ```
59
+
60
+ ### Api
61
+
62
+ Only interact with the a api instance.
63
+ Api calls always return a instance of Barker::Api::Response.
64
+
65
+ Barker exceptions will not bubble up the stacktrace instead they will be caught in response.error.
66
+ You can check for errors with the response.ok? method.
67
+
68
+ ```ruby
69
+ class Response < OpenStruct
70
+ def ok?
71
+ !error
72
+ end
73
+ end
74
+ ```
75
+
76
+ #### Create a new game show
77
+
78
+ The game show will automatically be stored in a memory store.
79
+
80
+ ```ruby
81
+ api = Barker::Api::Base.new
82
+ api.create # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=nil, started?=false, running?=false, finished?=false, aborted?=false, jokers=[], stage=-1, round=-1>
83
+ ```
84
+
85
+ You can pass a optional guideline
86
+
87
+ ```ruby
88
+ api.create(:guideline => :llmcquiz) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=nil, started?=false, running?=false, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
89
+ ```
90
+
91
+ #### Add or remove candidates
92
+
93
+ ```ruby
94
+ api = Barker::Api::Base.new
95
+ game_show_id = api.create.game_show_id
96
+
97
+ api.candidates(game_show_id) # => []
98
+ api.join(game_show_id, 42, "de") # => <Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:44:23 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>
99
+ api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:44:51 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>]
100
+ api.leave(game_show_id, candidate_id) => #<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:45:46 +0200, leaved_at=2013-06-11 18:45:46 +0200, aborted?=false, joined?=true, leaved?=true, jokers=[]>
101
+ api.candidates(game_show_id, candidate_id) => [#<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:45:46 +0200, leaved_at=2013-06-11 18:45:46 +0200, aborted?=false, joined?=true, leaved?=true, jokers=[]>]
102
+ ```
103
+
104
+ #### Control
105
+
106
+ ##### Start
107
+
108
+ ```ruby
109
+ api = Barker::Api::Base.new
110
+ game_show_id = api.create.game_show_id
111
+
112
+ api.start(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=2013-06-11 18:51:41 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
113
+ api.abort(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=2013-06-11 18:51:41 +0200, started?=false, running?=false, finished?=false, aborted?=true, jokers=[:fifty_fifty], stage=-1, round=-1>
114
+ api.finish(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=2013-06-11 18:51:41 +0200, aborted_at=nil, started?=false, running?=false, finished?=true, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
115
+ ```
116
+
117
+ ##### Abort
118
+
119
+ ```ruby
120
+ api = Barker::Api::Base.new
121
+ game_show_id = api.create.game_show_id
122
+
123
+ api.abort(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=2013-06-11 18:51:41 +0200, started?=false, running?=false, finished?=false, aborted?=true, jokers=[:fifty_fifty], stage=-1, round=-1>
124
+ ```
125
+
126
+ ##### Finish
127
+
128
+ ```ruby
129
+ api = Barker::Api::Base.new
130
+ game_show_id = api.create.game_show_id
131
+
132
+ api.finish(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=2013-06-11 18:51:41 +0200, aborted_at=nil, started?=false, running?=false, finished?=true, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
133
+ ```
134
+
135
+ ##### Ask
136
+
137
+ ```ruby
138
+ api = Barker::Api::Base.new
139
+ game_show_id = api.create.game_show_id
140
+
141
+ api.join(game_show_id, 42, "de")
142
+
143
+ api.start(game_show_id)
144
+ api.next_stage(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[42], started_at=2013-06-11 19:00:20 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=0, round=-1>}
145
+ api.next_round(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[42], started_at=2013-06-11 19:00:20 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=0, round=0>}
146
+
147
+ api.ask(game_show_id, 42) # => <Barker::Api::Response question_id=9, candidate_id=42, label="frage", state=:asked, asked_at=2013-06-11 19:02:52 +0200, answered_at=nil, asked?=true, answered?=false, open?=false, answers=[#<Barker::Api::Response answer_id=21, label="antwort", correct?=false, revealed?=false>, #<Barker::Api::Response answer_id=22, label="antwort", correct?=true, revealed?=false>
148
+ ```
149
+
150
+ ##### Answer
151
+
152
+ ```ruby
153
+ api = Barker::Api::Base.new
154
+ game_show_id = api.create.game_show_id
155
+
156
+ api.join(game_show_id, 42, "de")
157
+
158
+ api.start(game_show_id)
159
+ api.next_stage(game_show_id)
160
+ api.next_round(game_show_id)
161
+ api.ask(game_show_id, 42)
162
+
163
+ api.answer(game_show_id, 42, 25) # => <Barker::Api::Response answer_id=25, label="antwort", correct?=false, revealed?=false>
164
+ api.given_answer(game_show_id, 25) # => <Barker::Api::Response answer_id=25, label="antwort", correct?=false, revealed?=false>
165
+ ```
166
+
167
+ ##### Joker
168
+
169
+ ```ruby
170
+ api = Barker::Api::Base.new
171
+ game_show_id = api.create.game_show_id
172
+
173
+ api.join(game_show_id, 42, "de")
174
+
175
+ api.start(game_show_id)
176
+ api.next_stage(game_show_id)
177
+ api.next_round(game_show_id)
178
+ api.ask(game_show_id, 42)
179
+
180
+ api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 19:08:44 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>]
181
+ api.joker(game_show_id) # => <Barker::Api::Response joker_id="fifty_fifty">
182
+ api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 19:08:44 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[:fifty_fifty]>]
183
+ ```
184
+
185
+ ### Store
186
+
187
+ For now barker only support a memory store / repo.
188
+ ActiveRecord will be supported sooner or later.
189
+
190
+ ### How to use in Rails
191
+
192
+ Here is a very early concept how it could be used in a rails app.
193
+
194
+ http://gitlab.neopoly.de/jo/barker/issues/19#note_23
195
+
196
+ TODO: Add example with real api.
197
+
198
+ ## Contributing
199
+
200
+ 1. Fork it
201
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
202
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
203
+ 4. Push to the branch (`git push origin my-new-feature`)
204
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "test"
10
+ t.test_files = FileList['test/**/*test.rb']
11
+ t.verbose = true
12
+ end
data/barker.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'barker/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["Jan Owiesniak"]
8
+ gem.email = ["jo@neopoly.de"]
9
+ gem.description = %q{barker}
10
+ gem.summary = %q{barker}
11
+ gem.homepage = ""
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "barker"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Barker::VERSION
19
+
20
+ gem.add_development_dependency "rake"
21
+ gem.add_development_dependency "rack"
22
+ gem.add_development_dependency "minitest"
23
+ gem.add_development_dependency "factory_girl"
24
+ gem.add_development_dependency "mocha"
25
+
26
+ gem.add_dependency 'activesupport'
27
+ gem.add_dependency 'activemodel'
28
+ gem.add_dependency "thread_safe"
29
+ end
data/ci/run ADDED
@@ -0,0 +1,20 @@
1
+ #!/bin/bash
2
+
3
+ if [ "$WORKSPACE" = "" -o "$JOB_NAME" = "" ]; then
4
+ echo "\$WORKSPACE or \$JOB_NAME missing"
5
+ exit 1
6
+ fi
7
+
8
+ CI_DIR="$WORKSPACE/ci"
9
+ ROOT_DIR="$CI_DIR/.."
10
+
11
+ export CI=1
12
+ export PROJECT="$JOB_NAME"
13
+ export COVERAGE=1
14
+
15
+ # RVM
16
+ source "$HOME/.rvm/scripts/rvm"
17
+ rvm use $(cat $ROOT_DIR/.ruby-version)@$(cat $ROOT_DIR/.ruby-gemset)
18
+
19
+ # run
20
+ bundle && bundle exec rake test
@@ -0,0 +1,11 @@
1
+ module Barker
2
+ class Answer < Entity.new(:id, :label, :correct)
3
+
4
+ include Barker::Validations::NumericalityOfId
5
+
6
+ def correct?
7
+ correct
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,145 @@
1
+ module Barker
2
+ module Api
3
+ class Base
4
+
5
+ include Errors
6
+
7
+ def create(options = {})
8
+ catch_error do
9
+ game_show = Barker::GameShow.new([], [], :guideline => options[:guideline])
10
+ repo.add game_show
11
+ build_game_show(game_show)
12
+ end
13
+ end
14
+
15
+ def get(game_show_id)
16
+ catch_error do
17
+ build_game_show repo.get(game_show_id)
18
+ end
19
+ end
20
+
21
+ def remove(game_show_id)
22
+ catch_error do
23
+ build_game_show repo.remove(repo.get(game_show_id))
24
+ end
25
+ end
26
+
27
+ def join(game_show_id, candidate_id, candidate_locale)
28
+ catch_error do
29
+ candidate = Barker::Candidate.new(OpenStruct.new(:id => candidate_id, :locale => candidate_locale))
30
+ repo.around(game_show_id) { |game_show| game_show.join(candidate) }
31
+ build_candidate(candidate)
32
+ end
33
+ end
34
+
35
+ def leave(game_show_id, candidate_id)
36
+ catch_error do
37
+ game_show = repo.get game_show_id
38
+ candidate = game_show.leave(candidate_id)
39
+ repo.update game_show
40
+ build_candidate(candidate)
41
+ end
42
+ end
43
+
44
+ def start(game_show_id)
45
+ catch_error do
46
+ build_game_show repo.around(game_show_id) { |game_show| game_show.start }
47
+ end
48
+ end
49
+
50
+ def raw(game_show_id)
51
+ catch_error do
52
+ repo.get(game_show_id)
53
+ end
54
+ end
55
+
56
+ def finish(game_show_id)
57
+ catch_error do
58
+ build_game_show repo.around(game_show_id) { |game_show| game_show.finish }
59
+ end
60
+ end
61
+
62
+ def abort(game_show_id)
63
+ catch_error do
64
+ build_game_show repo.around(game_show_id) { |game_show| game_show.abort }
65
+ end
66
+ end
67
+
68
+ def next_stage(game_show_id)
69
+ catch_error do
70
+ build_game_show repo.around(game_show_id) { |game_show| game_show.next_stage }
71
+ end
72
+ end
73
+
74
+ def next_round(game_show_id)
75
+ catch_error do
76
+ build_game_show repo.around(game_show_id) { |game_show| game_show.next_round }
77
+ end
78
+ end
79
+
80
+ def ask(game_show_id, candidate_id, options = {})
81
+ catch_error do
82
+ game_show = repo.get game_show_id
83
+ candidate_question = game_show.ask(candidate_id, options)
84
+ repo.update game_show
85
+ build_candidate_question(candidate_question)
86
+ end
87
+ end
88
+
89
+ def answer(game_show_id, candidate_id, answer_id)
90
+ catch_error do
91
+ game_show = repo.get game_show_id
92
+ given_answer = game_show.answer(candidate_id, answer_id)
93
+ repo.update game_show
94
+ build_candidate_answer(given_answer)
95
+ end
96
+ end
97
+
98
+ def joker(game_show_id, candidate_id, joker_id)
99
+ catch_error do
100
+ game_show = repo.get game_show_id
101
+ candidate_question = game_show.ask(candidate_id)
102
+ candidate_joker = game_show.joker(candidate_id, joker_id)
103
+ repo.update game_show
104
+ build_candidate_joker(candidate_joker)
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def catch_error
111
+ begin
112
+ yield
113
+ rescue Barker::Errors::BarkerError => e
114
+ Response::Error.new(:error => e.class)
115
+ end
116
+ end
117
+
118
+ def repo
119
+ @repo ||= Barker::Repo::Memory::GameShow.new
120
+ end
121
+
122
+ def build_candidate_question(candidate_question)
123
+ Response::CandidateQuestion.new(:candidate_question => candidate_question)
124
+ end
125
+
126
+ def build_candidate_answer(candidate_answer)
127
+ Response::CandidateAnswer.new(:candidate_answer => candidate_answer)
128
+ end
129
+
130
+ def build_candidate(candidate)
131
+ Response::Candidate.new(:candidate => candidate)
132
+ end
133
+
134
+ def build_candidate_joker(candidate_joker)
135
+ Response::CandidateJoker.new(:candidate_joker => candidate_joker)
136
+ end
137
+
138
+ def build_game_show(game_show)
139
+ Response::GameShow.new(:game_show => game_show)
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+ end
@@ -0,0 +1,25 @@
1
+ require 'forwardable'
2
+
3
+ module Barker
4
+ module Api
5
+ module Response
6
+
7
+ class Base < OpenStruct
8
+ def error?
9
+ !!error
10
+ end
11
+
12
+ def ok?
13
+ !error?
14
+ end
15
+
16
+ private
17
+
18
+ def to_i(attribute)
19
+ attribute && attribute.to_i
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class Candidate < Base
5
+
6
+ extend Forwardable
7
+ def_delegators :candidate, :id, :candidate_id, :locale, :joined_at, :leaved_at, :aborted_at, :joined?, :leaved?, :aborted?
8
+
9
+ def jokers
10
+ candidate.jokers.map {|candidate_joker| Response::CandidateJoker.new(:candidate_joker => candidate_joker)}
11
+ end
12
+
13
+ def to_hash
14
+ {
15
+ 'id' => id,
16
+ 'candidate_id' => candidate_id,
17
+ 'locale' => locale,
18
+ 'jokers' => jokers? && jokers.map(&:to_hash) || [],
19
+ 'joker_ids' => jokers? && jokers.map(&:joker_id) || [],
20
+ 'joined_at' => to_i(joined_at),
21
+ 'leaved_at' => to_i(leaved_at),
22
+ 'aborted_at' => to_i(aborted_at),
23
+ 'joined?' => joined?,
24
+ 'leaved?' => leaved?,
25
+ 'aborted?' => aborted?
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ def jokers?
32
+ jokers && jokers.map(&:candidate_joker).any?
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class CandidateAnswer < Base
5
+
6
+ extend Forwardable
7
+ def_delegators :candidate_answer, :id, :answer_id, :candidate_id, :label, :revealed?, :correct?
8
+
9
+ def to_hash
10
+ {
11
+ 'id' => id,
12
+ 'answer_id' => answer_id,
13
+ 'candidate_id' => candidate_id,
14
+ 'label' => label,
15
+ 'revealed?' => revealed?,
16
+ 'correct?' => correct?
17
+ }
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Barker
2
+ module Api
3
+ module Response
4
+ class CandidateJoker < Base
5
+
6
+ extend Forwardable
7
+ def_delegators :candidate_joker, :id, :joker_id, :candidate_id, :used_at
8
+
9
+ def to_hash
10
+ {
11
+ 'id' => id,
12
+ 'joker_id' => joker_id,
13
+ 'candidate_id' => candidate_id,
14
+ 'used_at' => to_i(used_at)
15
+ }
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end