boty 0.0.17.1 → 0.1.0

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +559 -54
  4. data/bin/bot +10 -13
  5. data/docs/images/readme-01-screen-integration.png +0 -0
  6. data/docs/images/readme-02-screen-integration.png +0 -0
  7. data/docs/images/readme-03-screen-integration.png +0 -0
  8. data/lib/boty/action.rb +1 -1
  9. data/lib/boty/bot.rb +46 -63
  10. data/lib/boty/dsl.rb +52 -0
  11. data/lib/boty/eventable.rb +41 -0
  12. data/lib/boty/http.rb +33 -0
  13. data/lib/boty/locale.rb +17 -4
  14. data/lib/boty/logger.rb +21 -4
  15. data/lib/boty/rspec.rb +2 -3
  16. data/lib/boty/script_loader.rb +1 -1
  17. data/lib/boty/session.rb +21 -17
  18. data/lib/boty/slack/chat.rb +2 -2
  19. data/lib/boty/slack/message.rb +29 -0
  20. data/lib/boty/slack/users.rb +13 -0
  21. data/lib/boty/slack.rb +6 -0
  22. data/lib/boty/version.rb +1 -1
  23. data/lib/boty.rb +4 -4
  24. data/spec/boty/bot_spec.rb +105 -174
  25. data/spec/boty/dsl_spec.rb +125 -0
  26. data/spec/boty/http_spec.rb +5 -0
  27. data/spec/boty/logger_spec.rb +33 -0
  28. data/spec/boty/rspec_spec.rb +1 -1
  29. data/spec/boty/script_loader_spec.rb +27 -0
  30. data/spec/boty/session_spec.rb +9 -11
  31. data/spec/boty/slack/message_spec.rb +34 -0
  32. data/spec/boty/slack/users_spec.rb +41 -15
  33. data/spec/happy_path_spec.rb +22 -12
  34. data/spec/script/i18n_spec.rb +10 -4
  35. data/spec/script/pug_spec.rb +1 -1
  36. data/spec/spec_helper.rb +5 -2
  37. data/spec/support/logger_support.rb +20 -0
  38. data/spec/support/session_support.rb +2 -2
  39. data/template/project/bot.tt +4 -13
  40. data/template/project/script/ping.rb +3 -3
  41. metadata +14 -5
  42. data/lib/boty/message.rb +0 -27
  43. data/lib/boty/script_dsl.rb +0 -80
  44. data/spec/boty/message_spec.rb +0 -32
@@ -1,44 +1,70 @@
1
1
  module Boty
2
2
  module Slack
3
3
  RSpec.describe Users do
4
- subject(:users) { described_class.new }
5
-
6
- let(:action) { :info }
7
-
8
- let(:url) {
4
+ def url_for(action)
9
5
  "https://slack.com/api/users.#{action}?"+
10
6
  "token=#{ENV['SLACK_BOT_API_TOKEN']}"
11
- }
7
+ end
8
+
9
+ subject(:users) { described_class.new }
12
10
 
13
11
  before do
14
- allow(Slack::URL).to receive(:get).with(url + "&user=U023BECGF").
12
+ allow(Slack::URL).to receive(:get).with(url_for(:info) + "&user=U123456").
13
+ and_return(
14
+ "ok" => true,
15
+ "user" => {
16
+ "id" => "U123456",
17
+ "name" => "julian"
18
+ }
19
+ )
20
+
21
+ allow(Slack::URL).to receive(:get).with(url_for(:list)).
15
22
  and_return(
16
23
  {
17
24
  "ok" => true,
18
- "user" => {
19
- "id" => "U023BECGF",
25
+ "members" => [
26
+ {
27
+ "id" => "U023BECGF",
28
+ "name" => "bobby"
29
+ },
30
+ {
31
+ "id" => "U123456",
20
32
  "name" => "julian"
21
- }
33
+ }
34
+ ]
22
35
  }
23
36
  )
24
37
  end
25
38
 
26
39
  describe "#info" do
27
40
  it "calls Slack::URL get with token and user" do
28
- expect(Slack::URL).to receive(:get).with(url + "&user=U023BECGF")
41
+ expect(Slack::URL).to receive(:get).with(url_for(:info) + "&user=U123456")
29
42
 
30
- users.info "U023BECGF"
43
+ users.info "U123456"
31
44
  end
32
45
 
33
46
  it "returns a channel object with the im channel information" do
34
- expect(Slack::URL).to receive(:get).with(url + "&user=U023BECGF")
47
+ expect(Slack::URL).to receive(:get).with(url_for(:info) + "&user=U123456")
35
48
 
36
- user = users.info("U023BECGF")
49
+ user = users.info("U123456")
37
50
 
38
- expect(user.id).to eq "U023BECGF"
51
+ expect(user.id).to eq "U123456"
39
52
  expect(user.name).to eq "julian"
40
53
  end
41
54
  end
55
+
56
+ describe "#list" do
57
+ it "retrieves the users list" do
58
+ list = Slack.users.list
59
+ expect(list.map(&:name)).to eq ["bobby", "julian"]
60
+ end
61
+ end
62
+
63
+ describe "#by_name" do
64
+ it "uses the users.list to discover info on a given user.name" do
65
+ expect(Slack.users.by_name("julian").id).to eq "U123456"
66
+ end
67
+ end
42
68
  end
43
69
  end
44
70
  end
@@ -15,7 +15,7 @@ module Boty
15
15
  end
16
16
 
17
17
  def message(message)
18
- faye.message(message, {user: current_user_id})
18
+ faye.message(message, user: current_user_id)
19
19
  end
20
20
 
21
21
  let(:bot_name) { "boty" }
@@ -25,25 +25,35 @@ module Boty
25
25
  let(:current_user) { user_name }
26
26
  let(:current_user_id) { user_id }
27
27
 
28
- before do
29
- @dir = Dir.pwd
30
- dry_run
31
- capture(:stdout) do
32
- create_bot bot_name, company: "omg", api_key: "lol"
33
- end
34
- end
35
-
36
28
  after do
37
- Dir.chdir @dir
38
29
  Boty.locale = :en
39
30
  end
40
31
 
41
- it "creates the bot directory" do
42
- expect(Dir.exists? bot_name).to eq true
32
+ context "creating a new project from template" do
33
+ it "creates the project" do
34
+ capture(:stdout) do
35
+ expect(cli).to receive(:run).with "chmod +x bot"
36
+ expect(cli).to receive(:run).with "bundle install"
37
+ allow(cli).to receive(:ask).and_return "omg", "lol"
38
+ cli.new bot_name
39
+ end
40
+ expect(Dir.exists? bot_name).to eq true
41
+
42
+ Dir.chdir bot_name
43
+
44
+ expect(File.read ".env.local").to eq %{SLACK_COMPANY=omg
45
+ SLACK_BOT_API_TOKEN=lol
46
+ }
47
+ end
43
48
  end
44
49
 
45
50
  context "on the bot directory" do
46
51
  before do
52
+ capture(:stdout) do
53
+ dry_run
54
+ create_bot bot_name, company: "omg", api_key: "lol"
55
+ end
56
+
47
57
  Dir.chdir bot_name
48
58
  end
49
59
 
@@ -5,18 +5,24 @@ RSpec.describe "I18n on default scripts", :session do
5
5
 
6
6
  it "describes scripts in :en locale (default)" do
7
7
  session = Boty::Session.new
8
+ _know_how = nil
8
9
  session.start do |bot|
9
- expect(bot.know_how["knows"]).
10
- to eq "List all the commands known by this bot."
10
+ _know_how = know_how
11
11
  end
12
+
13
+ expect(_know_how["knows"]).
14
+ to eq "List all the commands known by this bot."
12
15
  end
13
16
 
14
17
  it "describes scripts in :pt-br local when forced" do
15
18
  Boty.locale = :"pt-br"
16
19
  session = Boty::Session.new
20
+ _know_how = nil
17
21
  session.start do |bot|
18
- expect(bot.know_how["knows"]).
19
- to eq "Lista os comandos conhecidos por esse bot."
22
+ _know_how = know_how
20
23
  end
24
+
25
+ expect(_know_how["knows"]).
26
+ to eq "Lista os comandos conhecidos por esse bot."
21
27
  end
22
28
  end
@@ -9,7 +9,7 @@ RSpec.describe "script/pug", :session do
9
9
  before do
10
10
  requests.get("/random") { |env|
11
11
  [200,
12
- { "Content-Type" => "application/json;charset=utf-8" },
12
+ {"Content-Type" => "application/json;charset=utf-8"},
13
13
  {pug: "some_pug"}.to_json]
14
14
  }
15
15
  end
data/spec/spec_helper.rb CHANGED
@@ -18,8 +18,11 @@ RSpec.configure do |config|
18
18
  config.include ThorSupport, thor: true
19
19
  config.include TemplateProjectSupport, project: true
20
20
  config.include FaradaySupport, faraday: true
21
-
21
+ config.include LoggerSupport, logger: true
22
22
  config.include SlackSupport::Users, users: true
23
23
 
24
- Boty::Logger.adapter = Boty::Logger::Null.new
24
+ null_logger = Boty::Logger::Null.new
25
+ config.before do
26
+ Boty::Logger.adapter = null_logger
27
+ end
25
28
  end
@@ -0,0 +1,20 @@
1
+ module LoggerSupport
2
+ def self.included(base)
3
+ base.instance_eval do
4
+ let(:logger) { Boty::Logger.adapter }
5
+
6
+ before do
7
+ @adapter = Boty::Logger.adapter
8
+ Boty::Logger.adapter = Boty::Logger::Memory.new
9
+ end
10
+
11
+ around do |example|
12
+ example.run
13
+ end
14
+
15
+ after do
16
+ Boty::Logger.adapter = @adapter
17
+ end
18
+ end
19
+ end
20
+ end
@@ -10,8 +10,8 @@ module SessionSupport
10
10
  include FayeSupport
11
11
  include SlackSupport::Users
12
12
 
13
- let(:bot) { ::Boty::ScriptDSL.new @session.bot }
14
- let(:dsl) { bot }
13
+ let(:dsl) { ::Boty::DSL.new @session.bot }
14
+ let(:bot) { dsl.bot }
15
15
  let(:bot_name) { "jeeba" }
16
16
  let(:bot_id) { "U1234" }
17
17
  let(:rtm_ws_url) { "ws://example.org/slack/sse/url" }
@@ -1,21 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "./<%= bot_name %>"
3
3
 
4
- def set_locale(locale)
5
- begin
6
- Boty.locale = locale || :en
7
- rescue I18n::InvalidLocale
8
- Boty.locale = :en
9
- end
10
- Boty::Locale.reload
11
- end
12
-
13
- set_locale ARGV.pop
4
+ Boty.locale = ARGV.pop || :en
14
5
 
15
6
  session = Boty::Session.new
16
- session.start do |bot|
17
- bot.desc bot.name, I18n.t("template.presence", bot_name: bot.name)
18
- bot.hear(/#{bot.name}/i) do
7
+ session.start do
8
+ desc name, I18n.t("template.presence", bot_name: name)
9
+ hear(/#{name}/i) do
19
10
  next if message_from_me?
20
11
  say I18n.t "template.hello", user_name: user.name
21
12
  end
@@ -1,11 +1,11 @@
1
- respond(/ping/i) do
1
+ command(/ping/i) do
2
2
  say "pong"
3
3
  end
4
4
 
5
- respond(/echo (.*)$/i) do |message|
5
+ command(/echo (.*)$/i) do |message|
6
6
  say message.match[1]
7
7
  end
8
8
 
9
- respond(/time/i) do
9
+ command(/time/i) do
10
10
  say "reeepitaaa: #{Time.now}"
11
11
  end
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.17.1
4
+ version: 0.1.0
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-12-03 00:00:00.000000000 Z
11
+ date: 2015-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -173,23 +173,28 @@ files:
173
173
  - bin/console
174
174
  - bin/setup
175
175
  - boty.gemspec
176
+ - docs/images/readme-01-screen-integration.png
177
+ - docs/images/readme-02-screen-integration.png
178
+ - docs/images/readme-03-screen-integration.png
176
179
  - exe/boty
177
180
  - gemfiles/Gemfile.rbx
178
181
  - lib/boty.rb
179
182
  - lib/boty/action.rb
180
183
  - lib/boty/bot.rb
181
184
  - lib/boty/cli.rb
185
+ - lib/boty/dsl.rb
186
+ - lib/boty/eventable.rb
187
+ - lib/boty/http.rb
182
188
  - lib/boty/locale.rb
183
189
  - lib/boty/logger.rb
184
- - lib/boty/message.rb
185
190
  - lib/boty/rspec.rb
186
- - lib/boty/script_dsl.rb
187
191
  - lib/boty/script_loader.rb
188
192
  - lib/boty/session.rb
189
193
  - lib/boty/slack.rb
190
194
  - lib/boty/slack/channel.rb
191
195
  - lib/boty/slack/chat.rb
192
196
  - lib/boty/slack/im.rb
197
+ - lib/boty/slack/message.rb
193
198
  - lib/boty/slack/rtm.rb
194
199
  - lib/boty/slack/url.rb
195
200
  - lib/boty/slack/user.rb
@@ -204,12 +209,15 @@ files:
204
209
  - script/remember.rb
205
210
  - spec/boty/bot_spec.rb
206
211
  - spec/boty/cli_spec.rb
212
+ - spec/boty/dsl_spec.rb
213
+ - spec/boty/http_spec.rb
207
214
  - spec/boty/logger_spec.rb
208
- - spec/boty/message_spec.rb
209
215
  - spec/boty/rspec_spec.rb
216
+ - spec/boty/script_loader_spec.rb
210
217
  - spec/boty/session_spec.rb
211
218
  - spec/boty/slack/chat_spec.rb
212
219
  - spec/boty/slack/im_spec.rb
220
+ - spec/boty/slack/message_spec.rb
213
221
  - spec/boty/slack/rtm_spec.rb
214
222
  - spec/boty/slack/url_spec.rb
215
223
  - spec/boty/slack/users_spec.rb
@@ -224,6 +232,7 @@ files:
224
232
  - spec/support/faraday_support.rb
225
233
  - spec/support/faye_support.rb
226
234
  - spec/support/file_system_matchers.rb
235
+ - spec/support/logger_support.rb
227
236
  - spec/support/session_support.rb
228
237
  - spec/support/slack_support.rb
229
238
  - spec/support/template_project_support.rb
data/lib/boty/message.rb DELETED
@@ -1,27 +0,0 @@
1
- module Boty
2
- class Message
3
- attr_accessor :text, :user, :channel, :ts, :team
4
- attr_reader :match
5
-
6
- def initialize(data, match: nil)
7
- @text = data["text"]
8
- @user = Slack.users.info data["user"]
9
- @channel = data["channel"]
10
- @ts = data["ts"]
11
- @team = data["team"]
12
- @match = match
13
- end
14
-
15
- def match!(regex)
16
- @match = regex.match @text
17
- end
18
-
19
- def from?(author)
20
- if author.respond_to? :id
21
- @user.id == author.id
22
- else
23
- @user.id == author
24
- end
25
- end
26
- end
27
- end
@@ -1,80 +0,0 @@
1
- require "forwardable"
2
-
3
- module Boty
4
- class ScriptDSL
5
- INSTANCES = {}
6
- private_constant :INSTANCES
7
- class << self
8
- alias original_constructor new
9
- end
10
-
11
- attr_accessor :bot
12
-
13
- extend Forwardable
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
21
-
22
- def initialize(bot)
23
- @bot = bot
24
- end
25
-
26
- def message
27
- @bot.trigger_message
28
- end
29
-
30
- def message_from_me?
31
- message.from? @bot
32
- end
33
-
34
- def hear(*args, &block)
35
- match(*args, &block)
36
- end
37
-
38
- def command(*args, &block)
39
- respond(*args, &block)
40
- end
41
-
42
- def match_im(*args, &block)
43
- command(*args, &block)
44
- end
45
-
46
- def http
47
- @http ||= HTTP.new
48
- end
49
- end
50
-
51
- class HTTP
52
- [:get, :post, :put, :delete, :head, :patch, :options].each do |verb|
53
- define_method verb do |url, params = {}|
54
- response = connection verb, url, params
55
- handle_response response
56
- end
57
- end
58
-
59
- private
60
-
61
- def connection(verb, url, params)
62
- uri = URI url
63
- Faraday.new(url: "#{uri.scheme}://#{uri.host}") { |builder|
64
- builder.adapter(*Faraday.default_adapter)
65
- }.public_send(verb) { |req|
66
- req.url uri.path, params
67
- }
68
- end
69
-
70
- def handle_response(response)
71
- # TODO: use a response parser accordingly to the
72
- body = response.body
73
- if /application\/json/.match response.headers["Content-Type"]
74
- JSON.parse body
75
- else
76
- body
77
- end
78
- end
79
- end
80
- end
@@ -1,32 +0,0 @@
1
- module Boty
2
- RSpec.describe Message, :users do
3
- subject(:message) { described_class.new message_json }
4
-
5
- let(:user_id) { "U7777" }
6
- let(:user_name) { "julian" }
7
-
8
- let(:message_json) {{
9
- "type" => "message",
10
- "text" => "omg lol bbq",
11
- "user" => "U7777",
12
- "channel" => "333",
13
- "ts" => "1234",
14
- "team" => "3452"
15
- }}
16
-
17
- it "uses Slack.users.info to create a User instance" do
18
- expect(message.user.id).to eq "U7777"
19
- expect(message.user.name).to eq "julian"
20
- end
21
-
22
- describe "#from?" do
23
- it "returns true if the param is the message author" do
24
- expect(message.from? "U7777").to eq true
25
- end
26
-
27
- it "returns true if the param `respond_to? :id` returning the message author" do
28
- expect(message.from? OpenStruct.new(id: "U7777")).to eq true
29
- end
30
- end
31
- end
32
- end