Class: RingCentral::Avatars::MultiAvatar

Inherits:
Object
  • Object
show all
Defined in:
lib/ringcentral-avatars/creator.rb

Constant Summary collapse

DEFAULT_STYLE =
'initials'.freeze
AVATARLY_DEFAULTS =
{
  size: 600,
  format: 'png'
}.freeze
IDENTICON_DEFAULTS =
{
  grid_size: 5,
  square_size: 70,
  background_color: 0xffffffff
}.freeze
IDENTICON_DEFAULT_FORMAT =
'png'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MultiAvatar

Returns a new instance of MultiAvatar.



162
163
164
165
166
167
168
# File 'lib/ringcentral-avatars/creator.rb', line 162

def initialize(opts = {})
  @avatarly_opts =  inflate_avatarly_opts opts[:initials_opts]
  @identicon_opts = inflate_identicon_opts opts[:identicon_opts]
  @style = opts.key?(:style) ? opts[:style] : DEFAULT_STYLE
  @png_metadata = opts.key?(:png_metadata) ? opts[:png_metadata] : {}
  @logger = opts.key?(:logger) ? opts[:logger] : Logger.new(STDOUT)
end

Instance Method Details

#avatar_blob(text, style = nil) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/ringcentral-avatars/creator.rb', line 180

def avatar_blob(text, style = nil)
  style = @style if style.nil?
  blob = style == 'initials' \
    ? Avatarly.generate_avatar(text, @avatarly_opts) \
    : RubyIdenticon.create(text, @identicon_opts)
  inflate_avatar_blob_png blob
end

#avatar_extensionObject



222
223
224
# File 'lib/ringcentral-avatars/creator.rb', line 222

def avatar_extension
  ".#{avatar_format}"
end

#avatar_faraday_uploadio(text, style = nil) ⇒ Object



204
205
206
207
208
# File 'lib/ringcentral-avatars/creator.rb', line 204

def avatar_faraday_uploadio(text, style = nil)
  file = avatar_temp_file text, style
  @logger.debug "Building Avatar Temp File: #{file.path}"
  Faraday::UploadIO.new file.path, avatar_mime_type
end

#avatar_formatObject



210
211
212
# File 'lib/ringcentral-avatars/creator.rb', line 210

def avatar_format
  @style == 'initials' ? @avatarly_opts[:format] : IDENTICON_DEFAULT_FORMAT
end

#avatar_mime_typeObject



214
215
216
217
218
219
220
# File 'lib/ringcentral-avatars/creator.rb', line 214

def avatar_mime_type
  types = MIME::Types.type_for avatar_format
  if types.empty?
    raise "Unknown avatar format: #{avatar_format}"
  end
  types[0].to_s
end

#avatar_temp_file(text, style = nil) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/ringcentral-avatars/creator.rb', line 195

def avatar_temp_file(text, style = nil)
  blob = avatar_blob text, style
  file = Tempfile.new ['avatar', avatar_extension]
  file.binmode
  file.write blob
  file.flush
  file
end

#inflate_avatar_blob_png(blob) ⇒ Object



188
189
190
191
192
193
# File 'lib/ringcentral-avatars/creator.rb', line 188

def inflate_avatar_blob_png(blob)
  return blob unless avatar_format == 'png' && @png_metadata
  img = ChunkyPNG::Image.from_blob blob
  img. = @png_metadata
  img.to_blob
end

#inflate_avatarly_opts(avatarly_opts = {}) ⇒ Object



170
171
172
173
# File 'lib/ringcentral-avatars/creator.rb', line 170

def inflate_avatarly_opts(avatarly_opts = {})
  avatarly_opts = {} unless avatarly_opts.is_a? Hash
  AVATARLY_DEFAULTS.merge avatarly_opts
end

#inflate_identicon_opts(identicon_opts = {}) ⇒ Object



175
176
177
178
# File 'lib/ringcentral-avatars/creator.rb', line 175

def inflate_identicon_opts(identicon_opts = {})
  identicon_opts = {} unless identicon_opts.is_a? Hash
  IDENTICON_DEFAULTS.merge identicon_opts
end