Class: HipChat::Room

Inherits:
OpenStruct
  • Object
show all
Includes:
HTTParty
Defined in:
lib/hipchat.rb

Instance Method Summary collapse

Constructor Details

#initialize(token, params) ⇒ Room

Returns a new instance of Room.



36
37
38
39
40
# File 'lib/hipchat.rb', line 36

def initialize(token, params)
  @token = token

  super(params)
end

Instance Method Details

#send(from, message, options_or_notify = {}) ⇒ Object

Send a message to this room.

Usage:

# Default
send 'nickname', 'some message'

# Notify users and color the message red
send 'nickname', 'some message', :notify => true, :color => 'red'

# Notify users (deprecated)
send 'nickname', 'some message', true

Options:

color

“yellow”, “red”, “green”, “purple”, or “random” (default “yellow”)

notify

true or false (default false)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hipchat.rb', line 61

def send(from, message, options_or_notify = {})
  options = if options_or_notify == true or options_or_notify == false
    warn "DEPRECATED: Specify notify flag as an option (e.g., :notify => true)"
    { :notify => options_or_notify }
  else
    options_or_notify || {}
  end

  options = { :color => 'yellow', :notify => false }.merge options

  response = self.class.post('/message',
    :query => { :auth_token => @token },
    :body  => {
      :room_id => room_id,
      :from    => from,
      :message => message,
      :color   => options[:color],
      :notify  => options[:notify] ? 1 : 0
    }
  )

  case response.code
  when 200; true
  when 404
    raise UnknownRoom,  "Unknown room: `#{room_id}'"
  when 401
    raise Unauthorized, "Access denied to room `#{room_id}'"
  else
    raise UnknownResponseCode, "Unexpected #{response.code} for room `#{room_id}'"
  end
end