expect-behaviors 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +88 -9
- data/VERSION +1 -1
- data/expect-behaviors.gemspec +2 -2
- data/lib/expect/match.rb +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDM1NWQyOTg2NmYxOTVhOTNlNWM1MTg4ODZjNWQ1NDVhNWExZmIwYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjU0MjMzYmE3ZjcwMGEyNThmMTcxMzY0Y2YxMTRhZGU4ODI0ODMwOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzVjYmZhMDIyZWIyOGEzZWRiNDNhMmNiNWVhZGM2MGFjZmFkMzU2ZDdjMmEz
|
10
|
+
MWZjM2I2M2QzMjZlMjdjNDFmY2YzNWNmOTRlNTZhZDhjNDczZmE0MTVjZTZh
|
11
|
+
YThhMGNkYTlhN2E1MTIyZDBjZTkzNjkxYmY1ODljMDM2Njg4NDI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MmNjZDVhYzE4YjljMzNlZTU1OTg2Y2Q2NzY4NDRkZjAxZmQyNzQwYjUyZGUx
|
14
|
+
NmQ2ZTAwNWNiYTdjODNmNGMwOGFkODRhYjcxOWM5YmVkZGQ1MDE3YzNhODBl
|
15
|
+
OTE2MjI3NDQxYTUwMjhmYmUxZmQyNWUwY2Q3ZjFiNDIxYmE4YmI=
|
data/README.md
CHANGED
@@ -5,12 +5,91 @@ Ruby Mixin to add Expect Behaviors to SSH/Serial/Telnet controllers
|
|
5
5
|
[![Code Climate](https://codeclimate.com/github/francisluong/expect-behaviors/badges/gpa.svg)](https://codeclimate.com/github/francisluong/expect-behaviors)
|
6
6
|
[![Test Coverage](https://codeclimate.com/github/francisluong/expect-behaviors/badges/coverage.svg)](https://codeclimate.com/github/francisluong/expect-behaviors)
|
7
7
|
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
# Using the Mixin
|
9
|
+
|
10
|
+
Two public methods are required to support adding expect behaviors to your controller.
|
11
|
+
|
12
|
+
- #exp_process - should do one iteration of handle input and append buffer
|
13
|
+
- #exp_buffer - provide the current buffer contents from controller and empty it
|
14
|
+
|
15
|
+
The you need to include the module in your class:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'expect/behavior'
|
19
|
+
|
20
|
+
class Klass
|
21
|
+
include Expect::Behavior
|
22
|
+
end
|
23
|
+
|
24
|
+
```
|
25
|
+
|
26
|
+
# Batteries Included: Expect::SSH
|
27
|
+
|
28
|
+
You can find an example which uses the module in this repo: [Expect::SSH](lib/expect/ssh.rb). This example implements the required methods:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
##
|
32
|
+
# exp_buffer - provide the current buffer contents and empty it
|
33
|
+
def exp_buffer
|
34
|
+
result = @receive_buffer
|
35
|
+
@receive_buffer = ''
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# exp_process - should do one iteration of handle input and append buffer
|
41
|
+
def exp_process
|
42
|
+
sleep(@wait_sec.to_f)
|
43
|
+
@ssh.process(0)
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
# Using Expect
|
48
|
+
|
49
|
+
Once Expect::Behaviors has been included you should be able to use Expect blocks in your code.
|
50
|
+
|
51
|
+
Here is an example assuming an instance of Expect::SSH that expects a switch prompt and includes a timeout block that expires after 3 seconds. It returns different values depending on how things work out.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
result = @ssh.expect do
|
55
|
+
when_matching(/switch-prompt1#/) do
|
56
|
+
"switch 1"
|
57
|
+
end
|
58
|
+
when_matching(/switch-prompt2#/) do
|
59
|
+
"switch 2"
|
60
|
+
end
|
61
|
+
when_timeout(3) do
|
62
|
+
"timed out"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
You can set the timeout value:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
@ssh.exp_timeout_sec = 2 * 60
|
71
|
+
```
|
72
|
+
|
73
|
+
You can set the exp_sleep_interval_sec between buffer checks:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
@includer.exp_sleep_interval_sec = 10
|
77
|
+
```
|
78
|
+
|
79
|
+
# Expect::Match
|
80
|
+
|
81
|
+
And you can use #exp_match or return @exp_match from within a #when_matching block to return an Expect::Match object.
|
82
|
+
|
83
|
+
Expect::Match exposes the following methods:
|
84
|
+
|
85
|
+
- #buffer - returns the full contents of the buffer that matched
|
86
|
+
- #exact_match_string - returns the first capture from the match
|
87
|
+
- #to_s - returns the contents of the buffer up to the match
|
88
|
+
- #remainder - returns the contents of the buffer following the first match
|
89
|
+
- #nil? - true if there were no matches
|
90
|
+
|
91
|
+
# That's All the Crummy Documentation?
|
92
|
+
|
93
|
+
For now, yes. I need to learn how to use YARD and RDOC. Sorry!
|
94
|
+
|
95
|
+
-Franco
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/expect-behaviors.gemspec
CHANGED
@@ -6,12 +6,12 @@
|
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "expect-behaviors"
|
9
|
-
s.version =
|
9
|
+
s.version = IO.read('VERSION')
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Francis Luong (Franco)"]
|
14
|
-
s.date =
|
14
|
+
s.date = File.mtime('VERSION')
|
15
15
|
s.description = "Ruby Mixin to add Expect Behaviors to SSH/Serial/Telnet controllers"
|
16
16
|
s.email = "me@francisluong.com"
|
17
17
|
s.extra_rdoc_files = [
|
data/lib/expect/match.rb
CHANGED
@@ -8,6 +8,7 @@ module Expect
|
|
8
8
|
@matches = @buffer.match(@expression)
|
9
9
|
end
|
10
10
|
|
11
|
+
# returns the first capture from the match
|
11
12
|
def exact_match_string
|
12
13
|
@matches.nil? ? nil : @matches[0]
|
13
14
|
end
|
@@ -16,15 +17,18 @@ module Expect
|
|
16
17
|
Regexp.new(".*?#{@expression.source}", @expression.options | Regexp::MULTILINE)
|
17
18
|
end
|
18
19
|
|
20
|
+
# true if there were no matches
|
19
21
|
def nil?
|
20
22
|
@matches.nil?
|
21
23
|
end
|
22
24
|
|
25
|
+
# returns the contents of the buffer up to the match
|
23
26
|
def substring_up_to_match
|
24
27
|
@matches.nil? ? nil : @buffer.match(expr_substring_to_match)[0]
|
25
28
|
end
|
26
29
|
alias_method :to_s, :substring_up_to_match
|
27
30
|
|
31
|
+
# returns the contents of the buffer following the first match
|
28
32
|
def substring_remainder
|
29
33
|
if @matches.nil?
|
30
34
|
@buffer
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: expect-behaviors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francis Luong (Franco)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-ssh
|