konamio 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37ffc53d7fc6b46bcbb18189e27cc170152dc44c
4
+ data.tar.gz: 924d388a7c51e926ffecb6ffbc8058326ac7eca9
5
+ SHA512:
6
+ metadata.gz: 7bb9c6a000e52adea2c05c8f8c75a0e40350e35bce0fb650222260bb7805782337acef371cf2601c902e780ff3f9bae0bc3598c59341ba3da3091dbca549cae5
7
+ data.tar.gz: 889017b85fd30f1e188af487f320c4660b8cbcc413bbc8f3c9c8478dccdfb0c3e7b8e8bbf144598f33fa204cd90496de4036cb27ac16958e7077ff6840f645ae
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ script: bundle exec rake test
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## Konamio
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/konamio.png)](http://badge.fury.io/rb/konamio) [![Build Status](https://travis-ci.org/rthbound/konamio.png?branch=master)](https://travis-ci.org/rthbound/konamio) [![Coverage Status](https://coveralls.io/repos/rthbound/konamio/badge.png)](https://coveralls.io/r/rthbound/konamio) [![Code Climate](https://codeclimate.com/github/rthbound/konamio.png)](https://codeclimate.com/github/rthbound/konamio)
4
+
5
+
3
6
  ![Konami Code](http://images.nintendolife.com/news/2012/01/the_origins_of_the_konami_code_revealed/attachment/0/small.jpg)
4
7
 
5
8
  gem install konamio
@@ -9,18 +12,23 @@ and
9
12
  require "konamio"
10
13
 
11
14
 
12
- Default Usage:
15
+ ### Default Usage
13
16
  ```
14
17
  > Konamio::Sequence::Requisition.new.execute!
15
18
  Enter konami code (or hit escape)
16
19
  Good job, you.
17
- => #<PayDirt::Result:0x937ddb4
20
+ => #<Konamio::Result:0x937ddb4
18
21
  @data={:data=>{:confirmation=>"Good job, you."}},
19
22
  @success=true>
20
23
  ```
21
24
 
22
- You can configure it to listen for any sequence you want:
23
- ```
25
+ ### Configuration
26
+
27
+ #### Sequences
28
+
29
+ You can configure it to listen for any ascii based sequence you want. This can be specified using a string (`"foobar"`) or an array of recognized symbols and one character strings (escaped characters are okay, e.g. `[:up, "1", "2", "3", "\t"]`):
30
+
31
+ ```ruby
24
32
  > Konamio::Sequence::Requisition.new({
25
33
  sequence: "a".upto("z").to_a.reverse,
26
34
  prompt: "Say the alphabet backwards",
@@ -28,18 +36,31 @@ You can configure it to listen for any sequence you want:
28
36
  }).execute!
29
37
  Say the alphabet backwards
30
38
  Okay, you can go
31
- => #<PayDirt::Result:0x9265788
39
+ => #<Konamio::Result:0x9265788
32
40
  @data={:data=>{:confirmation=>"Okay, you can go"}},
33
41
  @success=true>
34
42
  ```
35
43
 
36
- You can also specify a block of code to be executed when the sequence is received successfully.
44
+ #### Output
45
+
46
+ There are three dialogs that Konamio might send to standard out.
47
+
48
+ 1. `:prompt` is the dialog displayed initially, and each time the user fails to supply the proper sequence.
49
+ 2. `:confirmation` is displayed when the required sequence is entered properly.
50
+ 3. `:cancellation` is displayed when user terminates by pressing the escape key.
51
+
52
+ You can customize any of these dialogs, or disable them individually by passing a falsey value.
53
+
54
+ #### Success!
55
+
56
+ It would be boring if all Konamio did was return a result object. In fact, `Konamio::Sequence::Requisition#execute!` takes a block, and will execute that block when the sequence has been successfully entered. You're limited only by your imagination and the context of your application.
57
+
37
58
  The following code would prompt the user to enter the konami code twice:
38
- ```
59
+ ```ruby
39
60
  Konamio::Sequence::Requisition.new.execute! { Konamio::Sequence::Requisition.new.execute! }
40
61
  ```
41
62
  This would give you +30 lives:
42
- ```
63
+ ```ruby
43
64
  Konamio::Sequence::Requisition.new.execute! { 30.times { puts "+1up" } }
44
65
  ```
45
66
 
@@ -53,10 +74,7 @@ Konamio::Sequence::Requisition.new.execute! { 30.times { puts "+1up" } }
53
74
 
54
75
  ```ruby
55
76
  require "konamio"
56
- Konamio::Sequence::Requisition.new.execute!
77
+ Konamio::Sequence::Requisition.new(prompt: false, confirmation: false, cancellation: false).execute!
57
78
  ```
58
-
59
- to require console users to enter the konami code, or to require some other password:
60
79
 
61
- require "konamio"
62
- Konamio::Sequence::Requisition.new(sequence: "foobar").execute!
80
+ to require console users to enter the konami code. This funny trick obviously does not provide any real protection, but it would certainly make for a nasty practical joke.
data/konamio.gemspec CHANGED
@@ -7,16 +7,17 @@ Gem::Specification.new do |s|
7
7
  s.version = Konamio::VERSION
8
8
  s.authors = ["Tad Hosford"]
9
9
  s.email = ["tad.hosford@gmail.com"]
10
- s.homepage = "https://github.com/rthbound/konamio"
11
- s.summary = "I'm gonna recognize the konami sequence"
12
- s.description = "Recognize sequences in Ruby... just for fun."
10
+ s.homepage = "https://rthbound.github.io/konamio"
11
+ s.summary = "The Ruby Konami code gem."
12
+ s.description = "Tell it which sequence to listen for and what to do when the sequence is received."
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.test_files = `git ls-files -- {test}/*`.split("\n")
16
16
  s.require_paths = ["lib"]
17
17
 
18
- s.add_runtime_dependency "pay_dirt"
18
+ s.add_runtime_dependency "pay_dirt", "1.0.0"
19
19
  s.add_development_dependency "minitest"
20
20
  s.add_development_dependency "pry"
21
21
  s.add_development_dependency "rake"
22
+ s.add_development_dependency "coveralls"
22
23
  end
@@ -0,0 +1,5 @@
1
+ module Konamio
2
+ # A basic service object
3
+ class Base < PayDirt::Base
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Konamio
2
- class Prompt < PayDirt::Base
2
+ class Prompt < Konamio::Base
3
3
  def initialize(options)
4
4
  options = {
5
5
  output: $stdout
@@ -9,8 +9,8 @@ module Konamio
9
9
  end
10
10
 
11
11
  def execute!
12
- @output.puts @prompt
13
- return PayDirt::Result.new(success: true, data: { prompted: @prompt })
12
+ prompt
13
+ return Konamio::Result.new(success: true, data: { prompted: @prompt })
14
14
  end
15
15
 
16
16
  def prompt
@@ -0,0 +1,5 @@
1
+ module Konamio
2
+ # The object all my service objects will return
3
+ class Result < PayDirt::Result
4
+ end
5
+ end
@@ -1,6 +1,6 @@
1
1
  module Konamio
2
2
  module Sequence
3
- class Listener < PayDirt::Base
3
+ class Listener < Konamio::Base
4
4
  include Konamio::KeyMap
5
5
 
6
6
  def initialize(options)
@@ -11,13 +11,11 @@ module Konamio
11
11
  def execute!
12
12
  case listen
13
13
  when true
14
- return PayDirt::Result.new(success: true, data: { sequence: @sequence[1..-1]})
14
+ return Konamio::Result.new(success: true, data: { sequence: @sequence[1..-1]})
15
15
  when false
16
- return PayDirt::Result.new(success: false, data: { sequence: @sequence })
16
+ return Konamio::Result.new(success: false, data: { sequence: @sequence })
17
17
  when :negative
18
- return PayDirt::Result.new(success: false, data: { sequence: :negative })
19
- else
20
- return PayDirt::Result.new(success: false, data: { sequence: @sequence })
18
+ return Konamio::Result.new(success: false, data: { sequence: :negative })
21
19
  end
22
20
  end
23
21
 
@@ -1,15 +1,21 @@
1
1
  module Konamio
2
2
  module Sequence
3
- class Requisition < PayDirt::Base
3
+ class Requisition < Konamio::Base
4
4
  include Konamio::KeyMap
5
+ # @param [Hash] options The group of options for the sequence requisition
6
+ # @option options [Class] :speaker (Konamio::Prompt) You could theoretically replace this with a class that responds to #new with an instance that responds to #execute! Not sure why you would.
7
+ # @option options [String] :prompt ("Enter konami code (or hit escape)") override with a falsey value to skip prompt dialog
8
+ # @option options [String] :confirmation ("Enter konami code (or hit escape)") override with a falsey value to skip confirmation dialog
9
+ # @option options [String] :cancellation ("Goodbye!") override with a falsey value to skip cancellation dialog
10
+ # @option options [Array, String] :sequence ([:up,:up,:down,:down,:left,:right,:left,:right,"B","A"])
5
11
  def initialize(options={})
6
12
  options = {
7
- output: $stdout,
8
13
  input: $stdin,
9
- speaker: Konamio::Prompt,
14
+ output: $stdout,
10
15
  listener: Konamio::Sequence::Listener,
11
- sequence: [:up,:up,:down,:down,:left,:right,:left,:right,"B","A"],
16
+ speaker: Konamio::Prompt,
12
17
  prompt: "Enter konami code (or hit escape)",
18
+ sequence: [:up,:up,:down,:down,:left,:right,:left,:right,"B","A"],
13
19
  confirmation: "Good job, you.",
14
20
  cancellation: "Goodbye!"
15
21
  }.merge(options)
@@ -17,23 +23,39 @@ module Konamio
17
23
  load_options(:sequence, options)
18
24
  end
19
25
 
26
+ # @api public
27
+ # @yieldreturn The result of a block, if supplied
28
+ # @return Konamio::Result
20
29
  def execute! &block
21
30
  prompt
31
+
22
32
  result = listen(@sequence)
23
33
  yield if block_given? && result.successful?
24
- return result
34
+
35
+ result
25
36
  end
26
37
 
27
- def prompt(prompt = @prompt)
28
- @speaker.new(prompt: prompt, output: @output).execute!
38
+ private
39
+ # @api private
40
+ # @param prompt
41
+ # @return [Konamio::Result] unless running silently
42
+ # @return [Boolean] false if running silently
43
+ def prompt(prompt = @prompt, speaker = @speaker)
44
+ !!prompt && speaker.new(prompt: prompt, output: @output).execute!
29
45
  end
30
46
 
47
+ # @api private
48
+ # @param [String, Array<String,Symbol>] sequence The sequence you want to require. When providing an array, each element must be a recognized symbol or the string representation of an ascii character.
49
+ # @return [Konamio::Result]
31
50
  def listen(sequence)
32
51
  listener = @listener.new(sequence: sequence, input: @input)
33
52
  received = listener.execute!
34
53
  signal = received.data[:sequence]
35
54
 
36
- prompt(@confirmation) and return result(true, data: { confirmation: @confirmation }) if signal.empty?
55
+ if signal.empty?
56
+ prompt(@confirmation)
57
+ return result true, data: { confirmation: @confirmation }
58
+ end
37
59
 
38
60
  case signal
39
61
  when :negative
@@ -43,28 +65,17 @@ module Konamio
43
65
  prompt
44
66
  listen(@sequence)
45
67
  when sequence[1..-1]
46
- listen received.data[:sequence]
47
- else
48
- result(false, data: { confirmation: "Unexpected termination" })
68
+ listen signal
49
69
  end
50
- #if received.successful?
51
- # if received.data[:sequence].empty?
52
- # prompt(@confirmation)
53
- # result(true, data: { confirmation: @confirmation })
54
- # else
55
- # listen(received.data[:sequence])
56
- # end
57
- #elsif received.data[:terminate]
58
- # prompt("Goodbye!")
59
- # result(false, data: { confirmation: :negative })
60
- #else
61
- # prompt
62
- # listen(@sequence)
63
- #end
64
70
  end
65
71
 
72
+ # @api private
73
+ # @param [Boolean] success whether the expected sequence was received.
74
+ # @param [Hash] ({}) data
75
+ # @option data [String, Boolean] :confirmation A message about how the program exited.
76
+ # @return [Konamio::Result]
66
77
  def result(success, data={})
67
- PayDirt::Result.new(success: success, data: data)
78
+ Konamio::Result.new(success: success, data: data)
68
79
  end
69
80
  end
70
81
  end
@@ -1,3 +1,3 @@
1
1
  module Konamio
2
- VERSION = "0.2.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/konamio.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require "pay_dirt"
2
- require "io/console"
2
+ require_relative 'konamio/base'
3
+ require_relative 'konamio/result'
3
4
 
5
+ require "io/console"
4
6
  require_relative 'konamio/key_map'
5
7
  require_relative 'konamio/sequence/listener'
6
8
  require_relative 'konamio/prompt'
@@ -27,4 +27,12 @@ describe Konamio::Sequence::Listener do
27
27
  assert @input.verify
28
28
  assert !result.successful?
29
29
  end
30
+
31
+ it "lets you escape" do
32
+ @input.expect(:getch, "\e")
33
+ @input.expect(:getch, "")
34
+ @input.expect(:getch, "")
35
+ result = @subject.new(sequence: @sequence, input: @input).execute!
36
+ assert !result.successful?
37
+ end
30
38
  end
@@ -0,0 +1,42 @@
1
+ require "minitest_helper"
2
+
3
+ describe Konamio::Sequence::Requisition do
4
+ before do
5
+ @subject = Konamio::Sequence::Requisition
6
+ @options = {
7
+ input: @input = MiniTest::Mock.new,
8
+ output: @output = MiniTest::Mock.new,
9
+ prompt: @prompt = false,
10
+ listener: @listener = MiniTest::Mock.new,
11
+ speaker: @speaker = MiniTest::Mock.new,
12
+ sequence: @sequence = ["a"],
13
+ confirmation: @confirmation = false,
14
+ cancellation: false
15
+ }
16
+
17
+ # We will call new on @listener and return
18
+ @listener_instance = MiniTest::Mock.new
19
+ @listener.expect(:new, @listener_instance, [{ sequence: @sequence, input: @input }])
20
+
21
+ # We will call execute! on the @listener_instance and return
22
+ @listener_result = MiniTest::Mock.new
23
+ @listener_instance.expect(:execute!, @listener_result)
24
+
25
+ # The @listener_result contains some data
26
+ @data_hash = MiniTest::Mock.new
27
+ @listener_result.expect(:data, @data_hash)
28
+ @data_hash.expect(:[], :negative, [:sequence])
29
+ end
30
+
31
+ it "can be initialized" do
32
+ @subject.must_respond_to :new
33
+ end
34
+
35
+ it "must execute" do
36
+ @subject.new(@options).must_respond_to :execute!
37
+ end
38
+
39
+ it "can be cancelled" do
40
+ assert !@subject.new(@options).execute!.successful?
41
+ end
42
+ end
@@ -16,16 +16,24 @@ describe Konamio::Sequence::Requisition do
16
16
  @speaker_instance = MiniTest::Mock.new
17
17
  @speaker_result = MiniTest::Mock.new
18
18
  @speaker_instance.expect(:execute!, @speaker_result)
19
+ @speaker_instance.expect(:execute!, @speaker_result)
19
20
 
20
21
  @listener_instance = MiniTest::Mock.new
21
22
  @listener_result = MiniTest::Mock.new
22
23
  @listener_instance.expect(:execute!, @listener_result)
24
+ @listener_instance.expect(:execute!, @listener_result)
25
+
26
+ @speaker.expect(:new, @speaker_instance, [{ prompt: @prompt, output: @output }])
27
+ @listener.expect(:new, @listener_instance, [{ sequence: @sequence, input: @input }])
28
+ @data_hash = MiniTest::Mock.new
29
+ @listener_result.expect(:data, @data_hash)
30
+ @data_hash.expect(:[], ["a"], [:sequence])
23
31
 
24
32
  @speaker.expect(:new, @speaker_instance, [{ prompt: @prompt, output: @output }])
25
33
  @listener.expect(:new, @listener_instance, [{ sequence: @sequence, input: @input }])
26
34
  @data_hash = MiniTest::Mock.new
27
35
  @listener_result.expect(:data, @data_hash)
28
- @data_hash.expect(:[], "", [:sequence])
36
+ @data_hash.expect(:[], [], [:sequence])
29
37
 
30
38
  @speaker_confirmation_instance = MiniTest::Mock.new
31
39
  @speaker_confirmation_result = MiniTest::Mock.new
@@ -1,7 +1,8 @@
1
1
  # Testing frameworks
2
- require "minitest/spec"
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
3
5
  require "minitest/autorun"
4
- require "minitest/mock"
5
6
 
6
7
  # Debugger
7
8
  require "pry"
metadata CHANGED
@@ -1,81 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konamio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tad Hosford
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-15 00:00:00.000000000 Z
11
+ date: 2013-06-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: pay_dirt
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: 1.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '='
28
25
  - !ruby/object:Gem::Version
29
- version: '0'
26
+ version: 1.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: minitest
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: pry
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
68
74
  - !ruby/object:Gem::Version
69
75
  version: '0'
70
76
  type: :development
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
- - - ! '>='
80
+ - - '>='
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
- description: Recognize sequences in Ruby... just for fun.
83
+ description: Tell it which sequence to listen for and what to do when the sequence
84
+ is received.
79
85
  email:
80
86
  - tad.hosford@gmail.com
81
87
  executables: []
@@ -83,50 +89,47 @@ extensions: []
83
89
  extra_rdoc_files: []
84
90
  files:
85
91
  - .gitignore
92
+ - .travis.yml
86
93
  - Gemfile
87
94
  - MIT-LICENSE
88
95
  - README.md
89
96
  - Rakefile
90
97
  - konamio.gemspec
91
98
  - lib/konamio.rb
99
+ - lib/konamio/base.rb
92
100
  - lib/konamio/key_map.rb
93
101
  - lib/konamio/prompt.rb
102
+ - lib/konamio/result.rb
94
103
  - lib/konamio/sequence/listener.rb
95
104
  - lib/konamio/sequence/requisition.rb
96
105
  - lib/konamio/version.rb
97
106
  - test/konamio/key_map_test.rb
98
107
  - test/konamio/prompt_test.rb
99
108
  - test/konamio/sequence/listener_test.rb
109
+ - test/konamio/sequence/requisition_cancelled_test.rb
100
110
  - test/konamio/sequence/requisition_test.rb
101
111
  - test/minitest_helper.rb
102
- homepage: https://github.com/rthbound/konamio
112
+ homepage: https://rthbound.github.io/konamio
103
113
  licenses: []
114
+ metadata: {}
104
115
  post_install_message:
105
116
  rdoc_options: []
106
117
  require_paths:
107
118
  - lib
108
119
  required_ruby_version: !ruby/object:Gem::Requirement
109
- none: false
110
120
  requirements:
111
- - - ! '>='
121
+ - - '>='
112
122
  - !ruby/object:Gem::Version
113
123
  version: '0'
114
- segments:
115
- - 0
116
- hash: -728235217
117
124
  required_rubygems_version: !ruby/object:Gem::Requirement
118
- none: false
119
125
  requirements:
120
- - - ! '>='
126
+ - - '>='
121
127
  - !ruby/object:Gem::Version
122
128
  version: '0'
123
- segments:
124
- - 0
125
- hash: -728235217
126
129
  requirements: []
127
130
  rubyforge_project:
128
- rubygems_version: 1.8.25
131
+ rubygems_version: 2.0.3
129
132
  signing_key:
130
- specification_version: 3
131
- summary: I'm gonna recognize the konami sequence
133
+ specification_version: 4
134
+ summary: The Ruby Konami code gem.
132
135
  test_files: []