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'.freeze
PNG_DEFAULT_METADATA =
{
  'Description' => 'RingCentral Default Avatar'
}.freeze

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ringcentral-avatars/creator.rb', line 31

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
  opts[:logger] = @client.config.logger
  @retry = opts.key?(:retry) && opts[:retry] ? true : false
  opts[:retry] = @retry
  @avatars = RingCentral::Avatars::MultiAvatar.new opts
  @retry_util = FaradayMiddleware::Request::RetryUtil.new logger: client.config.logger
  load_extensions
end

Instance Attribute Details

#avatar_optsObject

Returns the value of attribute avatar_opts.



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

def avatar_opts
  @avatar_opts
end

#avatarsObject

Returns the value of attribute avatars.



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

def avatars
  @avatars
end

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#extensionsObject

Returns the value of attribute extensions.



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

def extensions
  @extensions
end

#png_metadataObject

Returns the value of attribute png_metadata.



25
26
27
# File 'lib/ringcentral-avatars/creator.rb', line 25

def 
  @png_metadata
end

#retryObject

Returns the value of attribute retry.



26
27
28
# File 'lib/ringcentral-avatars/creator.rb', line 26

def retry
  @retry
end

Instance Method Details

#avatar?(ext) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

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



136
137
138
139
140
141
142
# File 'lib/ringcentral-avatars/creator.rb', line 136

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



120
121
122
123
124
125
126
127
128
# File 'lib/ringcentral-avatars/creator.rb', line 120

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



60
61
62
63
64
65
66
# File 'lib/ringcentral-avatars/creator.rb', line 60

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ringcentral-avatars/creator.rb', line 84

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

#create_defaults(opts = {}) ⇒ Object

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



52
53
54
55
# File 'lib/ringcentral-avatars/creator.rb', line 52

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



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

def create_mine(opts = {})
  try_req = true
  while try_req
    res_ext = @client.http.get 'account/~/extension/~'
    try_req = retry_status res_ext
  end
  res_avt = create_avatar res_ext.body, opts
  res_avt
end

#load_extensionsObject



112
113
114
115
# File 'lib/ringcentral-avatars/creator.rb', line 112

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

#my_avatar_url(opts = {}) ⇒ Object



130
131
132
133
134
# File 'lib/ringcentral-avatars/creator.rb', line 130

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

#retry_status(res) ⇒ Object



100
101
102
103
# File 'lib/ringcentral-avatars/creator.rb', line 100

def retry_status(res)
  return false unless @retry
  @retry_util.retry_status(res.status, res.headers['Retry-After'])
end