Class: Zm::Client::RestConnector
- Inherits:
-
Object
- Object
- Zm::Client::RestConnector
- Defined in:
- lib/zm/client/connector/rest_connector.rb
Instance Attribute Summary collapse
-
#cookies ⇒ Object
writeonly
Sets the attribute cookies.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #download(download_url, dest_file_path) ⇒ Object
-
#initialize(verbose: false, timeout: 300) {|_self| ... } ⇒ RestConnector
constructor
A new instance of RestConnector.
- #read(download_url) ⇒ Object
- #upload(upload_url, src_file_path) ⇒ Object
Constructor Details
#initialize(verbose: false, timeout: 300) {|_self| ... } ⇒ RestConnector
Returns a new instance of RestConnector.
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 = { verify: false, verify_hostname: false, verify_mode: OpenSSL::SSL::VERIFY_NONE } = nil yield(self) if block_given? end |
Instance Attribute Details
#cookies=(value) ⇒ Object (writeonly)
Sets the attribute cookies
10 11 12 |
# File 'lib/zm/client/connector/rest_connector.rb', line 10 def (value) = value end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
9 10 11 |
# File 'lib/zm/client/connector/rest_connector.rb', line 9 def timeout @timeout end |
#verbose ⇒ Object (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
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..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..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(**(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 |