Class: Lita::Adapters::Idobata::Connector
- Inherits:
-
Object
- Object
- Lita::Adapters::Idobata::Connector
- Defined in:
- lib/lita/adapters/idobata/connector.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#robot ⇒ Object
readonly
Returns the value of attribute robot.
-
#roster ⇒ Object
readonly
Returns the value of attribute roster.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(robot, api_token: nil, pusher_key: nil, idobata_url: nil, debug: false) ⇒ Connector
constructor
A new instance of Connector.
- #message(target, strings) ⇒ Object
- #rooms ⇒ Object
- #shut_down ⇒ Object
Constructor Details
#initialize(robot, api_token: nil, pusher_key: nil, idobata_url: nil, debug: false) ⇒ Connector
Returns a new instance of Connector.
14 15 16 17 18 19 20 21 |
# File 'lib/lita/adapters/idobata/connector.rb', line 14 def initialize(robot, api_token: nil, pusher_key: nil, idobata_url: nil, debug: false) @robot = robot @api_token = api_token @pusher_key = pusher_key @idobata_url = idobata_url @debug = debug Lita.logger.info("Enabling log.") if @debug end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/lita/adapters/idobata/connector.rb', line 12 def client @client end |
#robot ⇒ Object (readonly)
Returns the value of attribute robot.
12 13 14 |
# File 'lib/lita/adapters/idobata/connector.rb', line 12 def robot @robot end |
#roster ⇒ Object (readonly)
Returns the value of attribute roster.
12 13 14 |
# File 'lib/lita/adapters/idobata/connector.rb', line 12 def roster @roster end |
Instance Method Details
#connect ⇒ Object
23 24 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 |
# File 'lib/lita/adapters/idobata/connector.rb', line 23 def connect raise "api_token is requeired." unless @api_token response = http_client.get '/api/seed' seed = JSON.parse(response.body) @bot = seed['records']['bot'] channel_name = @bot['channel_name'] = { encrypted: !!@idobata_url.match(/^https/), auth_method: auth_method } socket = PusherClient::Socket.new(@pusher_key, ) socket.connect(true) socket.bind('pusher:connection_established') do |data| socket_id = JSON.parse(data)['socket_id'] socket.subscribe(channel_name, socket_id) end socket.bind('message_created') do |data| = JSON.parse(data)["message"] if ["sender_id"] != @bot['id'] user = User.new(["sender_id"], name: ["sender_name"]) source = Source.new(user: user, room: ["room_id"]) = Message.new(robot, ["body_plain"], source) robot.receive() end end end |
#message(target, strings) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/lita/adapters/idobata/connector.rb', line 53 def (target, strings) http_client.post '/api/messages', { 'message[room_id]' => target.room, 'message[source]' => strings.join("\n") } end |
#rooms ⇒ Object
60 61 |
# File 'lib/lita/adapters/idobata/connector.rb', line 60 def rooms end |
#shut_down ⇒ Object
63 64 |
# File 'lib/lita/adapters/idobata/connector.rb', line 63 def shut_down end |