Completed
Push — master ( 712d17...e72bcb )
by John
01:59 queued 59s
created

Creator.avatar_urls()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
require 'avatarly'
2
require 'faraday'
3
require 'mime/types'
4
require 'ringcentral_sdk'
5
require 'tempfile'
6
7
module RingCentral
8
  module Avatars
9
    class Creator
0 ignored issues
show
Coding Style introduced by
This class should have a documentation comment as per coding style.
Loading history...
10
      DEFAULT_SIZE = 600
11
12
      attr_accessor :avatar_opts
13
      attr_accessor :client
14
      attr_accessor :extensions
15
16
      ##
17
      # Requires RingCentralSdk instance
18
      # `:avatar_opts` is optional to pass-through options for Avatarly
19
      def initialize(client, opts = {})
20
        @client = client
21
        @avatar_opts = build_avatar_opts opts[:avatar_opts]
22
        load_extensions
23
      end
24
25
      def build_avatar_opts(avatar_opts = {})
26
        avatar_opts = {} unless avatar_opts.is_a? Hash
27
        unless avatar_opts.key? :size
28
          avatar_opts[:size] = DEFAULT_SIZE
29
        end
30
        unless avatar_opts.key? :format
31
          avatar_opts[:format] = 'png'
32
        end
33
        return avatar_opts
34
      end
35
36
      ##
37
      # Convenience method for creating default avatars for all extensions
38
      # Defaults to not overwriting existing avatars
39
      def create_defaults(opts = {})
40
        opts[:overwrite] = false
41
        create_all opts
42
      end
43
44
      ##
45
      # Convenience method for creating avatars for all extensions
46
      # Defaults to overwriting existing avatar
47
      def create_all(opts = {})
48
        opts[:overwrite] = true unless opts.key?(:overwrite)
49
        @extensions.extensions_hash.each do |ext_id, ext|
50
          create_avatar ext, opts
51
        end
52
        load_extensions
53
      end
54
55
      ##
56
      # Convenience method for creating avatar for authorized extension
57
      # Defaults to not overwriting existing avatar
58
      def create_mine(opts = {})
59
        res_ext = @client.http.get 'account/~/extension/~'
60
        res_av = create_avatar res_ext.body, opts
61
        load_extensions
62
        res_av
63
      end
64
65
      ##
66
      # Create the avatar for the extension.
67
      # Defaults to not overwriting existing avatar
68
      def create_avatar(ext, opts = {})
69
        opts[:overwrite] = false unless opts.key?(:overwrite)
70
        return if has_avatar(ext) && !opts[:overwrite]
71
        avatar_temp = get_avatar_tmp_file ext
72
        url = "account/~/extension/#{ext['id']}/profile-image"
73
        image = Faraday::UploadIO.new avatar_temp.path, avatar_mime_type
74
        @client.http.put url, image: image
75
      end
76
0 ignored issues
show
Coding Style introduced by
Your coding style discourages the use of prefixes like is_ or has_ on predicates. Consider renaming has_avatar to avatar.
Loading history...
77
      ##
78
      # Determines if extension has an existing avatar
79
      # Checks by looking ofr the presence of the `etag` property
80
      def has_avatar(ext)
81
        return ext['profileImage'].key?('etag') ? true : false
82
      end
83
84
      def get_avatar_tmp_file(ext)
85
        avatar_blob = Avatarly.generate_avatar(ext['name'], @avatar_opts)
86
        avatar_temp = Tempfile.new(['avatar', avatar_extension])
87
        avatar_temp.binmode
88
        avatar_temp.write(avatar_blob)
89
        avatar_temp.flush
90
        avatar_temp
91
      end
92
93
      def load_extensions
94
        @extensions = RingCentralSdk::REST::Cache::Extensions.new client
95
        @extensions.retrieve_all
96
      end
97
98
      ##
99
      # Returns a list of avatar URLs which can be useful for testing purposes
100
      # Adding the current access token is optional
101
      def avatar_urls(opts = {})
102
        opts[:include_token] = false unless opts.key? :include_token
103
        urls = []
104
        @extensions.extensions_hash.keys.sort.each do |ext_id|
105
          ext = @extensions.extensions_hash[ext_id]
106
          urls.push avatar_url(ext, opts)
107
        end
108
        return urls
109
      end
110
111
      def my_avatar_url(opts = {})
112
        opts[:include_token] = false unless opts.key? :include_token
113
        res = @client.http.get 'account/~/extension/~'
114
        avatar_url(res.body, opts)
115
      end
116
117
      def avatar_url(ext, opts = {})
118
        opts[:include_token] = false unless opts.key? :include_token
119
        token = @client.token.to_hash[:access_token]
120
        url = ext['profileImage']['uri']
121
        url += "?access_token=#{token}" if opts[:include_token]
122
        return url
123
      end
0 ignored issues
show
Coding Style introduced by
Consider using empty? instead of length == 0 to check for zero length.
Loading history...
124
125
      def avatar_mime_type
126
        types = MIME::Types.type_for @avatar_opts[:format]
127
        if types.length == 0
128
          raise "Unknown avatar format: #{@avatar_opts[:format]}"
129
        end
130
        return types[0].to_s
131
      end
132
133
      def avatar_extension
134
        ".#{@avatar_opts[:format]}"
135
      end
136
    end
137
  end
138
end