Class: Lita::Adapters::Idobata::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/adapters/idobata/connector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/lita/adapters/idobata/connector.rb', line 12

def client
  @client
end

#robotObject (readonly)

Returns the value of attribute robot.



12
13
14
# File 'lib/lita/adapters/idobata/connector.rb', line 12

def robot
  @robot
end

#rosterObject (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

#connectObject



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']

  options = {
    encrypted: !!@idobata_url.match(/^https/),
    auth_method: auth_method
  }
  socket = PusherClient::Socket.new(@pusher_key, options)
  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|
    message = JSON.parse(data)["message"]
    if message["sender_id"] != @bot['id']
      user    = User.new(message["sender_id"], name: message["sender_name"])
      source  = Source.new(user: user, room: message["room_id"])
      message = Message.new(robot, message["body_plain"], source)
      robot.receive(message)
    end
  end
end

#message(target, strings) ⇒ Object



53
54
55
56
57
58
# File 'lib/lita/adapters/idobata/connector.rb', line 53

def message(target, strings)
  http_client.post '/api/messages', {
    'message[room_id]' => target.room,
    'message[source]' => strings.join("\n")
  }
end

#roomsObject



60
61
# File 'lib/lita/adapters/idobata/connector.rb', line 60

def rooms
end

#shut_downObject



63
64
# File 'lib/lita/adapters/idobata/connector.rb', line 63

def shut_down
end