boty 0.0.14 → 0.0.15

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: 1456a7baa55bfe9bd3d7f66044b48ad900753686
4
- data.tar.gz: 3bf63d30f6d2f77b20fe2ae7b13fbbed8283ad26
3
+ metadata.gz: f7c783caaed0c590d532335aeea11bea43728cd9
4
+ data.tar.gz: 9771f725b49e5aca5f20e60c637afdfa5117ba97
5
5
  SHA512:
6
- metadata.gz: 13a273517a75e7d28dfcf8784167ad6950843a97ef1392bf4c522910f4ad456dade641ec935d9a79018e88c1914eba02b2b9a58664c11956beba791275abcb32
7
- data.tar.gz: 973e402fa4780ca293790850b58690ab51d62ee0d3540bc0026f1a35190b3ee1b7846609ab2086409a3df09a6da96aaaed5973432e1745e3baeeaea68c3fc90f
6
+ metadata.gz: 3363a3588cc5ef5978bbb106752ffd158de63f04242e2b38093f3de8dc25b92873a3cdb22ca994bf5c800676d6ec94d62d6d4ecd61d2cbdb8cea895cb4bc650a
7
+ data.tar.gz: 48ab7f8d3be6eae365461c780f9aa0e9bbd333c48ccc33363a54ece2a55d66bdb498d3ac1e2a09e0b9612f440afffc70846806ff1ff4cecf5ef65f1ee4dda912
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.2.0"
4
+ - "2.2.3"
5
+ #matrix:
6
+ # include:
7
+ # - rvm: rbx
8
+ # gemfile: gemfiles/Gemfile.rbx
9
+ script: bundle exec rspec --require=spec_helper
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
+
5
+ gem "byebug"
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boty (0.0.14)
4
+ boty (0.0.15)
5
5
  eventmachine
6
+ faraday
6
7
  faye-websocket
7
8
  i18n
8
9
  thor
@@ -15,12 +16,13 @@ GEM
15
16
  dotenv (2.0.2)
16
17
  eventmachine (1.0.8)
17
18
  fakefs (0.6.7)
19
+ faraday (0.9.2)
20
+ multipart-post (>= 1.2, < 3)
18
21
  faye-websocket (0.10.1)
19
22
  eventmachine (>= 0.12.0)
20
23
  websocket-driver (>= 0.5.1)
21
- foreman (0.78.0)
22
- thor (~> 0.19.1)
23
24
  i18n (0.7.0)
25
+ multipart-post (2.0.0)
24
26
  rake (10.4.2)
25
27
  rspec (3.4.0)
26
28
  rspec-core (~> 3.4.0)
@@ -49,7 +51,6 @@ DEPENDENCIES
49
51
  byebug
50
52
  dotenv
51
53
  fakefs
52
- foreman
53
54
  rake (~> 10.0)
54
55
  rspec
55
56
 
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Boty
2
+ ![](https://travis-ci.org/ricardovaleriano/boty.svg?branch=master)
2
3
 
3
4
  `Boty` is a utilitary to create bots (at this time, specificaly Slack bots).
4
5
 
data/bin/bot CHANGED
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "./lib/boty"
3
3
 
4
- Boty.locale = ARGV.pop || :en
5
4
 
6
5
  session = Boty::Session.new
7
6
  session.start do |bot|
7
+ Boty.locale = ARGV.pop || :en
8
+
8
9
  bot.desc I18n.t "template.presence", bot_name: bot.name
9
10
  bot.message(/#{bot.name}/i) do |message|
10
11
  next if message.from? self
data/boty.gemspec CHANGED
@@ -25,11 +25,10 @@ Gem::Specification.new do |spec|
25
25
  spec.add_runtime_dependency "faye-websocket"
26
26
  spec.add_runtime_dependency "thor"
27
27
  spec.add_runtime_dependency "i18n"
28
+ spec.add_runtime_dependency "faraday"
28
29
 
29
30
  spec.add_development_dependency "bundler", "~> 1.10"
30
31
  spec.add_development_dependency "rake", "~> 10.0"
31
- spec.add_development_dependency "foreman"
32
- spec.add_development_dependency "byebug"
33
32
  spec.add_development_dependency "rspec"
34
33
  spec.add_development_dependency "dotenv"
35
34
  spec.add_development_dependency "fakefs"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: "../"
4
+
5
+ gem "rubinius-compiler"
6
+ gem "rubinius-debugger"
data/lib/boty/action.rb CHANGED
@@ -39,7 +39,12 @@ module Boty
39
39
  end
40
40
 
41
41
  def execute(bot, message)
42
- ScriptDSL.new(bot).instance_exec message, &action if message.match! regex
42
+ dsl = ScriptDSL.new(bot)
43
+ if match = message.match!(regex)
44
+ matches = Array(match)
45
+ matches.shift
46
+ dsl.instance_exec(*matches, &action)
47
+ end
43
48
  rescue => e
44
49
  logger.error e.message
45
50
  raise e
data/lib/boty/bot.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  module Boty
2
2
  class Bot
3
3
  include Boty::Logger
4
+ attr_accessor :trigger_message
4
5
  attr_reader :id, :name
5
6
 
6
7
  def initialize(bot_info, session)
8
+ Locale.reload
7
9
  @raw_info, @id, @name = bot_info, bot_info["id"], bot_info["name"]
8
10
  @handlers ||= []
9
11
  @commands ||= []
@@ -33,11 +35,11 @@ module Boty
33
35
  end
34
36
  end
35
37
 
36
- def message(regex, &block)
38
+ def match(regex, &block)
37
39
  @handlers << create_action(regex, &block)
38
40
  end
39
41
 
40
- def no_message(regex, &block)
42
+ def no_match(regex, &block)
41
43
  @handlers.delete_if do |action|
42
44
  action.is_this? regex, block
43
45
  end
@@ -64,7 +66,7 @@ module Boty
64
66
  end
65
67
 
66
68
  def say(message, api_parameters = {})
67
- channel = (@_message && @_message.channel) || "general"
69
+ channel = (@trigger_message && @trigger_message.channel) || "general"
68
70
  options = { channel: channel }.merge api_parameters
69
71
  post_response = Slack.chat.post_message message, options
70
72
  logger.debug { "post response: #{post_response}" }
@@ -82,8 +84,8 @@ module Boty
82
84
  end
83
85
 
84
86
  def im(message)
85
- logger.debug { "sending im messge to #{@_message.user.name}" }
86
- Slack.chat.post_im @_message.user.id, message
87
+ logger.debug { "sending im messge to #{@trigger_message.user.name}" }
88
+ Slack.chat.post_im @trigger_message.user.id, message
87
89
  end
88
90
 
89
91
  def brain
@@ -119,13 +121,13 @@ module Boty
119
121
 
120
122
  def message_handler(data)
121
123
  actions = has_valid_mention?(data) ? @commands : @handlers
122
- @_message = Message.new data
124
+ @trigger_message = Message.new data
123
125
  begin
124
126
  Array(actions).each do |action|
125
- action.execute self, @_message
127
+ action.execute self, @trigger_message
126
128
  end
127
129
  ensure
128
- @_message = nil
130
+ @trigger_message = nil
129
131
  end
130
132
  end
131
133
  end
@@ -0,0 +1,11 @@
1
+ module Boty
2
+ class Locale
3
+ def self.reload
4
+ default_locales_path = File.expand_path("../../../locale/**/*.yml", __FILE__)
5
+ locales = (Dir["locale/**/*.yml"] + Dir[default_locales_path]).
6
+ map { |file| File.expand_path file }
7
+ I18n.load_path = locales.uniq
8
+ I18n.available_locales = I18n::Backend::Simple.new.available_locales
9
+ end
10
+ end
11
+ end
@@ -2,23 +2,75 @@ require "forwardable"
2
2
 
3
3
  module Boty
4
4
  class ScriptDSL
5
- attr_accessor :bot
6
-
7
5
  INSTANCES = {}
8
6
  private_constant :INSTANCES
9
7
  class << self
10
8
  alias original_constructor new
11
9
  end
12
10
 
11
+ attr_accessor :bot
12
+
13
13
  extend Forwardable
14
- def_delegators :bot, :desc, :respond, :name, :say, :im, :brain, :know_how
14
+ def_delegators :bot, :desc, :respond, :name, :brain, :know_how, :im, :say,
15
+ :match, :no_match, :no_respond, :no
16
+ def_delegators :message, :user, :channel
17
+
18
+ def self.new(bot)
19
+ INSTANCES[bot] ||= original_constructor bot
20
+ end
15
21
 
16
22
  def initialize(bot)
17
23
  @bot = bot
18
24
  end
19
25
 
20
- def self.new(bot)
21
- INSTANCES[bot] ||= original_constructor bot
26
+ def message
27
+ @bot.trigger_message
28
+ end
29
+
30
+ def hear(*args, &block)
31
+ match(*args, &block)
32
+ end
33
+
34
+ def command(*args, &block)
35
+ respond(*args, &block)
36
+ end
37
+
38
+ def match_im(*args, &block)
39
+ command(*args, &block)
40
+ end
41
+
42
+ def http
43
+ @http ||= HTTP.new
44
+ end
45
+ end
46
+
47
+ class HTTP
48
+ [:get, :post, :put, :delete, :head, :patch, :options].each do |verb|
49
+ define_method verb do |url, params = {}|
50
+ response = connection verb, url, params
51
+ handle_response response
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def connection(verb, url, params)
58
+ uri = URI url
59
+ Faraday.new(url: "#{uri.scheme}://#{uri.host}") { |builder|
60
+ builder.adapter(*Faraday.default_adapter)
61
+ }.public_send(verb) { |req|
62
+ req.url uri.path, params
63
+ }
64
+ end
65
+
66
+ def handle_response(response)
67
+ # TODO: use a response parser accordingly to the
68
+ body = response.body
69
+ if /application\/json/.match response.headers["Content-Type"]
70
+ JSON.parse body
71
+ else
72
+ body
73
+ end
22
74
  end
23
75
  end
24
76
  end
data/lib/boty/session.rb CHANGED
@@ -9,18 +9,18 @@ module Boty
9
9
  def start(&block)
10
10
  EM.run do
11
11
  login
12
- bot = initialize_bot(&block)
13
-
12
+ @bot = initialize_bot(&block)
14
13
  stablish_connection do |ws|
15
14
  ws.on :message do |event|
16
15
  begin
17
- on_message event, bot
16
+ on_message event, @bot
18
17
  rescue StandardError => e
19
18
  logger.error "Message #{event} could not be processed. #{e.message}"
20
19
  end
21
20
  end
22
21
  end
23
22
  end
23
+ self
24
24
  end
25
25
 
26
26
  private
@@ -34,7 +34,7 @@ module Boty
34
34
 
35
35
  def initialize_bot(&block)
36
36
  Bot.new(@slack_info["self"], self).tap { |bot|
37
- block.call bot if block_given?
37
+ block.call ScriptDSL.new(bot) if block_given?
38
38
  logger.debug { "bot is configured and ready to go!" }
39
39
  }
40
40
  end
data/lib/boty/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Boty
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
3
3
  end
data/lib/boty.rb CHANGED
@@ -10,15 +10,13 @@ rescue LoadError
10
10
  end
11
11
 
12
12
  require "faye/websocket"
13
-
14
13
  require "i18n"
15
- locales = Dir[File.expand_path("../../locale/**/*", __FILE__)]
16
- I18n.load_path += locales
17
14
 
18
15
  $:.unshift File.expand_path("../../lib", __FILE__)
19
16
 
20
17
  module Boty
21
18
  def self.locale=(lang)
19
+ Locale.reload
22
20
  I18n.locale = lang
23
21
  end
24
22
 
@@ -35,4 +33,5 @@ require "boty/message"
35
33
  require "boty/action"
36
34
  require "boty/script_loader"
37
35
  require "boty/script_dsl"
36
+ require "boty/locale"
38
37
  require "boty/bot"
data/script/knows.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  desc "knows", I18n.t("scripts.knows")
2
- respond(/knows/i) do
3
- say KnownFormmater.new(know_how).format
2
+ command(/knows/i) do
3
+ im KnownFormmater.new(know_how).format
4
4
  end
5
5
 
6
6
  class KnownFormmater
data/script/pug.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  desc "pug me", I18n.t("scripts.pug_me")
2
- respond(/pug me/i) do
3
- response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/random")
2
+ command(/pug me/i) do
3
+ response = http.get "http://pugme.herokuapp.com/random"
4
4
  say "<#{response["pug"]}>"
5
5
  end
6
6
 
7
7
  desc "pug bomb X", I18n.t("scripts.pug_bomb")
8
- respond(/pug bomb( (\d+))?/i) do |message|
9
- count = message.match[2] || 5
10
- response = JSON.parse Net::HTTP.get(URI "http://pugme.herokuapp.com/bomb?count=#{count}")
8
+ command(/pug bomb( \d+)?/i) do |count|
9
+ count = (count || "5").strip
10
+ response = http.get "http://pugme.herokuapp.com/bomb", count: count
11
11
  response["pugs"].each do |pug| say "<#{pug}>" end
12
12
  end
data/script/remember.rb CHANGED
@@ -1,18 +1,17 @@
1
1
  desc I18n.t("scripts.remember.command"),
2
2
  I18n.t("scripts.remember.desc", name: name)
3
- respond(/remember(:?) (.+)/i) do |message|
3
+ command(/remember(:?) (.+)/i) do |_, stuff|
4
4
  brain[:user] ||= {}
5
- brain[:user][message.user.name] ||= []
6
- brain[:user][message.user.name] << message.match[2]
5
+ brain[:user][user.name] ||= []
6
+ brain[:user][user.name] << stuff
7
7
  im I18n.t("scripts.remember.response")
8
8
  end
9
9
 
10
10
  desc I18n.t("scripts.about.command"), I18n.t("scripts.about.desc")
11
- respond(/about (\w+\b)(:?) (.+)/i) do |message|
12
- user = message.match[3]
11
+ command(/about (\w+\b)(:?) (.+)/i) do |brain_area, _, user_about|
13
12
  im I18n.t "scripts.about.response",
14
- brain_area: message.match[1],
15
- user_asking: message.user.name,
16
- user_about: user
17
- brain[message.match[1].to_sym][user].each do |info| im info end
13
+ brain_area: brain_area,
14
+ user_asking: user.name,
15
+ user_about: user_about
16
+ brain[message.match[1].to_sym][user_about].each do |info| im info end
18
17
  end
@@ -1,13 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "./<%= bot_name %>"
3
3
 
4
- Boty.locale = ARGV.pop || :en
4
+ def set_locale(locale)
5
+ begin
6
+ Boty.locale = locale || :en
7
+ rescue I18n::InvalidLocale
8
+ Boty.locale = :en
9
+ end
10
+ end
5
11
 
6
12
  session = Boty::Session.new
7
13
  session.start do |bot|
14
+ set_locale ARGV.pop
15
+
8
16
  bot.desc I18n.t "template.presence", bot_name: bot.name
9
- bot.message(/#{bot.name}/i) do |message|
17
+ bot.match(/#{bot.name}/i) do
10
18
  next if message.from? self
11
- say I18n.t "template.hello", user_name: message.user.name
19
+ say I18n.t "template.hello", user_name: user.name
12
20
  end
13
21
  end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Valeriano
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-30 00:00:00.000000000 Z
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -67,61 +67,47 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: bundler
70
+ name: faraday
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1.10'
76
- type: :development
75
+ version: '0'
76
+ type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1.10'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: rake
84
+ name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '10.0'
89
+ version: '1.10'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '10.0'
97
- - !ruby/object:Gem::Dependency
98
- name: foreman
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
96
+ version: '1.10'
111
97
  - !ruby/object:Gem::Dependency
112
- name: byebug
98
+ name: rake
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
- - - ">="
101
+ - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: '0'
103
+ version: '10.0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
- - - ">="
108
+ - - "~>"
123
109
  - !ruby/object:Gem::Version
124
- version: '0'
110
+ version: '10.0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rspec
127
113
  requirement: !ruby/object:Gem::Requirement
@@ -175,6 +161,7 @@ extra_rdoc_files: []
175
161
  files:
176
162
  - ".env"
177
163
  - ".gitignore"
164
+ - ".travis.yml"
178
165
  - CODE_OF_CONDUCT.md
179
166
  - Gemfile
180
167
  - Gemfile.lock
@@ -187,10 +174,12 @@ files:
187
174
  - bin/setup
188
175
  - boty.gemspec
189
176
  - exe/boty
177
+ - gemfiles/Gemfile.rbx
190
178
  - lib/boty.rb
191
179
  - lib/boty/action.rb
192
180
  - lib/boty/bot.rb
193
181
  - lib/boty/cli.rb
182
+ - lib/boty/locale.rb
194
183
  - lib/boty/logger.rb
195
184
  - lib/boty/message.rb
196
185
  - lib/boty/rspec.rb
@@ -221,6 +210,7 @@ files:
221
210
  - template/project/Rakefile
222
211
  - template/project/app/.empty
223
212
  - template/project/bot.tt
213
+ - template/project/locale/.empty
224
214
  - template/project/script/ping.rb
225
215
  - template/project/spec/.empty
226
216
  - template/project/spec/script/ping_spec.rb