Class: RingCentral::Avatars::MultiAvatar

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MultiAvatar

Returns a new instance of MultiAvatar.



139
140
141
142
143
144
# File 'lib/ringcentral-avatars/creator.rb', line 139

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] : {}
end

Instance Method Details

#avatar_blob(text, style = nil) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/ringcentral-avatars/creator.rb', line 156

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



197
198
199
# File 'lib/ringcentral-avatars/creator.rb', line 197

def avatar_extension
  ".#{avatar_format}"
end

#avatar_faraday_uploadio(text, style = nil) ⇒ Object



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

def avatar_faraday_uploadio(text, style = nil)
  file = avatar_temp_file text, style
  image = Faraday::UploadIO.new file.path, avatar_mime_type
end

#avatar_formatObject



185
186
187
# File 'lib/ringcentral-avatars/creator.rb', line 185

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

#avatar_mime_typeObject



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

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

#avatar_temp_file(text, style = nil) ⇒ Object



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

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



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

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



146
147
148
149
# File 'lib/ringcentral-avatars/creator.rb', line 146

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



151
152
153
154
# File 'lib/ringcentral-avatars/creator.rb', line 151

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