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



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:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/zm/client/upload/upload.rb', line 56

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



33
34
35
# File 'lib/zm/client/upload/upload.rb', line 33

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:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zm/client/upload/upload.rb', line 37

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



135
136
137
138
139
# File 'lib/zm/client/upload/upload.rb', line 135

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

  v.nil? || v.empty?
end

#query_ids(ids) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/zm/client/upload/upload.rb', line 120

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



128
129
130
131
132
133
# File 'lib/zm/client/upload/upload.rb', line 128

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

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

#read_file(folder_path, fmt, types, ids) ⇒ Object



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

def read_file(folder_path, fmt, types, ids)
  @rac.read(download_file_url(folder_path, fmt, types, ids))
end

#send_attachment(src_file_path) ⇒ Object



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

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



79
80
81
# File 'lib/zm/client/upload/upload.rb', line 79

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:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/zm/client/upload/upload.rb', line 83

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



108
109
110
# File 'lib/zm/client/upload/upload.rb', line 108

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

#upload_attachment_urlObject



112
113
114
115
116
117
118
# File 'lib/zm/client/upload/upload.rb', line 112

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

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