Class: HipChat::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
# File 'lib/hipchat/client.rb', line 8

def initialize(token, options={})
  @token = token
  default_options = { api_version: 'v2', server_url: 'https://api.hipchat.com' }
  @options = default_options.merge options
  @api_version = @options[:api_version]
  @api = HipChat::ApiVersion::Client.new(@options)
  self.class.base_uri(@api.base_uri)
  http_proxy = @options[:http_proxy] || ENV['http_proxy']
  setup_proxy(http_proxy) if http_proxy
end

Instance Method Details

#[](name) ⇒ Object



23
24
25
# File 'lib/hipchat/client.rb', line 23

def [](name)
  HipChat::Room.new(@token, { room_id: name, :api_version => @api_version, :server_url => @options[:server_url] })
end

#create_room(name, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hipchat/client.rb', line 64

def create_room(name, options={})
  if @api.version == 'v1' && options[:owner_user_id].nil?
    raise RoomMissingOwnerUserId, 'V1 API Requires owner_user_id'
  end

  if name.length > 50
    raise RoomNameTooLong, "Room name #{name} is #{name.length} characters long. Limit is 50."
  end
  unless options[:guest_access].nil?
    options[:guest_access] = @api.bool_val(options[:guest_access])
  end

  response = self.class.post(@api.create_room_config[:url],
    :query => { :auth_token => @token },
    :body => {
      :name => name
      }.merge(options).send(@api.create_room_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :room, name, response
  response.parsed_response
end

#create_user(name, email, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hipchat/client.rb', line 88

def create_user(name, email, options={})
  if name.length > 50
    raise UsernameTooLong, "User name #{name} is #{name.length} characters long. Limit is 50."
  end

  response = self.class.post(@api.create_user_config[:url],
    :query => { :auth_token => @token },
    :body => {
      :name => name,
      :email => email
      }.merge(options).send(@api.create_user_config[:body_format]),
    :headers => @api.headers
  )

  ErrorHandler.response_code_to_exception_for :user, email, response
  response.parsed_response
end

#roomsObject



19
20
21
# File 'lib/hipchat/client.rb', line 19

def rooms
  @rooms ||= _rooms
end

#scopes(room: nil) ⇒ Object

Returns the scopes for the Auth token

Calls the endpoint:

https://api.hipchat.com/v2/oauth/token/#{token}

The response is a JSON object containing a client key. The client
object contains a list of allowed scopes.

There are two possible response types, for a global API token, the
room object will be nil. For a room API token, the room object will
be populated:

Optional room parameter can be passed in. The method will return the following:

- if it's a global API token and room param is nil: scopes
- if it's a global API token and room param is not nil: scopes
- if it's a room API token and room param is nil: nil
- if it's a room API token and room param is not nil
    - if room param's room_id matches token room_id: scopes
    - if room param's room_id doesn't match token room_id: nil

Raises errors if response is unrecognizable



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hipchat/client.rb', line 51

def scopes(room: nil)
  path = "#{@api.scopes_config[:url]}/#{URI::escape(@token)}"
  response = self.class.get(path,
    :query => { :auth_token => @token },
    :headers => @api.headers
  )
  ErrorHandler.response_code_to_exception_for :room, 'scopes', response
  return response['scopes'] unless response['client']['room']
  if room && response['client']['room']['id'] == room.room_id
    return response['scopes']
  end
end

#user(name) ⇒ Object



106
107
108
# File 'lib/hipchat/client.rb', line 106

def user(name)
  HipChat::User.new(@token, { :user_id => name, :api_version => @api_version, :server_url => @options[:server_url] })
end

#usersObject



110
111
112
# File 'lib/hipchat/client.rb', line 110

def users
  @users ||= _users
end