Class: RingCentral::Avatars::Creator

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

Constant Summary collapse

DEFAULT_SIZE =
600
DEFAULT_FORMAT =
'png'
PNG_DEFAULT_METADATA =
{
  'Description' => 'RingCentral Default Avatar'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, opts = {}) ⇒ Creator

Requires RingCentralSdk instance ‘:avatar_opts` is optional to pass-through options for Avatarly



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ringcentral-avatars/creator.rb', line 28

def initialize(client, opts = {})
  @client = client
  if !opts.key?(:initials_opts) && opts.key?(:avatar_opts)
    opts[:initials_opts] = opts[:avatar_opts]
  end
  if opts.key(:png_metadata)
    opts[:png_metadata] = PNG_DEFAULT_METADATA.merge(opts[:png_metadata])
  else
    opts[:png_metadata] = PNG_DEFAULT_METADATA
  end
  @avatars = RingCentral::Avatars::MultiAvatar.new opts
  load_extensions
end

Instance Attribute Details

#avatar_optsObject

Returns the value of attribute avatar_opts.



19
20
21
# File 'lib/ringcentral-avatars/creator.rb', line 19

def avatar_opts
  @avatar_opts
end

#avatarsObject

Returns the value of attribute avatars.



20
21
22
# File 'lib/ringcentral-avatars/creator.rb', line 20

def avatars
  @avatars
end

#clientObject

Returns the value of attribute client.



21
22
23
# File 'lib/ringcentral-avatars/creator.rb', line 21

def client
  @client
end

#extensionsObject

Returns the value of attribute extensions.



22
23
24
# File 'lib/ringcentral-avatars/creator.rb', line 22

def extensions
  @extensions
end

#png_metadataObject

Returns the value of attribute png_metadata.



23
24
25
# File 'lib/ringcentral-avatars/creator.rb', line 23

def 
  @png_metadata
end

Instance Method Details

#avatar_url(ext, opts = {}) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/ringcentral-avatars/creator.rb', line 113

def avatar_url(ext, opts = {})
  opts[:include_token] = false unless opts.key? :include_token
  token = @client.token.to_hash[:access_token]
  url = ext['profileImage']['uri']
  url += "?access_token=#{token}" if opts[:include_token]
  url
end

#avatar_urls(opts = {}) ⇒ Object

Returns a list of avatar URLs which can be useful for testing purposes Adding the current access token is optional



97
98
99
100
101
102
103
104
105
# File 'lib/ringcentral-avatars/creator.rb', line 97

def avatar_urls(opts = {})
  opts[:include_token] = false unless opts.key? :include_token
  urls = []
  @extensions.extensions_hash.keys.sort.each do |ext_id|
    ext = @extensions.extensions_hash[ext_id]
    urls.push avatar_url(ext, opts)
  end
  urls
end

#create_all(opts = {}) ⇒ Object

Convenience method for creating avatars for all extensions Defaults to overwriting existing avatar



53
54
55
56
57
58
59
# File 'lib/ringcentral-avatars/creator.rb', line 53

def create_all(opts = {})
  opts[:overwrite] = true unless opts.key?(:overwrite)
  @extensions.extensions_hash.each do |ext_id, ext|
    create_avatar ext, opts
  end
  load_extensions
end

#create_avatar(ext, opts = {}) ⇒ Object

Create the avatar for the extension. Defaults to not overwriting existing avatar



74
75
76
77
78
79
80
# File 'lib/ringcentral-avatars/creator.rb', line 74

def create_avatar(ext, opts = {})
  opts[:overwrite] = false unless opts.key?(:overwrite)
  return if has_avatar(ext) && !opts[:overwrite]
  url = "account/~/extension/#{ext['id']}/profile-image"
  image = @avatars.avatar_faraday_uploadio ext['name']
  @client.http.put url, image: image
end

#create_defaults(opts = {}) ⇒ Object

Convenience method for creating default avatars for all extensions Defaults to not overwriting existing avatars



45
46
47
48
# File 'lib/ringcentral-avatars/creator.rb', line 45

def create_defaults(opts = {})
  opts[:overwrite] = false
  create_all opts
end

#create_mine(opts = {}) ⇒ Object

Convenience method for creating avatar for authorized extension Defaults to not overwriting existing avatar



64
65
66
67
68
69
# File 'lib/ringcentral-avatars/creator.rb', line 64

def create_mine(opts = {})
  res_ext = @client.http.get 'account/~/extension/~'
  res_av = create_avatar res_ext.body, opts
  load_extensions
  res_av
end

#has_avatar(ext) ⇒ Object

Determines if extension has an existing avatar Checks by looking for the presence of the ‘etag` property



85
86
87
# File 'lib/ringcentral-avatars/creator.rb', line 85

def has_avatar(ext)
  ext['profileImage'].key?('etag') ? true : false
end

#load_extensionsObject



89
90
91
92
# File 'lib/ringcentral-avatars/creator.rb', line 89

def load_extensions
  @extensions = RingCentralSdk::REST::Cache::Extensions.new client
  @extensions.retrieve_all
end

#my_avatar_url(opts = {}) ⇒ Object



107
108
109
110
111
# File 'lib/ringcentral-avatars/creator.rb', line 107

def my_avatar_url(opts = {})
  opts[:include_token] = false unless opts.key? :include_token
  res = @client.http.get 'account/~/extension/~'
  avatar_url(res.body, opts)
end