Class: Zm::Client::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/client/upload/upload.rb

Overview

class for upload account file

Constant Summary collapse

FMT_TYPES_H =
{
  'ics' => ['appointment'],
  'vcard' => ['contact']
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(parent, rac = nil) ⇒ Upload

Returns a new instance of Upload.



12
13
14
15
16
# File 'lib/zm/client/upload/upload.rb', line 12

def initialize(parent, rac = nil)
  @parent = parent
  @rac = rac || @parent.rac
  @rac.cookies("ZM_AUTH_TOKEN=#{@parent.token}")
end

Instance Method Details

#download_file(folder_path, fmt, types, ids, dest_file_path) ⇒ Object



25
26
27
# File 'lib/zm/client/upload/upload.rb', line 25

def download_file(folder_path, fmt, types, ids, dest_file_path)
  @rac.download(download_file_url(folder_path, fmt, types, ids), dest_file_path)
end

#download_file_url(folder_path, fmt, types, ids = []) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zm/client/upload/upload.rb', line 52

def download_file_url(folder_path, fmt, types, ids = [])
  raise ZmError, 'home_url is not defined' if @parent.home_url.nil?

  url_folder_path = File.join(@parent.home_url, folder_path.to_s)

  h = {
    fmt: fmt,
    types: query_value_types(types, fmt),
    emptyname: 'Empty',
    charset: 'UTF-8',
    auth: 'co',
    disp: 'a'
  }

  h.merge!(query_ids(ids))

  h.reject! { |_, v| is_blank?(v) }

  url_folder_path << '?' << Utils.format_url_params(h)

  url_folder_path
end

#download_file_with_url(url, dest_file_path) ⇒ Object

Raises:



18
19
20
21
22
23
# File 'lib/zm/client/upload/upload.rb', line 18

def download_file_with_url(url, dest_file_path)
  raise ZmError, 'home_url is not defined' if @parent.home_url.nil?

  url = File.join(@parent.home_url, url) unless url.start_with?('http')
  @rac.download(url, dest_file_path)
end

#download_folder(id, fmt, dest_file_path) ⇒ Object



29
30
31
# File 'lib/zm/client/upload/upload.rb', line 29

def download_folder(id, fmt, dest_file_path)
  @rac.download(download_folder_url(id, fmt), dest_file_path)
end

#download_folder_url(id, fmt) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zm/client/upload/upload.rb', line 33

def download_folder_url(id, fmt)
  raise ZmError, 'home_url is not defined' if @parent.home_url.nil?

  url_folder_path = @parent.home_url.dup

  h = {
    fmt: fmt,
    id: id,
    emptyname: 'Empty',
    charset: 'UTF-8',
    auth: 'co',
    disp: 'a'
  }

  url_folder_path << '?' << Utils.format_url_params(h)

  url_folder_path
end

#is_blank?(v) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
# File 'lib/zm/client/upload/upload.rb', line 131

def is_blank?(v)
  return false if v.is_a?(Numeric)

  v.nil? || v.empty?
end

#query_ids(ids) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/zm/client/upload/upload.rb', line 116

def query_ids(ids)
  return {} if ids.nil?
  return { id: ids } unless ids.is_a?(Array)
  return { id: ids.first } if ids.length == 1

  { list: ids.join(',') }
end

#query_value_types(types, fmt) ⇒ Object



124
125
126
127
128
129
# File 'lib/zm/client/upload/upload.rb', line 124

def query_value_types(types, fmt)
  types = FMT_TYPES_H[fmt] if types.nil?

  types = [types] unless types.is_a?(Array)
  types.join(',')
end

#send_attachment(src_file_path) ⇒ Object



99
100
101
102
# File 'lib/zm/client/upload/upload.rb', line 99

def send_attachment(src_file_path)
  str = upload_attachment(src_file_path)
  AttachmentResponse.new(str)
end

#send_file(folder_path, fmt, types, resolve, src_file_path) ⇒ Object



75
76
77
# File 'lib/zm/client/upload/upload.rb', line 75

def send_file(folder_path, fmt, types, resolve, src_file_path)
  @rac.upload(send_file_url(folder_path, fmt, types, resolve), src_file_path)
end

#send_file_url(folder_path, fmt, types, resolve) ⇒ Object

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/zm/client/upload/upload.rb', line 79

def send_file_url(folder_path, fmt, types, resolve)
  raise ZmError, 'home_url is not defined' if @parent.home_url.nil?

  # resolve=[modfy|replace|reset|skip]
  url_folder_path = File.join(@parent.home_url, Utils.format_url_path(folder_path.to_s))

  h = {
    fmt: fmt,
    types: query_value_types(types, fmt),
    resolve: resolve,
    auth: 'co'
  }

  h.reject! { |_, v| is_blank?(v) }

  url_folder_path << '?' << Utils.format_url_params(h)

  url_folder_path
end

#upload_attachment(src_file_path) ⇒ Object



104
105
106
# File 'lib/zm/client/upload/upload.rb', line 104

def upload_attachment(src_file_path)
  @rac.upload(upload_attachment_url, src_file_path)
end

#upload_attachment_urlObject



108
109
110
111
112
113
114
# File 'lib/zm/client/upload/upload.rb', line 108

def upload_attachment_url
  h = {
    fmt: 'extended,raw'
  }

  File.join(@parent.public_url, 'service/upload') << '?' << Utils.format_url_params(h)
end