Class: Zm::Client::RestConnector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false, timeout: 300) {|_self| ... } ⇒ RestConnector

Returns a new instance of RestConnector.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zm/client/connector/rest_connector.rb', line 12

def initialize(verbose: false, timeout: 300)
  @verbose = verbose
  @timeout = timeout

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

  @cookies = nil

  yield(self) if block_given?
end

Instance Attribute Details

#cookies=(value) ⇒ Object (writeonly)

Sets the attribute cookies

Parameters:

  • value

    the value to set the attribute cookies to.



10
11
12
# File 'lib/zm/client/connector/rest_connector.rb', line 10

def cookies=(value)
  @cookies = value
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



9
10
11
# File 'lib/zm/client/connector/rest_connector.rb', line 9

def timeout
  @timeout
end

#verboseObject (readonly)

Returns the value of attribute verbose.



9
10
11
# File 'lib/zm/client/connector/rest_connector.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#download(download_url, dest_file_path) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zm/client/connector/rest_connector.rb', line 27

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

  conn = conn_get(url)

  response = nil

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

  return unless response.status >= 400

  FileUtils.rm_f(dest_file_path)
  raise RestError, "Download failure: #{response.body} (status=#{response.status})"
end

#read(download_url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zm/client/connector/rest_connector.rb', line 48

def read(download_url)
  url, path = split_url(download_url)

  conn = conn_get(url)

  data = +''

  response = conn.get(path) do |request|
    request.options.on_data = Proc.new do |chunk, _, _|
      data.concat(chunk)
    end
  end

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

  data
end

#upload(upload_url, src_file_path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zm/client/connector/rest_connector.rb', line 68

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)

  upload_return(response, failure_message: "Upload failure ! #{src_file_path}")
end