Module: FmRest::V1::ContainerFields
- Included in:
- FmRest::V1
- Defined in:
- lib/fmrest/v1/container_fields.rb
Constant Summary collapse
- DEFAULT_UPLOAD_CONTENT_TYPE =
"application/octet-stream".freeze
Instance Method Summary collapse
-
#fetch_container_data(container_field_url, base_connection = nil) ⇒ IO
Given a container field URL it tries to fetch it and returns an IO object with its body content (see Ruby's OpenURI for how the IO object is extended with useful HTTP response information).
-
#upload_container_data(connection, container_path, filename_or_io, options = {}) ⇒ Object
Handles the core logic of uploading a file into a container field.
Instance Method Details
#fetch_container_data(container_field_url, base_connection = nil) ⇒ IO
Given a container field URL it tries to fetch it and returns an IO object with its body content (see Ruby's OpenURI for how the IO object is extended with useful HTTP response information).
This method uses OpenURI instead of Faraday for fetching the actual container file.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fmrest/v1/container_fields.rb', line 25 def fetch_container_data(container_field_url, base_connection = nil) require "open-uri" if container_field_url == "" raise FmRest::ContainerFieldError, "Container field URL is empty string" end begin url = URI(container_field_url) rescue ::URI::InvalidURIError, ArgumentError raise FmRest::ContainerFieldError, "Invalid container field URL `#{container_field_url}'" end # Make sure we don't try to open anything on the file:/ URI scheme unless url.scheme.match(/\Ahttps?\Z/) raise FmRest::ContainerFieldError, "Container field URL is not HTTP (#{container_field_url})" end = base_connection && base_connection.ssl && base_connection.ssl.to_hash = base_connection && base_connection.proxy && base_connection.proxy.to_hash conn = Faraday.new(nil, ssl: || FmRest.default_connection_settings[:ssl], proxy: || FmRest.default_connection_settings[:proxy] ) = (conn).merge(redirect: false) begin url.open() rescue OpenURI::HTTPRedirect => e unless = e.io..dig("set-cookie", 0) raise FmRest::ContainerFieldError, "Container field's initial request didn't return a session cookie, the URL may be stale (try downloading it again immediately after retrieving the record)" end url = if e.io.["location"].match(/\Ahttps?:/) URI(e.io.["location"]) else URI.join("#{url.scheme}://#{url.host}:#{url.port}", e.io.["location"]) end # Now request the URL again with the proper session cookie using # OpenURI, which wraps the response in an IO object which also responds # to #content_type url.open(.merge("Cookie" => )) end end |
#upload_container_data(connection, container_path, filename_or_io, options = {}) ⇒ Object
Handles the core logic of uploading a file into a container field
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/fmrest/v1/container_fields.rb', line 86 def upload_container_data(connection, container_path, filename_or_io, = {}) content_type = [:content_type] || DEFAULT_UPLOAD_CONTENT_TYPE connection.post do |request| request.url container_path request.headers['Content-Type'] = "multipart/form-data" filename = [:filename] || filename_or_io.try(:original_filename) request.body = { upload: Faraday::Multipart::FilePart.new(filename_or_io, content_type, filename) } end end |