1
|
|
|
require 'avatarly' |
2
|
|
|
require 'faraday' |
3
|
|
|
require 'mime/types' |
4
|
|
|
require 'ringcentral_sdk' |
5
|
|
|
require 'ruby_identicon' |
6
|
|
|
require 'tempfile' |
7
|
|
|
|
8
|
|
|
module RingCentral |
9
|
|
|
module Avatars |
10
|
|
|
class Creator |
|
|
|
|
11
|
|
|
DEFAULT_SIZE = 600 |
12
|
|
|
DEFAULT_FORMAT = 'png' |
13
|
|
|
|
14
|
|
|
attr_accessor :avatar_opts |
15
|
|
|
attr_accessor :avatars |
16
|
|
|
attr_accessor :client |
17
|
|
|
attr_accessor :extensions |
18
|
|
|
|
19
|
|
|
## |
20
|
|
|
# Requires RingCentralSdk instance |
21
|
|
|
# `:avatar_opts` is optional to pass-through options for Avatarly |
22
|
|
|
def initialize(client, opts = {}) |
23
|
|
|
@client = client |
24
|
|
|
if !opts.key?(:initials_opts) && opts.key?(:avatar_opts) |
25
|
|
|
opts[:initials_opts] = opts[:avatar_opts] |
26
|
|
|
end |
27
|
|
|
@avatars = RingCentral::Avatars::MultiAvatar.new opts |
28
|
|
|
load_extensions |
29
|
|
|
end |
30
|
|
|
|
31
|
|
|
## |
32
|
|
|
# Convenience method for creating default avatars for all extensions |
33
|
|
|
# Defaults to not overwriting existing avatars |
34
|
|
|
def create_defaults(opts = {}) |
35
|
|
|
opts[:overwrite] = false |
36
|
|
|
create_all opts |
37
|
|
|
end |
38
|
|
|
|
39
|
|
|
## |
40
|
|
|
# Convenience method for creating avatars for all extensions |
41
|
|
|
# Defaults to overwriting existing avatar |
42
|
|
|
def create_all(opts = {}) |
43
|
|
|
opts[:overwrite] = true unless opts.key?(:overwrite) |
44
|
|
|
@extensions.extensions_hash.each do |ext_id, ext| |
45
|
|
|
create_avatar ext, opts |
46
|
|
|
end |
47
|
|
|
load_extensions |
48
|
|
|
end |
49
|
|
|
|
50
|
|
|
## |
51
|
|
|
# Convenience method for creating avatar for authorized extension |
52
|
|
|
# Defaults to not overwriting existing avatar |
53
|
|
|
def create_mine(opts = {}) |
54
|
|
|
res_ext = @client.http.get 'account/~/extension/~' |
55
|
|
|
res_av = create_avatar res_ext.body, opts |
56
|
|
|
load_extensions |
57
|
|
|
res_av |
58
|
|
|
end |
59
|
|
|
|
60
|
|
|
## |
61
|
|
|
# Create the avatar for the extension. |
62
|
|
|
# Defaults to not overwriting existing avatar |
63
|
|
|
def create_avatar(ext, opts = {}) |
64
|
|
|
opts[:overwrite] = false unless opts.key?(:overwrite) |
65
|
|
|
return if has_avatar(ext) && !opts[:overwrite] |
66
|
|
|
url = "account/~/extension/#{ext['id']}/profile-image" |
67
|
|
|
image = @avatars.avatar_faraday_uploadio ext['name'] |
68
|
|
|
@client.http.put url, image: image |
69
|
|
|
end |
70
|
|
|
|
71
|
|
|
## |
72
|
|
|
# Determines if extension has an existing avatar |
73
|
|
|
# Checks by looking for the presence of the `etag` property |
74
|
|
|
def has_avatar(ext) |
|
|
|
|
75
|
|
|
ext['profileImage'].key?('etag') ? true : false |
76
|
|
|
end |
77
|
|
|
|
78
|
|
|
def load_extensions |
79
|
|
|
@extensions = RingCentralSdk::REST::Cache::Extensions.new client |
80
|
|
|
@extensions.retrieve_all |
81
|
|
|
end |
82
|
|
|
|
83
|
|
|
## |
84
|
|
|
# Returns a list of avatar URLs which can be useful for testing purposes |
85
|
|
|
# Adding the current access token is optional |
86
|
|
|
def avatar_urls(opts = {}) |
87
|
|
|
opts[:include_token] = false unless opts.key? :include_token |
88
|
|
|
urls = [] |
89
|
|
|
@extensions.extensions_hash.keys.sort.each do |ext_id| |
90
|
|
|
ext = @extensions.extensions_hash[ext_id] |
91
|
|
|
urls.push avatar_url(ext, opts) |
92
|
|
|
end |
93
|
|
|
urls |
94
|
|
|
end |
95
|
|
|
|
96
|
|
|
def my_avatar_url(opts = {}) |
97
|
|
|
opts[:include_token] = false unless opts.key? :include_token |
98
|
|
|
res = @client.http.get 'account/~/extension/~' |
99
|
|
|
avatar_url(res.body, opts) |
100
|
|
|
end |
101
|
|
|
|
102
|
|
|
def avatar_url(ext, opts = {}) |
103
|
|
|
opts[:include_token] = false unless opts.key? :include_token |
104
|
|
|
token = @client.token.to_hash[:access_token] |
105
|
|
|
url = ext['profileImage']['uri'] |
106
|
|
|
url += "?access_token=#{token}" if opts[:include_token] |
107
|
|
|
url |
108
|
|
|
end |
109
|
|
|
end |
110
|
|
|
end |
111
|
|
|
end |
112
|
|
|
|
113
|
|
|
module RingCentral |
114
|
|
|
module Avatars |
115
|
|
|
class MultiAvatar |
|
|
|
|
116
|
|
|
DEFAULT_STYLE = 'initials' |
117
|
|
|
AVATARLY_DEFAULTS = { |
118
|
|
|
size: 600, |
119
|
|
|
format: 'png' |
120
|
|
|
} |
121
|
|
|
IDENTICON_DEFAULTS = { |
122
|
|
|
grid_size: 5, |
123
|
|
|
square_size: 70, |
124
|
|
|
background_color: 0xffffffff |
125
|
|
|
} |
126
|
|
|
IDENTICON_DEFAULT_FORMAT = 'png' |
127
|
|
|
|
128
|
|
|
def initialize(opts = {}) |
129
|
|
|
@avatarly_opts = inflate_avatarly_opts opts[:initials_opts] |
130
|
|
|
@identicon_opts = inflate_identicon_opts opts[:identicon_opts] |
131
|
|
|
@style = opts.key?(:style) ? opts[:style] : DEFAULT_STYLE |
132
|
|
|
end |
133
|
|
|
|
134
|
|
View Code Duplication |
def inflate_avatarly_opts(avatarly_opts = {}) |
|
|
|
|
135
|
|
|
avatarly_opts = {} unless avatarly_opts.is_a? Hash |
136
|
|
|
AVATARLY_DEFAULTS.merge avatarly_opts |
137
|
|
|
end |
138
|
|
|
|
139
|
|
View Code Duplication |
def inflate_identicon_opts(identicon_opts = {}) |
|
|
|
|
140
|
|
|
identicon_opts = {} unless identicon_opts.is_a? Hash |
141
|
|
|
IDENTICON_DEFAULTS.merge identicon_opts |
142
|
|
|
end |
143
|
|
|
|
144
|
|
|
def avatar_blob(text, style = nil) |
145
|
|
|
style = @style if style.nil? |
|
|
|
|
146
|
|
|
blob = @style == 'initials' \ |
|
|
|
|
147
|
|
|
? Avatarly.generate_avatar(text, @avatarly_opts) \ |
148
|
|
|
: RubyIdenticon.create(text, @identicon_opts) |
149
|
|
|
end |
150
|
|
|
|
151
|
|
|
def avatar_temp_file(text, style = nil) |
152
|
|
|
blob = avatar_blob text, style |
153
|
|
|
file = Tempfile.new ['avatar', avatar_extension] |
154
|
|
|
file.binmode |
155
|
|
|
file.write blob |
156
|
|
|
file.flush |
157
|
|
|
file |
158
|
|
|
end |
159
|
|
|
|
160
|
|
|
def avatar_faraday_uploadio(text, style = nil) |
161
|
|
|
file = avatar_temp_file text, style |
162
|
|
|
image = Faraday::UploadIO.new file.path, avatar_mime_type |
|
|
|
|
163
|
|
|
end |
164
|
|
|
|
165
|
|
|
def avatar_format |
166
|
|
|
@style == 'initials' ? @avatarly_opts[:format] : IDENTICON_DEFAULT_FORMAT |
167
|
|
|
end |
168
|
|
|
|
169
|
|
|
def avatar_mime_type |
170
|
|
|
types = MIME::Types.type_for avatar_format |
171
|
|
|
if types.length == 0 |
|
|
|
|
172
|
|
|
raise "Unknown avatar format: #{avatar_format}" |
173
|
|
|
end |
174
|
|
|
types[0].to_s |
175
|
|
|
end |
176
|
|
|
|
177
|
|
|
def avatar_extension |
178
|
|
|
".#{avatar_format}" |
179
|
|
|
end |
180
|
|
|
end |
181
|
|
|
end |
182
|
|
|
end |