Class: Janky::ChatService::Hubot

Inherits:
Object
  • Object
show all
Defined in:
lib/janky/chat_service/hubot.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Hubot

Returns a new instance of Hubot.



4
5
6
7
8
9
10
11
12
# File 'lib/janky/chat_service/hubot.rb', line 4

def initialize(settings)
  @available_rooms = settings["JANKY_CHAT_HUBOT_ROOMS"]
  @default_room = settings["JANKY_CHAT_HUBOT_DEFAULT_ROOM"]
  url = settings["JANKY_CHAT_HUBOT_URL"]
  if url.nil? || url.empty?
    raise Error, "JANKY_CHAT_HUBOT_URL setting is required"
  end
  @url = URI(url)
end

Instance Method Details

#request(message, room) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/janky/chat_service/hubot.rb', line 26

def request(message, room)
  room ||= @default_room
  uri  = @url
  user = uri.user
  pass = uri.password
  path = File.join(uri.path, "janky")

  http = Net::HTTP.new(uri.host, uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
  end

  post = Net::HTTP::Post.new(path)
  post.basic_auth(user, pass) if user && pass
  post["Content-Type"] = "application/json"
  post.body = {:message => message, :room => room}.to_json
  response = http.request(post)
  unless response.code == "200"
    Exception.push_http_response(response)
    raise Error, "Failed to notify"
  end
end

#roomsObject



18
19
20
21
22
23
24
# File 'lib/janky/chat_service/hubot.rb', line 18

def rooms
  @available_rooms.split(',').map do |room|
    id, name = room.strip.split(':')
    name ||= id
    Room.new(id, name)
  end
end

#speak(message, room, options = {:color => "yellow"}) ⇒ Object



14
15
16
# File 'lib/janky/chat_service/hubot.rb', line 14

def speak(message, room, options = {:color => "yellow"})
  request(message, room)
end