secret_knock 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc9d932e84a76fdbc96e3935d749d9e6841999a2
4
- data.tar.gz: 2f4e1d99d1b08d1018093b966ffdb45ba4b2ccab
3
+ metadata.gz: f563e31f57fd2e069234691672ca82d30ac7f408
4
+ data.tar.gz: eeef07610f8195bbf7e2dfaf6fbb9cf2bd2675dc
5
5
  SHA512:
6
- metadata.gz: 4fe898c8acd2a4807ac473038d4dcb4f6167fb866ef84f1f41c45e141be2fae4cb155a5d4c9dbce25b34cbf687f8733a5e328dff1e796f899707394f47840180
7
- data.tar.gz: bcfb6222515dc9a9cb11e3c70aec671c92516ae42b8a187d90762e25cc2cfb1bd2bdee94430ec7b06db97d536d07cfc13dfaa1772ddd92efd56f4bd264d9c795
6
+ metadata.gz: 2290017d47f7026c92c34491f888a858a694238d9158c1c81c1dc9e6359c28ad17abbea8a40cb1fc632bcfdf18a6f828beb41c5d53cfe529d2076e9e97fe7ff3
7
+ data.tar.gz: 84f1cf5bae8b3a22e960210c961795fe70f0508218996431315ff5348b6f79f97300107cb8b87b5e4b5e253c464d4ed7a270766b47b4d7f468cf2b2728bdc433
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/secret_knock.rb CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  class SecretKnock
7
7
 
8
- attr_reader :h, :message
8
+ attr_reader :h, :message, :listening
9
9
 
10
10
  def initialize(verbose: true, external: nil)
11
11
 
@@ -42,12 +42,27 @@ class SecretKnock
42
42
  @hb = h.invert
43
43
 
44
44
  @short_delay, @long_delay = 0.35, 0.9 # seconds
45
+ @listening = false
45
46
 
46
47
  end
47
48
 
48
49
  def autoknock(s)
49
50
  playback knockerize(s)
50
51
  end
52
+
53
+ def detect(timeout: 2, repeat: true)
54
+
55
+ listen
56
+
57
+ Thread.new do
58
+
59
+ sleep 0.2 while @t + timeout > Time.now or @a.length <= 1
60
+ msg = decipher()
61
+ on_timeout msg
62
+ @external.message msg if @external
63
+ detect(timeout: timeout, repeat: repeat) if repeat
64
+ end
65
+ end
51
66
 
52
67
  def knocked()
53
68
 
@@ -98,7 +113,9 @@ class SecretKnock
98
113
 
99
114
  @t = Time.now
100
115
  @i = 1
101
- @a = [0]
116
+ @a = []
117
+
118
+ @listening = true
102
119
 
103
120
  if block_given? then
104
121
  yield self
@@ -113,6 +130,10 @@ class SecretKnock
113
130
  sleep @long_delay + 0.1
114
131
 
115
132
  end
133
+
134
+ def on_timeout(msg='')
135
+ # override this method if you wish
136
+ end
116
137
 
117
138
  alias pause long_pause
118
139
 
@@ -137,10 +158,19 @@ class SecretKnock
137
158
 
138
159
  def stop_listening()
139
160
 
140
- @message = @a[2..-1].join.gsub(/(\d{2})(\d)/,'\1,\2').split(',')\
161
+ @listening = false
162
+ @message = decipher()
163
+
164
+ end
165
+
166
+ private
167
+
168
+ def decipher()
169
+
170
+ @a.join.gsub(/(\d{2})(\d)/,'\1,\2').split(',')\
141
171
  .inject([]) do |r, x|
142
172
  (x != '42' ? r << @hb[x.to_i] : r[0..-2])
143
173
  end.join
144
-
145
174
  end
175
+
146
176
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secret_knock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file