class HipChat::Room

Public Class Methods

new(token, params) click to toggle source
Calls superclass method
# File lib/hipchat/room.rb, line 11
def initialize(token, params)
  @token = token
  @api = HipChat::ApiVersion::Room.new(params)
  self.class.base_uri(@api.base_uri)
  super(params)
end

Public Instance Methods

get_room() click to toggle source

Retrieve data for this room

# File lib/hipchat/room.rb, line 19
def get_room
  response = self.class.get(@api.get_room_config[:url],
    :query => {:auth_token => @token },
    :headers => @api.headers
  )

  case response.code
  when 200
    response.parsed_response
  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
history(options = {}) click to toggle source

Pull this room's history

Usage

# Default

Options

date

Whether to return a specific day (YYYY-MM-DD format) or recent

(default "recent")
timezone

Your timezone. Supported timezones are at: www.hipchat.com/docs/api/timezones

(default "UTC")
format

Format to retrieve the history in. Valid options are JSON and XML

(default "JSON")
# File lib/hipchat/room.rb, line 198
def history(options = {})

  options = { :date => 'recent', :timezone => 'UTC', :format => 'JSON' }.merge options

  response = self.class.get(@api.history_config[:url],
    :query => {
      :room_id    => room_id,
      :date       => options[:date],
      :timezone   => options[:timezone],
      :format     => options[:format],
      :auth_token => @token,
    },
    :headers => @api.headers
  )

  case response.code
  when 200
    response.body
  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
invite(user, reason="") click to toggle source

Invite user to this room

# File lib/hipchat/room.rb, line 71
def invite(user, reason="")
  response = self.class.post(@api.invite_config[:url]+"/#{user}",
    :query => { :auth_token => @token },
    :body => {
      :reason => reason
    }.to_json,
    :headers => @api.headers)

  case response.code
  when 200, 204; 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
send(from, message, options_or_notify = {}) click to toggle source

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)

# File lib/hipchat/room.rb, line 110
def send(from, message, options_or_notify = {})
  if from.length > 15
    raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 15'"
  end
  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(@api.send_config[:url],
    :query => { :auth_token => @token },
    :body  => {
      :room_id        => room_id,
      :from           => from,
      :message        => message,
      :message_format => options[:message_format] || 'html',
      :color          => options[:color],
      :notify         => @api.bool_val(options[:notify])
    }.send(@api.send_config[:body_format]),
    :headers => @api.headers
  )

  case response.code
  when 200, 204; 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
statistics(options = {}) click to toggle source

Pull this room's statistics

# File lib/hipchat/room.rb, line 226
def statistics(options = {})

  response = self.class.get(@api.statistics_config[:url],
    :query => {
      :room_id    => room_id,
      :date       => options[:date],
      :timezone   => options[:timezone],
      :format     => options[:format],
      :auth_token => @token,
    },
    :headers => @api.headers
  )

  case response.code
  when 200
    response.body
  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
topic(new_topic, options = {}) click to toggle source

Change this room's topic

Usage:

# Default
topic 'my awesome topic'

Options:

from

the name of the person changing the topic

(default "API")
# File lib/hipchat/room.rb, line 158
def topic(new_topic, options = {})

  options = { :from => 'API' }.merge options

  response = self.class.send(@api.topic_config[:method], @api.topic_config[:url],
    :query => { :auth_token => @token },
    :body  => {
      :room_id        => room_id,
      :from           => options[:from],
      :topic          => new_topic
    }.send(@api.topic_config[:body_format]),
    :headers => @api.headers
  )

  case response.code
  when 204,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
update_room(options = {}) click to toggle source

Update a room

# File lib/hipchat/room.rb, line 38
def update_room(options = {})
  options = {
    :privacy => "public",
    :is_archived => false,
    :is_guest_accessible => false
  }.merge symbolize(options)

  response = self.class.send(@api.topic_config[:method], @api.update_room_config[:url],
    :query => { :auth_token => @token },
    :body => {
      :name => options[:name],
      :topic => options[:topic],
      :privacy => options[:privacy],
      :is_archived => @api.bool_val(options[:is_archived]),
      :is_guest_accessible => @api.bool_val(options[:is_guest_accessible]),
      :owner => options[:owner]
    }.to_json,
    :headers => @api.headers)

  puts response.body

  case response.code
  when 200, 204; true
  when 404
    raise Unknown Room, "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

Private Instance Methods

symbolize(obj) click to toggle source
# File lib/hipchat/room.rb, line 252
def symbolize(obj)
  return obj.reduce({}) do |memo, (k, v)|
    memo.tap { |m| m[k.to_sym] = symbolize(v) }
  end if obj.is_a? Hash
    
  return obj.reduce([]) do |memo, v| 
    memo << symbolize(v); memo
  end if obj.is_a? Array
  obj
end