Class: Kazoo::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kazoo-client/error.rb,
lib/kazoo-client/media.rb,
lib/kazoo-client/menus.rb,
lib/kazoo-client/users.rb,
lib/kazoo-client/common.rb,
lib/kazoo-client/limits.rb,
lib/kazoo-client/queues.rb,
lib/kazoo-client/devices.rb,
lib/kazoo-client/servers.rb,
lib/kazoo-client/version.rb,
lib/kazoo-client/vmboxes.rb,
lib/kazoo-client/accounts.rb,
lib/kazoo-client/callflows.rb,
lib/kazoo-client/resources.rb,
lib/kazoo-client/conferences.rb,
lib/kazoo-client/directories.rb,
lib/kazoo-client/phone_numbers.rb,
lib/kazoo-client/temporal_rules.rb

Defined Under Namespace

Classes: AuthError, DeleteError, Error, GetError, PostError, PutError, VersionError

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, version, username, password, realm) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/kazoo-client/common.rb', line 5

def initialize(base_url, version, username, password, realm)
  @base_url = base_url
  @username = username
  @password = password
  @realm = realm
  @version = version

  unless authenticated?
    authenticate
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def 
  @account_id
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def auth_token
  @auth_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def base_url
  @base_url
end

#realmObject (readonly)

Returns the value of attribute realm.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def realm
  @realm
end

#usernameObject (readonly)

Returns the value of attribute username.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def username
  @username
end

#versionObject (readonly)

Returns the value of attribute version.



3
4
5
# File 'lib/kazoo-client/common.rb', line 3

def version
  @version
end

Instance Method Details

#add_callflow(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/callflows.rb', line 11

def add_callflow(data)
  put_request('callflows', data)
end

#add_conference(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/conferences.rb', line 11

def add_conference(data)
  put_request('conferences', data)
end

#add_deployment(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/servers.rb', line 11

def add_deployment(data)
  put_request("servers/#{id}/deployment", data)
end

#add_device(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/devices.rb', line 11

def add_device(data)
  put_request('devices', data)
end

#add_directory(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/directories.rb', line 11

def add_directory(data)
  put_request('directories', data)
end

#add_media(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/media.rb', line 11

def add_media(data)
  put_request('media', data)
end

#add_menu(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/menus.rb', line 11

def add_menu(data)
  put_request('menus', data)
end

#add_queue(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/queues.rb', line 11

def add_queue(data)
  put_request('queues', data)
end

#add_resource(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/resources.rb', line 11

def add_resource(data)
  put_request('resources', data)
end

#add_temporal_rule(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/temporal_rules.rb', line 11

def add_temporal_rule(data)
  put_request('temporal_rules', data)
end

#add_user(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/users.rb', line 11

def add_user(data)
  put_request('users', data)
end

#add_vmbox(data) ⇒ Object



11
12
13
# File 'lib/kazoo-client/vmboxes.rb', line 11

def add_vmbox(data)
  put_request('vmboxes', data)
end

#authenticateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kazoo-client/common.rb', line 21

def authenticate
  credentials = Digest::MD5.hexdigest "#{@username}:#{@password}"
  auth_hash = {
    data: {
      credentials: credentials,
      account_name: @realm
    },
    verb: 'PUT'
  }
  response = HTTP.headers(content_type: 'application/json', accept: 'application/json')
                 .put("#{@base_url}/#{@version}/user_auth", json: auth_hash)
  result = JSON.parse(response.body)
  case response.code
  when (200..299)
    @account_id = result['data']['account_id']
    @auth_token = result['auth_token']
    @base_header = {
      content_type: 'application/json',
      accept: 'application/json',
      X_Auth_Token: @auth_token
    }
    return result['data']
  else
    raise AuthError, "Authentication error: #{response.status}"
  end
end

#authenticated?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/kazoo-client/common.rb', line 17

def authenticated?
  !@auth_token.nil?
end

#delete_callflow(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/callflows.rb', line 19

def delete_callflow(id)
  delete_request("callflows/#{id}")
end

#delete_conference(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/conferences.rb', line 19

def delete_conference(id)
  delete_request("conferences/#{id}")
end

#delete_device(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/devices.rb', line 19

def delete_device(id)
  delete_request("devices/#{id}")
end

#delete_directory(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/directories.rb', line 19

def delete_directory(id)
  delete_request("directories/#{id}")
end

#delete_media(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/media.rb', line 19

def delete_media(id)
  delete_request("media/#{id}")
end

#delete_menu(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/menus.rb', line 19

def delete_menu(id)
  delete_request("menus/#{id}")
end

#delete_phone_number(id) ⇒ Object



11
12
13
# File 'lib/kazoo-client/phone_numbers.rb', line 11

def delete_phone_number(id)
  delete_request("phone_numbers/#{id}")
end

#delete_queue(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/queues.rb', line 19

def delete_queue(id)
  delete_request("queues/#{id}")
end

#delete_request(url) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kazoo-client/common.rb', line 84

def delete_request(url)
  response = HTTP.headers(@base_header)
                 .delete("#{@base_url}/#{@version}/accounts/#{@account_id}/#{url}")
  result = JSON.parse(response.body)
  case response.code
  when (200..299)
    return result['data']
  else
    raise DeleteError, "Delete error: #{response.status} #{result['data']}"
  end
end

#delete_resource(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/resources.rb', line 19

def delete_resource(id)
  delete_request("resources/#{id}")
end

#delete_temporal_rule(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/temporal_rules.rb', line 19

def delete_temporal_rule(id)
  delete_request("temporal_rules/#{id}")
end

#delete_user(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/users.rb', line 19

def delete_user(id)
  delete_request("users/#{id}")
end

#delete_vmbox(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/vmboxes.rb', line 19

def delete_vmbox(id)
  delete_request("vmboxes/#{id}")
end

#get_account(id) ⇒ Object



3
4
5
# File 'lib/kazoo-client/accounts.rb', line 3

def (id)
  get_request("accounts/#{id}")
end

#get_account_children(id) ⇒ Object



11
12
13
# File 'lib/kazoo-client/accounts.rb', line 11

def (id)
  get_request("accounts/#{id}/children")
end

#get_account_descendants(id) ⇒ Object



15
16
17
# File 'lib/kazoo-client/accounts.rb', line 15

def (id)
  get_request("accounts/#{id}/descendants")
end

#get_account_siblings(id) ⇒ Object



19
20
21
# File 'lib/kazoo-client/accounts.rb', line 19

def (id)
  get_request("accounts/#{id}/siblings")
end

#get_callflow(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/callflows.rb', line 7

def get_callflow(id)
  get_request("callflows/#{id}")
end

#get_callflowsObject



3
4
5
# File 'lib/kazoo-client/callflows.rb', line 3

def get_callflows
  get_request('callflows')
end

#get_conference(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/conferences.rb', line 7

def get_conference(id)
  get_request("conferences/#{id}")
end

#get_conferencesObject



3
4
5
# File 'lib/kazoo-client/conferences.rb', line 3

def get_conferences
  get_request('conferences')
end

#get_deployment(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/servers.rb', line 7

def get_deployment(id)
  get_request("servers/#{id}/deployment")
end

#get_device(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/devices.rb', line 7

def get_device(id)
  get_request("devices/#{id}")
end

#get_devicesObject



3
4
5
# File 'lib/kazoo-client/devices.rb', line 3

def get_devices
  get_request('devices')
end

#get_devices_statusObject



23
24
25
# File 'lib/kazoo-client/devices.rb', line 23

def get_devices_status
  get_request('devices/status')
end

#get_directoriesObject



3
4
5
# File 'lib/kazoo-client/directories.rb', line 3

def get_directories
  get_request('directories')
end

#get_directory(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/directories.rb', line 7

def get_directory(id)
  get_request("directories/#{id}")
end

#get_hotdeskObject



23
24
25
# File 'lib/kazoo-client/users.rb', line 23

def get_hotdesk
  get_request('users/hotdesk')
end

#get_languagesObject



23
24
25
# File 'lib/kazoo-client/media.rb', line 23

def get_languages
  get_request('media/languages')
end

#get_languages_missingObject



27
28
29
# File 'lib/kazoo-client/media.rb', line 27

def get_languages_missing
  get_request('media/languages/missing')
end

#get_limitsObject



3
4
5
# File 'lib/kazoo-client/limits.rb', line 3

def get_limits
  get_request('limits')
end

#get_logs(id) ⇒ Object



15
16
17
# File 'lib/kazoo-client/servers.rb', line 15

def get_logs(id)
  get_request("servers/#{id}/log")
end

#get_media(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/media.rb', line 7

def get_media(id)
  get_request("media/#{id}")
end

#get_mediasObject



3
4
5
# File 'lib/kazoo-client/media.rb', line 3

def get_medias
  get_request('media')
end

#get_menu(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/menus.rb', line 7

def get_menu(id)
  get_request("menus/#{id}")
end

#get_menusObject



3
4
5
# File 'lib/kazoo-client/menus.rb', line 3

def get_menus
  get_request('menus')
end

#get_phone_numbersObject



3
4
5
# File 'lib/kazoo-client/phone_numbers.rb', line 3

def get_phone_numbers
  get_request('phone_numbers')
end

#get_queue(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/queues.rb', line 7

def get_queue(id)
  get_request("queues/#{id}")
end

#get_queuesObject



3
4
5
# File 'lib/kazoo-client/queues.rb', line 3

def get_queues
  get_request('queues')
end

#get_request(url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kazoo-client/common.rb', line 48

def get_request(url)
  response = HTTP.headers(@base_header)
                 .get("#{@base_url}/#{@version}/accounts/#{@account_id}/#{url}")
  result = JSON.parse(response.body)
  case response.code
  when (200..299)
    return result['data']
  else
    raise GetError, "Get error: #{response.status} #{result['data']}"
  end
end

#get_resource(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/resources.rb', line 7

def get_resource(id)
  get_request("resources/#{id}")
end

#get_resourcesObject



3
4
5
# File 'lib/kazoo-client/resources.rb', line 3

def get_resources
  get_request('resources')
end

#get_serversObject



3
4
5
# File 'lib/kazoo-client/servers.rb', line 3

def get_servers
  get_request('servers')
end

#get_temporal_rule(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/temporal_rules.rb', line 7

def get_temporal_rule(id)
  get_request("temporal_rules/#{id}")
end

#get_temporal_rulesObject



3
4
5
# File 'lib/kazoo-client/temporal_rules.rb', line 3

def get_temporal_rules
  get_request('temporal_rules')
end

#get_user(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/users.rb', line 7

def get_user(id)
  get_request("users/#{id}")
end

#get_usersObject



3
4
5
# File 'lib/kazoo-client/users.rb', line 3

def get_users
  get_request('users')
end

#get_vmbox(id) ⇒ Object



7
8
9
# File 'lib/kazoo-client/vmboxes.rb', line 7

def get_vmbox(id)
  get_request("vmboxes/#{id}")
end

#get_vmboxesObject



3
4
5
# File 'lib/kazoo-client/vmboxes.rb', line 3

def get_vmboxes
  get_request('vmboxes')
end

#post_request(url, data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kazoo-client/common.rb', line 72

def post_request(url, data)
  response = HTTP.headers(@base_header)
                 .post("#{@base_url}/#{@version}/accounts/#{@account_id}/#{url}", json: data)
  result = JSON.parse(response.body)
  case response.code
  when (200..299)
    return result['data']
  else
    raise PostError, "Post error: #{response.status} #{result['data']}"
  end
end

#put_request(url, data) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kazoo-client/common.rb', line 60

def put_request(url, data)
  response = HTTP.headers(@base_header)
                 .put("#{@base_url}/#{@version}/accounts/#{@account_id}/#{url}", json: data)
  result = JSON.parse(response.body)
  case response.code
  when (200..299)
    return result['data']
  else
    raise PutError, "Put error: #{response.status} #{result['data']}"
  end
end

#sync_device(id) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/kazoo-client/devices.rb', line 27

def sync_device(id)
  data = { data: {}, verb: 'POST' }
  if @version == 'v2'
    post_request("devices/#{id}/sync", data)
  else
    raise VersionError, 'sync_device is not implemented for v1 api'
  end
end

#update_account(id, data) ⇒ Object



7
8
9
# File 'lib/kazoo-client/accounts.rb', line 7

def (id, data)
  post_request("accounts/#{id}", data)
end

#update_callflow(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/callflows.rb', line 15

def update_callflow(id, data)
  post_request("callflows/#{id}", data)
end

#update_conference(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/conferences.rb', line 15

def update_conference(id, data)
  post_request("conferences/#{id}", data)
end

#update_device(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/devices.rb', line 15

def update_device(id, data)
  post_request("devices/#{id}", data)
end

#update_directory(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/directories.rb', line 15

def update_directory(id, data)
  post_request("directories/#{id}", data)
end

#update_media(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/media.rb', line 15

def update_media(id, data)
  post_request("media/#{id}", data)
end

#update_menu(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/menus.rb', line 15

def update_menu(id, data)
  post_request("menus/#{id}", data)
end

#update_phone_number(id, data) ⇒ Object



7
8
9
# File 'lib/kazoo-client/phone_numbers.rb', line 7

def update_phone_number(id, data)
  post_request("phone_numbers/#{id}", data)
end

#update_queue(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/queues.rb', line 15

def update_queue(id, data)
  post_request("queues/#{id}", data)
end

#update_resource(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/resources.rb', line 15

def update_resource(id, data)
  post_request("resources/#{id}", data)
end

#update_temporal_rule(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/temporal_rules.rb', line 15

def update_temporal_rule(id, data)
  post_request("temporal_rules/#{id}", data)
end

#update_user(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/users.rb', line 15

def update_user(id, data)
  post_request("users/#{id}", data)
end

#update_vmbox(id, data) ⇒ Object



15
16
17
# File 'lib/kazoo-client/vmboxes.rb', line 15

def update_vmbox(id, data)
  post_request("vmboxes/#{id}", data)
end