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
52
53
54
55
56
57
# 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    = find_user(*message.values_at('sender_id', 'sender_name', 'sender_type'))
      source  = Source.new(user: user, room: message["room_id"])
      # `message["body_plain"]` is nil on image upload
      message = Message.new(robot, message["body_plain"].to_s, source)
      robot.receive(message)
    end
  end

  socket.bind('disconnected') do
    sleep 5
    socket.connect(true)
  end
end

#message(target, strings) ⇒ Object



59
60
61
62
63
64
# File 'lib/lita/adapters/idobata/connector.rb', line 59

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

#roomsObject



66
67
# File 'lib/lita/adapters/idobata/connector.rb', line 66

def rooms
end

#shut_downObject



69
70
# File 'lib/lita/adapters/idobata/connector.rb', line 69

def shut_down
end