class Ruboty::Adapters::Idobata

Public Instance Methods

auth_payload() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 106
def auth_payload
  {
    socket_id:    socket_id,
    channel_name: channel_name
  }
end
authorize() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 114
def authorize
  req = Net::HTTP::Post.new(idobata_pusher_auth_url.path, headers)
  req.form_data = auth_payload
  https = Net::HTTP.new(idobata_pusher_auth_url.host, idobata_pusher_auth_url.port)
  https.use_ssl = true
  https.start {|https| https.request(req) }
end
bot() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 96
def bot
  records["bot"]
end
channel() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 153
def channel
  socket.channels.add(channel_name)
end
channel_name() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 101
def channel_name
  bot["channel_name"]
end
connect() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 138
def connect
  socket.connect
end
connected?() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 143
def connected?
  socket.connected
end
headers() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 73
def headers
  {
    'X-API-Token' => idobata_api_token,
    'User-Agent'  => "ruboty-idobata / v#{Ruboty::Idobata::VERSION}"
  }
end
idobata_api_token() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 53
def idobata_api_token
  ENV["IDOBATA_API_TOKEN"]
end
idobata_messages_url() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 68
def idobata_messages_url
  URI.join(idobata_url, '/api/messages')
end
idobata_pusher_auth_url() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 63
def idobata_pusher_auth_url
  URI.join(idobata_url, '/pusher/auth')
end
idobata_pusher_key() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 48
def idobata_pusher_key
  ENV["IDOBATA_PUSHER_KEY"] || "44ffe67af1c7035be764"
end
idobata_seed_url() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 58
def idobata_seed_url
  URI.join(idobata_url, '/api/seed')
end
idobata_url() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 43
def idobata_url
  URI.parse(ENV["IDOBATA_URL"] || "https://idobata.io/")
end
join_channel() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 158
def join_channel
  socket.authorize_callback(channel, pusher_auth["auth"], pusher_auth["channel_data"])
end
listen() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 163
def listen
  channel.bind('message:created') do |message_json|
    message = JSON.parse(message_json)['message']
    robot.receive(
      body: message['body_plain'],
      from: message['sender_id'],
      from_name: message['sender_name'],
      room_id: message['room_id'],
      room_name: message['room_name']
    )
  end
end
log_message() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 177
def log_message
  channel.bind('message:created') do |message_json|
    pp JSON.parse message_json
  end
end
on_connection_established(&block) click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 38
def on_connection_established(&block)
  socket.bind('pusher:connection_established', &block)
end
pusher_auth() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 128
def pusher_auth
  JSON.parse pusher_auth_json
end
pusher_auth_json() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 123
def pusher_auth_json
  authorize.body
end
records() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 91
def records
  seed["records"]
end
run() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 16
def run
  on_connection_established do
    join_channel
    log_message
    listen
  end
  connect
end
say(message) click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 25
def say(message)
  pp message
  req = Net::HTTP::Post.new(idobata_messages_url.path, headers)
  req.form_data = {
    'message[room_id]' => message[:original][:room_id],
    'message[source]'  => message[:code] ? "```\n#{message[:body]}\n```" : message[:body],
    'message[format]'  => 'markdown'
  }
  https = Net::HTTP.new(idobata_messages_url.host, idobata_messages_url.port)
  https.use_ssl = true
  https.start {|https| https.request(req) }
end
seed() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 86
def seed
  JSON.parse(seed_json)
end
seed_json() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 81
def seed_json
  idobata_seed_url.read(headers)
end
socket() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 133
def socket
  PusherClient::Socket.new(idobata_pusher_key, encrypted: true)
end
socket_id() click to toggle source
# File lib/ruboty/adapters/idobata.rb, line 148
def socket_id
  socket.socket_id
end