|
1
|
|
|
require 'avatarly' |
|
2
|
|
|
require 'faraday' |
|
3
|
|
|
require 'ringcentral_sdk' |
|
4
|
|
|
require 'tempfile' |
|
5
|
|
|
|
|
6
|
|
|
module RingCentral |
|
7
|
|
|
module Avatars |
|
8
|
|
|
class Creator |
|
|
|
|
|
|
9
|
|
|
DEFAULT_SIZE = 600 |
|
10
|
|
|
|
|
11
|
|
|
attr_accessor :client |
|
12
|
|
|
attr_accessor :extensions |
|
13
|
|
|
|
|
14
|
|
|
def initialize(client) |
|
15
|
|
|
@client = client |
|
16
|
|
|
load_extensions |
|
17
|
|
|
end |
|
18
|
|
|
|
|
19
|
|
|
## |
|
20
|
|
|
# Convenience method for creating default avatars for all extensions |
|
21
|
|
|
# Defaults to not overwriting existing avatars |
|
22
|
|
|
def create_defaults(opts = {}) |
|
23
|
|
|
opts[:overwrite] = false |
|
24
|
|
|
create_all opts |
|
25
|
|
|
end |
|
26
|
|
|
|
|
27
|
|
|
## |
|
28
|
|
|
# Convenience method for creating avatars for all extensions |
|
29
|
|
|
# Defaults to overwriting existing avatar |
|
30
|
|
|
def create_all(opts = {}) |
|
31
|
|
|
opts[:overwrite] = true unless opts.key?(:overwrite) |
|
32
|
|
|
@extensions.extensions_hash.each do |ext_id, ext| |
|
33
|
|
|
create_avatar ext, opts |
|
34
|
|
|
end |
|
35
|
|
|
load_extensions |
|
36
|
|
|
end |
|
37
|
|
|
|
|
38
|
|
|
## |
|
39
|
|
|
# Convenience method for creating avatar for authorized extension |
|
40
|
|
|
# Defaults to not overwriting existing avatar |
|
41
|
|
|
def create_mine(opts = {}) |
|
42
|
|
|
res = @client.http.get 'account/~/extension/~' |
|
43
|
|
|
create_avatar res.body, opts |
|
44
|
|
|
load_extensions |
|
45
|
|
|
end |
|
46
|
|
|
|
|
47
|
|
|
## |
|
48
|
|
|
# Create the avatar for the extension. |
|
49
|
|
|
# Defaults to not overwriting existing avatar |
|
50
|
|
|
def create_avatar(ext, opts = {}) |
|
51
|
|
|
opts[:overwrite] = false unless opts.key?(:overwrite) |
|
52
|
|
|
return if has_avatar(ext) && !opts[:overwrite] |
|
53
|
|
|
avatar_temp = get_avatar_tmp_file ext |
|
54
|
|
|
url = "account/~/extension/#{ext['id']}/profile-image" |
|
55
|
|
|
image = Faraday::UploadIO.new(avatar_temp.path, 'image/png') |
|
56
|
|
|
@client.http.put url, image: image |
|
57
|
|
|
end |
|
58
|
|
|
|
|
59
|
|
|
## |
|
60
|
|
|
# Determines if extension has an existing avatar |
|
61
|
|
|
# Checks by looking ofr the presence of the `etag` property |
|
62
|
|
|
def has_avatar(ext) |
|
|
|
|
|
|
63
|
|
|
return ext['profileImage'].key?('etag') ? true : false |
|
64
|
|
|
end |
|
65
|
|
|
|
|
66
|
|
|
def get_avatar_tmp_file(ext) |
|
67
|
|
|
avatar_blob = Avatarly.generate_avatar(ext['name'], size: DEFAULT_SIZE) |
|
68
|
|
|
avatar_temp = Tempfile.new(['avatar', '.png']) |
|
69
|
|
|
avatar_temp.binmode |
|
70
|
|
|
avatar_temp.write(avatar_blob) |
|
71
|
|
|
avatar_temp.flush |
|
72
|
|
|
avatar_temp |
|
73
|
|
|
end |
|
74
|
|
|
|
|
75
|
|
|
def load_extensions |
|
76
|
|
|
@extensions = RingCentralSdk::REST::Cache::Extensions.new client |
|
77
|
|
|
@extensions.retrieve_all |
|
78
|
|
|
end |
|
79
|
|
|
|
|
80
|
|
|
## |
|
81
|
|
|
# Returns a list of avatar URLs which can be useful for testing purposes |
|
82
|
|
|
# Adding the current access token is optional |
|
83
|
|
|
def avatar_urls(include_token = false) |
|
84
|
|
|
urls = [] |
|
85
|
|
|
@extensions.extensions_hash.keys.sort.each do |ext_id| |
|
86
|
|
|
ext = @extensions.extensions_hash[ext_id] |
|
87
|
|
|
urls.push avatar_url(ext, include_token) |
|
88
|
|
|
end |
|
89
|
|
|
return urls |
|
90
|
|
|
end |
|
91
|
|
|
|
|
92
|
|
|
def my_avatar_url(include_token = false) |
|
93
|
|
|
res = @client.http.get 'account/~/extension/~' |
|
94
|
|
|
avatar_url(res.body, include_token) |
|
95
|
|
|
end |
|
96
|
|
|
|
|
97
|
|
|
def avatar_url(ext, include_token = false) |
|
98
|
|
|
token = @client.token.to_hash[:access_token] |
|
99
|
|
|
url = ext['profileImage']['uri'] |
|
100
|
|
|
url += "?access_token=#{token}" if include_token |
|
101
|
|
|
return url |
|
102
|
|
|
end |
|
103
|
|
|
end |
|
104
|
|
|
end |
|
105
|
|
|
end |