botolo 0.10.0 → 0.20.0

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: 3700d7909e38446f210f833a5056d4b16f9f84ae
4
- data.tar.gz: 14ef0e0ee8af888d8c4c7f82bee3f6f266c3057f
3
+ metadata.gz: d26a145e0b3ac102c30cf98e2ce8a45b25775149
4
+ data.tar.gz: 9655334f33005b852effe442d23142da14883175
5
5
  SHA512:
6
- metadata.gz: 347960ca6554b41963cd1075001df23de440e0483f8536052998bdf53460e744223f474ae7b4292831a12035fa2c506122d9204a5fc1fb352e7f30adcaeb8a35
7
- data.tar.gz: 62bae3351919d3093e4dab14c4254cc3ed0ab6ec2f1177531859e1f947b3427170d0299f7581156bb5ad35c1ff8986c6b91077bc9463388d0f1219296e3b0b2a
6
+ metadata.gz: de587e1e239f1ab995b149936377168ffa13dd0ef3903df6c608bce8c119133902c6f351086518cd3b3fc4cf32f29c7a0e635b5a7dc4eba084049bec5b768e98
7
+ data.tar.gz: 071885a1b089208ef848deac5c487f02d6d26d36bcee5ffb7939abd68fc8320e9a6fa06357bb60feffd35e4f5cdf4fb4522db6925e5a6eafa44098705b33b23c
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Botolo
2
2
 
3
- TODO: Write a gem description
3
+ Botolo is an engine for bots written in ruby. With botolo you must take care
4
+ only about writing actions your bot must implement and then the engine will
5
+ execute them for you.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,100 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ For creating a great bot you have to describe in a YAML configuration file the
24
+ Bot behaviour and in a separate ruby class you must implement the methods your
25
+ bot must carry on.
26
+
27
+ Let's see how to create a very dummy bot saying _foo_ every 3 seconds and
28
+ saying _hello_ every 5 seconds.
29
+
30
+ You have to write a config.yaml file containing the action schedule and some
31
+ very basic information about your bot.
32
+
33
+ ```
34
+ verbose: true
35
+
36
+ bot:
37
+ name: dummy-bot
38
+ version: 1.0
39
+ email: paolo@codesake.com
40
+ # This overrides any behaviour file passed as argument
41
+ behaviour: dummy-bot.rb
42
+
43
+ twitter:
44
+ enabled: no
45
+ task:
46
+ - { schedule: every 5 s, action: say_hello }
47
+ - { schedule: every 3 s, action: say_foo }
48
+ ```
49
+
50
+ Botolo expects to find in the current directory a file named dummy-bot.rb
51
+ implementing the two methods (say\_hello and say\_foo) it has to run based upon
52
+ this schedule.
53
+ There is a **big** constraint: your ruby file must be a Botolo::Bot::Behaviour
54
+ class. It must also implement an initialize method with an Hash parameter
55
+ engine will use to pass options to the bot.
56
+
57
+ It will be used in the future to implement some form of communication between
58
+ the main process and threads, useful to provide a centralized console about
59
+ what's going on on your bot.
60
+
61
+ Let's see our dummy bot behaviour:
62
+
63
+ ```
64
+ module Botolo
65
+ module Bot
66
+ class Behaviour
67
+ def initialize(options={})
68
+ end
69
+
70
+ def say_hello
71
+ puts "hello"
72
+ end
73
+
74
+ def say_foo
75
+ puts "foo"
76
+ end
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
82
+ It's easy, isn't it!?!
83
+
84
+ Now, all you have to do is running the botolo command specifying the config
85
+ file as parameter. Remember to put the behaviour class in the same directory of
86
+ your YAML file.
87
+
88
+ ```
89
+ $ botolo config.yaml
90
+ botolo:9: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
91
+ [*] dummy-bot v1.0 is starting up at 08:31:31
92
+ 08:31:31: 2 tasks loaded
93
+ 08:31:31: using ./dummy-bot.rb as bot behaviour
94
+ 08:31:31: dummy-bot is online
95
+ 08:31:31: entering main loop
96
+ 08:31:31: hello
97
+ 08:31:31: foo
98
+ 08:31:34: foo
99
+ 08:31:36: hello
100
+ 08:31:37: foo
101
+ 08:31:40: foo
102
+ 08:31:41: hello
103
+ 08:31:43: foo
104
+ ^C08:31:46: shutting down threads
105
+ 08:31:46: pid #<Thread:0x007fa731022e18> killed
106
+ 08:31:46: pid #<Thread:0x007fa731022cd8> killed
107
+ [*] bot is shutting down at 08:31:46
108
+ ```
109
+
110
+ Custom written behaviour can use the global variable $logger to use botolo logging
111
+ facilities and having the stdout/stderr prints more consistent.
112
+
113
+ ## Missing features
114
+
115
+ A back channels for threads to communicate with the engine about action
116
+ status.
22
117
 
23
118
  ## Contributing
24
119
 
data/bin/botolo CHANGED
@@ -5,10 +5,11 @@ require 'openssl'
5
5
  require 'codesake_commons'
6
6
 
7
7
  DEFAULT_BEHAVIOUR = "./lib/botolo/bot/behaviour.rb"
8
+
8
9
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
9
10
 
10
11
  $logger = Codesake::Commons::Logging.instance
11
- trap("INT") { $logger.helo('bot is shutting down'); Kernel.exit(0) }
12
+ trap("INT") { @bot.stop; $logger.helo('bot is shutting down'); Kernel.exit(0) }
12
13
 
13
14
  behaviour_file = DEFAULT_BEHAVIOUR
14
15
  config_file = nil
@@ -16,10 +17,10 @@ config_file = ARGV[0] if ARGV.count == 1
16
17
 
17
18
  $logger.die "usage: botolo bot_configuration_file" if config_file.nil?
18
19
 
19
- bot = Botolo::Bot::Engine.new({:config=>config_file})
20
- $logger.log "#{bot.name} is online" if bot.online?
21
- $logger.log "#{bot.name} is offline" unless bot.online?
22
- bot.run if bot.online?
23
-
20
+ @bot = Botolo::Bot::Engine.new({:config=>config_file})
21
+ $logger.log "#{@bot.name} is online" if @bot.online?
22
+ $logger.log "#{@bot.name} is offline" unless @bot.online?
23
+ @bot.run if @bot.online?
24
+ @bot.infinite_loop
24
25
 
25
26
 
data/botolo.gemspec CHANGED
@@ -9,7 +9,10 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Paolo Perego"]
10
10
  spec.email = ["thesp0nge@gmail.com"]
11
11
  spec.description = %q{botolo is a bot engine written in ruby}
12
- spec.summary = %q{botolo is a bot engine written in ruby}
12
+ spec.summary = %q{botolo is a bot engine written in ruby. With botolo
13
+ you can focus on writing a set of actions your bot will execute every
14
+ amount of seconds, minutes or whatever and implement those actions. The
15
+ part of executing them and putting babies to sleep will be up to botolo.}
13
16
  spec.homepage = "http://codesake.com"
14
17
  spec.license = "MIT"
15
18
 
@@ -3,23 +3,23 @@ require 'yaml'
3
3
 
4
4
  module Botolo
5
5
  module Bot
6
-
7
- # This is the main bot class, it is responsible of:
8
- # * reading configuration
9
- # * using Twitter APIs
10
- # * do something
11
6
  class Engine
12
7
 
13
8
  def initialize(options={})
14
9
  @start_time = Time.now
15
10
  @online = false
16
11
  @config = read_conf(options[:config])
17
- authenticate
12
+ authenticate if @config['twitter']['enabled']
13
+
18
14
  @tasks = @config['task']
15
+ @task_pids = []
16
+
19
17
  behaviour = File.join(".", @config['bot']['behaviour']) unless @config['bot']['behaviour'].nil?
20
18
 
21
19
  $logger.helo "#{name} v#{version} is starting up"
22
20
 
21
+ $logger.log "#{@tasks.size} tasks loaded"
22
+
23
23
  begin
24
24
  load behaviour
25
25
  $logger.log "using #{behaviour} as bot behaviour"
@@ -51,18 +51,45 @@ module Botolo
51
51
 
52
52
  def run
53
53
  $logger.log "entering main loop"
54
+ @tasks.each do |task|
55
+ @task_pids << Thread.start do
56
+ start_task(task['action'], task['schedule'])
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ def infinite_loop
54
63
  while true
55
- @tasks.each do |task|
56
- begin
57
- @behaviour.send(task["action"].to_sym) if @behaviour.respond_to? task["action"].to_sym
58
- rescue => e
59
- $logger.err "#{task["action"]} failed (#{e.message})"
60
- end
61
- sleep calc_sleep_time(task["schedule"])
64
+
65
+ end
66
+ end
67
+
68
+ def start_task(name, sleep)
69
+ while true
70
+ begin
71
+ @behaviour.send(name.to_sym) if @behaviour.respond_to? name.to_sym
72
+ rescue => e
73
+ $logger.err "#{name} failed (#{e.message})"
62
74
  end
75
+ sleep calc_sleep_time(sleep)
63
76
  end
64
77
 
65
78
  end
79
+
80
+ def stop
81
+ $logger.log "shutting down threads"
82
+ @task_pids.each do |pid|
83
+ Thread.kill(pid)
84
+ sleep 0.05
85
+ $logger.log "pid #{pid} killed" if ! pid.alive?
86
+ $logger.err "pid #{pid} not killed" if pid.alive?
87
+ end
88
+
89
+ true
90
+ end
91
+
92
+
66
93
  def authenticate
67
94
  begin
68
95
  Twitter.configure do |config|
@@ -1,3 +1,3 @@
1
1
  module Botolo
2
- VERSION = "0.10.0"
2
+ VERSION = "0.20.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: botolo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Perego
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-28 00:00:00.000000000 Z
11
+ date: 2013-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,5 +108,8 @@ rubyforge_project:
108
108
  rubygems_version: 2.0.3
109
109
  signing_key:
110
110
  specification_version: 4
111
- summary: botolo is a bot engine written in ruby
111
+ summary: botolo is a bot engine written in ruby. With botolo you can focus on writing
112
+ a set of actions your bot will execute every amount of seconds, minutes or whatever
113
+ and implement those actions. The part of executing them and putting babies to sleep
114
+ will be up to botolo.
112
115
  test_files: []