opsa 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/opsa +18 -0
  3. data/lib/opsa.rb +35 -0
  4. data/lib/opsa/language.rb +61 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: 3a7b0ace961a874a57cbdbeb454c617460ef0b60
4
+ data.tar.gz: b9119195a31b28407a38a73a2cc4df7205a44061
5
+ !binary "U0hBNTEy":
6
+ metadata.gz: 06cf46e994b367679a909fc029a813c62ad1f01094808997970452a1cd23681debc042cbedb66395074c6738268ea92106b8dae74c4ae2d4fc42a138623a841d
7
+ data.tar.gz: 749504c033af12c11811678849d07668382fbe9d27a13e19cb2f932701ba875287e57194da4f8c8748f667802b43a2fc2daa5a9c8ccb6e46702427b1dfa203aa
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'opsa'
4
+
5
+ lang = OPSA::Language.new
6
+
7
+ lang.respond_to Regexp.new('^say_hello (.+)') do |match|
8
+ puts "> Hello, #{match}!"
9
+ end
10
+ lang.respond_to Regexp.new('^echo (.+)') do |match|
11
+ puts "> #{match}"
12
+ end
13
+
14
+ robot = OPSA.new(:language => lang)
15
+
16
+ while line = gets and line.strip!
17
+ robot.r(line)
18
+ end
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class OPSA
4
+ attr_accessor :lang
5
+
6
+ # Initialize OPSA object
7
+ #
8
+ # Example:
9
+ # >> OPSA.new(:lang => OPSA::Language.new)
10
+ #
11
+ # Arguments:
12
+ # params: (Hash)
13
+ # :language: OPSA::Language
14
+
15
+ def initialize(params)
16
+ if lang = params[:language]
17
+ @lang = lang
18
+ end
19
+ end
20
+
21
+ # Read words and process with OPSA::Language
22
+ #
23
+ # Example:
24
+ # >> robot = OPSA.new(:language => OPSA::Language.new)
25
+ # >> robot.r("echo:hello")
26
+ #
27
+ # Arguments:
28
+ # words: (String)
29
+
30
+ def r(words)
31
+ @lang.input(words)
32
+ end
33
+ end
34
+
35
+ require 'opsa/language'
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ class OPSA::Language
4
+ def initialize
5
+ @actions = Hash.new nil
6
+ end
7
+
8
+ # Define language
9
+ #
10
+ # Example:
11
+ # lang.respond_to Regexp.new('^say_hello (.+)') do |match|
12
+ # puts "> Hello, #{match}!"
13
+ # end
14
+ #
15
+ # Arguments:
16
+ # regex: Regexp
17
+ # action: Block
18
+
19
+ def respond_to(regex, &action)
20
+ @actions[regex] ||= action
21
+ end
22
+
23
+ def input(text)
24
+ @actions.each do |regex, action|
25
+ if _match = text.match(regex)
26
+ args = _match.captures
27
+ if args.size > 0
28
+ action.call(*args)
29
+ else
30
+ action.call(text)
31
+ end
32
+ else
33
+ next
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ if __FILE__ == $0
40
+ lang = OPSA::Language.new
41
+
42
+ lang.respond_to Regexp.new('^say_hello (.+)') do |match|
43
+ puts "Hello, #{match}!"
44
+ end
45
+ lang.respond_to Regexp.new('^echo:(.+)') do |match|
46
+ puts match
47
+ end
48
+
49
+ counter = 0
50
+ lang.respond_to Regexp.new('^incr$') do |text|
51
+ counter += 1
52
+ end
53
+
54
+ lang.input("say_hello world")
55
+ lang.input("say_hello beautifull sunshine")
56
+ lang.input("echo:hello")
57
+ lang.input("echo:world")
58
+
59
+ lang.input("incr")
60
+ puts counter
61
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Liu Lantao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple systems operation robot
14
+ email: liulantao@gmail.com
15
+ executables:
16
+ - opsa
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/opsa.rb
21
+ - lib/opsa/language.rb
22
+ - bin/opsa
23
+ homepage: http://rubygems.org/gems/opsa
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.6
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: OPSA
47
+ test_files: []