Class: Lita::Adapters::Flowdock::Connector
- Inherits:
-
Object
- Object
- Lita::Adapters::Flowdock::Connector
- Defined in:
- lib/lita/adapters/flowdock/connector.rb
Instance Method Summary collapse
-
#initialize(robot, api_token, organization, flows, flowdock_client = nil) ⇒ Connector
constructor
A new instance of Connector.
- #run(url = stream_url, queue = nil) ⇒ Object
- #send_messages(target, messages, message_id = nil) ⇒ Object
- #shut_down ⇒ Object
Constructor Details
#initialize(robot, api_token, organization, flows, flowdock_client = nil) ⇒ Connector
Returns a new instance of Connector.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lita/adapters/flowdock/connector.rb', line 12 def initialize(robot, api_token, organization, flows, flowdock_client=nil) @robot = robot @api_token = api_token @organization = organization @flows = flows @client = flowdock_client || ::Flowdock::Client.new(api_token: api_token) @stream_url = "https://#{api_token}@stream.flowdock.com/flows?filter=#{request_flows}" UsersCreator.create_users(client.get('/users')) end |
Instance Method Details
#run(url = stream_url, queue = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/lita/adapters/flowdock/connector.rb', line 25 def run(url=stream_url, queue=nil) EM.run do @source = EventMachine::EventSource.new( url, {query: 'text/event-stream'}, {'Accept' => 'text/event-stream'} ) source.open do log.info('Connected to flowdock streaming API') robot.trigger(:connected) end source. do || event = MultiJson.load() (event) end source.error do |error| log.error(error) robot.trigger(:disconnected) log.info('Disconnected') end source.start queue << source if queue end end |
#send_messages(target, messages, message_id = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/lita/adapters/flowdock/connector.rb', line 54 def (target, , = nil) .each do || client.( flow: target, content: , message: ) end end |
#shut_down ⇒ Object
64 65 66 67 |
# File 'lib/lita/adapters/flowdock/connector.rb', line 64 def shut_down source.close EM.stop if EM.reactor_running? end |