lita-flowdock 0.1.2 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 838b561f440f09f8aa0979fdb4d3b13ba90d7977
4
- data.tar.gz: 860cf748150b895e1f43084eb05817887e19ad2b
3
+ metadata.gz: da12f1db04490cd7aae9b115c0279377ca28adee
4
+ data.tar.gz: 2939919dbfc81e1f5d52f4d04f2cb3644b158ae9
5
5
  SHA512:
6
- metadata.gz: bb8cc72b3421a7a4a64421bc40ac3ffd90313e6f1b8a00e64893c4aad1932e16e5514d1b503ce69dc88e03a1a7b57e996f42281cd0b320784be939b9cfca5ce4
7
- data.tar.gz: 517866c822c741dfbe6c3e627216c835aa4fc1f1dd1ab8460f106934d3ff22eb889a5a20e94799c1a68ee3d24719e70619d90d65f0abc8f4884d3d319f73db03
6
+ metadata.gz: d5836b2e3558a090f8f71976874ae0825f32566510b4658837cb3ffe8c04dd6e4e30bda07f4e54865d12af114ebb6d6bbe89711246ab619bc9f392e7b004776f
7
+ data.tar.gz: d2d4f7094d1dc4610a3ba72f554f9775e9ddea01e90af6eca6c4e95f1c6bd3f4f905728439012c9aa622b6953d5923cad83c98628c489c7fcf90e39c1cc5dffd
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ### v0.2.0
2
+
3
+ * add option to thread responses in Flowdock
4
+ * enabled by default
5
+ * disable by setting `config.adapters.flowdock.thread_responses = :disabled`
data/GETTING_STARTED.md CHANGED
@@ -41,7 +41,7 @@ Lita.configure do |config|
41
41
  config.robot.mention_name = "!"
42
42
  config.robot.log_level = :debug
43
43
  config.robot.adapter = :flowdock
44
- config.adapters.flowdock.api_key = "$FLOWDOCK_API_TOKEN"
44
+ config.adapters.flowdock.api_token = "$FLOWDOCK_API_TOKEN"
45
45
  config.adapters.flowdock.organization = "$ORG"
46
46
  config.adapters.flowdock.flows = ["$FLOW_NAME"]
47
47
  end
data/README.md CHANGED
@@ -33,6 +33,12 @@ For quick setup, see the [Getting Started](https://github.com/bhouse/lita-flowdo
33
33
  * `organization` (String) - The organization for the flowdock account
34
34
  * `flows` (Array) - Array of flows the bot should connect to, i.e. `main`
35
35
 
36
+ ### Optional Attributes
37
+ * `thread_responses` (Symbol) - Respond to the original message in a Flowdock
38
+ thread
39
+ * default: `:enabled`
40
+ * other values: `:disabled`
41
+
36
42
  ### Example
37
43
 
38
44
  #### lita_config.rb
@@ -9,6 +9,7 @@ module Lita
9
9
  config :api_token, type: String, required: true
10
10
  config :organization, type: String, required: true
11
11
  config :flows, type: Array, required: true
12
+ config :thread_responses, type: Symbol, required: false, default: :enabled
12
13
 
13
14
 
14
15
  def mention_format(name)
@@ -25,20 +26,19 @@ module Lita
25
26
  )
26
27
 
27
28
  connector.run
28
- rescue Interrupt
29
- shut_down
30
29
  end
31
30
 
32
31
  def shut_down
33
32
  return unless connector
34
33
  connector.shut_down
35
- rescue RuntimeError
36
- robot.trigger(:disconnected)
37
- log.info('Disconnected')
38
34
  end
39
35
 
40
36
  def send_messages(target, messages)
41
- connector.send_messages(target.room, messages)
37
+ if config.thread_responses.eql?(:enabled)
38
+ connector.send_messages(target.room, messages, target.message_id)
39
+ else
40
+ connector.send_messages(target.room, messages)
41
+ end
42
42
  end
43
43
 
44
44
  private
@@ -16,14 +16,16 @@ module Lita
16
16
  @flows = flows
17
17
  @client =
18
18
  flowdock_client || ::Flowdock::Client.new(api_token: api_token)
19
+ @stream_url =
20
+ "https://#{api_token}@stream.flowdock.com/flows?filter=#{request_flows}"
19
21
 
20
22
  UsersCreator.create_users(client.get('/users'))
21
23
  end
22
24
 
23
- def run
25
+ def run(url=stream_url, queue=nil)
24
26
  EM.run do
25
27
  @source = EventMachine::EventSource.new(
26
- "https://#{api_token}@stream.flowdock.com/flows?filter=#{request_flows}",
28
+ url,
27
29
  {query: 'text/event-stream'},
28
30
  {'Accept' => 'text/event-stream'}
29
31
  )
@@ -39,27 +41,34 @@ module Lita
39
41
  end
40
42
 
41
43
  source.error do |error|
42
- log.error(error.inspect)
43
- EM.stop
44
+ log.error(error)
45
+ robot.trigger(:disconnected)
46
+ log.info('Disconnected')
44
47
  end
45
48
 
46
49
  source.start
50
+ queue << source if queue
47
51
  end
48
52
  end
49
53
 
50
- def send_messages(target, messages)
54
+ def send_messages(target, messages, message_id = nil)
51
55
  messages.each do |message|
52
- client.chat_message(flow: target, content: message)
56
+ client.chat_message(
57
+ flow: target,
58
+ content: message,
59
+ message: message_id
60
+ )
53
61
  end
54
62
  end
55
63
 
56
64
  def shut_down
57
65
  source.close
66
+ EM.stop if EM.reactor_running?
58
67
  end
59
68
 
60
69
  private
61
70
  attr_reader :robot, :api_token, :organization, :flows, :source,
62
- :client
71
+ :client, :stream_url
63
72
 
64
73
  def log
65
74
  Lita.logger
@@ -1,4 +1,5 @@
1
1
  require 'lita/adapters/flowdock/users_creator'
2
+ require 'lita/source/flowdock_source'
2
3
 
3
4
  module Lita
4
5
  module Adapters
@@ -33,7 +34,7 @@ module Lita
33
34
  end
34
35
 
35
36
  def dispatch_message(user)
36
- source = Source.new(user: user, room: flow)
37
+ source = FlowdockSource.new(user: user, room: flow, message_id: id)
37
38
  message = Message.new(robot, body, source)
38
39
  robot.receive(message)
39
40
  end
@@ -42,6 +43,10 @@ module Lita
42
43
  data['flow']
43
44
  end
44
45
 
46
+ def id
47
+ data['id']
48
+ end
49
+
45
50
  def from_self?(user)
46
51
  user.id.to_i == robot_id
47
52
  end
@@ -0,0 +1,10 @@
1
+ module Lita
2
+ class FlowdockSource < Source
3
+ attr_reader :message_id
4
+
5
+ def initialize(user: nil, room: nil, private_message: false, message_id: nil)
6
+ super(user: user, room: room, private_message: private_message)
7
+ @message_id = message_id
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-flowdock"
3
- spec.version = "0.1.2"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["Ben House"]
5
5
  spec.email = ["ben@benhouse.io"]
6
6
  spec.description = %q{flowdock adapter for lita.io}
@@ -1,7 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Lita::Adapters::Flowdock::Connector, lita: true do
4
- subject { described_class }
4
+ def with_eventsource(subject, queue)
5
+ thread = Thread.new { subject.run(url, queue) }
6
+ thread.abort_on_exception = true
7
+ yield queue.pop
8
+ subject.shut_down
9
+ thread.join
10
+ end
11
+
12
+ subject {
13
+ described_class.new(robot, api_token, organization, flows, fd_client)
14
+ }
5
15
 
6
16
  let(:registry) { Lita::Registry.new }
7
17
  let(:robot) { Lita::Robot.new(registry) }
@@ -10,8 +20,12 @@ describe Lita::Adapters::Flowdock::Connector, lita: true do
10
20
  let(:flows) { ['testing'] }
11
21
  let(:fd_client) { instance_double('Flowdock::Client') }
12
22
  let(:users) { [ user_hash(1), user_hash(2) ] }
23
+ let(:queue) { Queue.new }
24
+ let(:url) { "http://example.com" }
13
25
 
14
26
  describe "#new" do
27
+ subject { described_class }
28
+
15
29
  it "creates users" do
16
30
  expect(fd_client).to receive(:get).with('/users').and_return(users)
17
31
  expect(Lita::Adapters::Flowdock::UsersCreator).to receive(
@@ -22,22 +36,35 @@ describe Lita::Adapters::Flowdock::Connector, lita: true do
22
36
  end
23
37
 
24
38
  describe "#run" do
39
+ before do
40
+ allow(fd_client).to receive(:get).with('/users').and_return([])
41
+ end
42
+
43
+ it "starts the reactor" do
44
+ with_eventsource(subject, queue) do
45
+ expect(EM.reactor_running?).to be_truthy
46
+ end
47
+ end
48
+
49
+ it "creates the event source" do
50
+ with_eventsource(subject, queue) do |source|
51
+ expect(source).to be_an_instance_of(EventMachine::EventSource)
52
+ end
53
+ end
25
54
  end
26
55
 
27
56
  describe "#send_messages" do
28
57
  let(:target) { 'testing:lita-test' }
29
58
  let(:message) { 'foo' }
30
- subject {
31
- described_class.new(robot, api_token, organization, flows, fd_client)
32
- }
59
+ let(:message_id) { 1234 }
33
60
 
34
61
  before do
35
62
  allow(fd_client).to receive(:get).with('/users').and_return(users)
36
63
  end
37
64
 
38
65
  it "sends messages" do
39
- expect(fd_client).to receive(:chat_message).with(flow: target, content: message)
40
- subject.send_messages(target, [message])
66
+ expect(fd_client).to receive(:chat_message).with(flow: target, content: message, message: message_id)
67
+ subject.send_messages(target, [message], message_id)
41
68
  end
42
69
  end
43
70
  end
@@ -25,23 +25,26 @@ describe Lita::Adapters::Flowdock::MessageHandler, lita: true do
25
25
 
26
26
  describe "#handle" do
27
27
  context "a normal message" do
28
+ let(:id) { 2345 }
28
29
  let(:data) do
29
30
  {
30
31
  'content' => 'Hello World!',
31
32
  'event' => 'message',
32
33
  'flow' => test_flow,
34
+ 'id' => id,
33
35
  'user' => test_user_id
34
36
  }
35
37
  end
36
38
  let(:message) { instance_double('Lita::Message', command!: false) }
37
- let(:source) { instance_double('Lita::Source', private_message?: false) }
39
+ let(:source) { instance_double('Lita::FlowdockSource', private_message?: false, message_id: id) }
38
40
  let(:user) { user_double(test_user_id) }
39
41
 
40
42
  before do
41
43
  allow(Lita::User).to receive(:find_by_id).and_return(user)
42
- allow(Lita::Source).to receive(:new).with(
44
+ allow(Lita::FlowdockSource).to receive(:new).with(
43
45
  user: user,
44
- room: test_flow
46
+ room: test_flow,
47
+ message_id: id
45
48
  ).and_return(source)
46
49
  allow(Lita::Message).to receive(:new).with(
47
50
  robot, 'Hello World!', source).and_return(message)
@@ -60,6 +63,7 @@ describe Lita::Adapters::Flowdock::MessageHandler, lita: true do
60
63
  'event' => 'message',
61
64
  'flow' => test_flow,
62
65
  'user' => test_user_id,
66
+ 'id' => id
63
67
  }
64
68
  end
65
69
 
@@ -45,17 +45,30 @@ describe Lita::Adapters::Flowdock, lita: true do
45
45
  end
46
46
 
47
47
  describe "#send_messages" do
48
- let(:room_source) { Lita::Source.new(room: '1234abcd') }
48
+ let(:id) { 8888 }
49
+ let(:room_source) { Lita::FlowdockSource.new(room: '1234abcd', message_id: id) }
49
50
  let(:user) { Lita::User.new('987654') }
50
51
  let(:user_source) { Lita::Source.new(user: user) }
51
52
 
52
53
  it "sends messages to flows" do
53
- expect(connector).to receive(:send_messages).with(room_source.room, ['foo'])
54
+ expect(connector).to receive(:send_messages).with(room_source.room, ['foo'], room_source.message_id)
54
55
 
55
56
  subject.run
56
57
 
57
58
  subject.send_messages(room_source, ['foo'])
58
59
  end
60
+
61
+ context "with thread_responses disabled" do
62
+ before do
63
+ registry.config.adapters.flowdock.thread_responses = :disabled
64
+ end
65
+
66
+ it "sends messages to flow without the original message id" do
67
+ expect(connector).to receive(:send_messages).with(room_source.room, ['foo'])
68
+ subject.run
69
+ subject.send_messages(room_source, ['foo'])
70
+ end
71
+ end
59
72
  end
60
73
 
61
74
  describe "#shut_down" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-flowdock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben House
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-08 00:00:00.000000000 Z
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -173,6 +173,7 @@ extra_rdoc_files: []
173
173
  files:
174
174
  - ".gitignore"
175
175
  - ".travis.yml"
176
+ - CHANGELOG.md
176
177
  - GETTING_STARTED.md
177
178
  - Gemfile
178
179
  - LICENSE
@@ -183,6 +184,7 @@ files:
183
184
  - lib/lita/adapters/flowdock/connector.rb
184
185
  - lib/lita/adapters/flowdock/message_handler.rb
185
186
  - lib/lita/adapters/flowdock/users_creator.rb
187
+ - lib/lita/source/flowdock_source.rb
186
188
  - lita-flowdock.gemspec
187
189
  - locales/en.yml
188
190
  - spec/lita/adapters/flowdock/connector_spec.rb