Class: Zm::Client::RestAccountConnector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRestAccountConnector

Returns a new instance of RestAccountConnector.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/zm/client/connector/rest_account.rb', line 10

def initialize
  @verbose = false
  @timeout = 300

  @ssl_options = {
    verify: false,
    verify_hostname: false,
    verify_mode: OpenSSL::SSL::VERIFY_NONE
  }

  @cookies = nil
end

Instance Attribute Details

#follow_locationObject (readonly)

Returns the value of attribute follow_location.



8
9
10
# File 'lib/zm/client/connector/rest_account.rb', line 8

def follow_location
  @follow_location
end

#verboseObject (readonly)

Returns the value of attribute verbose.



8
9
10
# File 'lib/zm/client/connector/rest_account.rb', line 8

def verbose
  @verbose
end

Instance Method Details

#cookies(cookies) ⇒ Object



27
28
29
# File 'lib/zm/client/connector/rest_account.rb', line 27

def cookies(cookies)
  @cookies = cookies
end

#download(download_url, dest_file_path) ⇒ Object



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

def download(download_url, dest_file_path)
  url, path = split_url(download_url)

  conn = Faraday.new(**http_options(url)) do |faraday|
    faraday.response :logger, nil, { headers: true, bodies: true, errors: true } if @verbose
  end

  response = nil

  File.open(dest_file_path, 'wb') do |f|
    response = conn.get(path) do |request|
      request.options.on_data = Proc.new do |chunk, _, _|
        f.write chunk
      end
    end
  end

  if response.status >= 400
    File.unlink(dest_file_path) if File.exist?(dest_file_path)
    raise RestError, "Download failure: #{response.body} (status=#{response.status})"
  end
end

#upload(upload_url, src_file_path) ⇒ Object



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

def upload(upload_url, src_file_path)
  url, path = split_url(upload_url)

  conn = Faraday.new(**http_options(url)) do |faraday|
    faraday.request :multipart
    faraday.response :logger, nil, { headers: true, bodies: true, errors: true } if @verbose
  end

  payload = { file: Faraday::Multipart::FilePart.new(src_file_path, nil) }
  response = conn.post(path, payload)

  if response.status >= 400
    messages = [
      "Upload failure ! #{src_file_path}",
      extract_title(response.body)
    ].compact

    raise RestError, messages.join("\n")
  end

  response.body
end

#verbose!Object



23
24
25
# File 'lib/zm/client/connector/rest_account.rb', line 23

def verbose!
  @verbose = true
end