Class: Karaden::Utility
- Inherits:
-
Object
- Object
- Karaden::Utility
- Defined in:
- lib/karaden/utility.rb
Constant Summary collapse
- OBJECT_TYPES =
{ Karaden::Model::Error::OBJECT_NAME => Karaden::Model::Error, Karaden::Model::Collection::OBJECT_NAME => Karaden::Model::Collection, Karaden::Model::Message::OBJECT_NAME => Karaden::Model::Message, Karaden::Model::BulkFile::OBJECT_NAME => Karaden::Model::BulkFile, Karaden::Model::BulkMessage::OBJECT_NAME => Karaden::Model::BulkMessage }.freeze
- DEFAULT_CONNECTION_TIMEOUT =
10
- DEFAULT_READ_TIMEOUT =
30
Class Method Summary collapse
- .construct_from(clazz, contents, request_options) ⇒ Object
- .convert_to_array(contents, request_options) ⇒ Object
- .convert_to_karaden_object(contents, request_options) ⇒ Object
- .get_timeout(request_options = nil) ⇒ Object
- .put_signed_url(signed_url, filename, content_type = 'application/octet-stream', request_options = nil) ⇒ Object
Class Method Details
.construct_from(clazz, contents, request_options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/karaden/utility.rb', line 18 def self.construct_from(clazz, contents, ) object = clazz.new(contents['id'], ) contents.each_pair do |key, value| v = if value.is_a?(Array) convert_to_array(value, ) elsif value.is_a?(Hash) convert_to_karaden_object(value, ) else value end object.set_property(key, v) end object end |
.convert_to_array(contents, request_options) ⇒ Object
33 34 35 36 37 |
# File 'lib/karaden/utility.rb', line 33 def self.convert_to_array(contents, ) contents.map do |v| v.is_a?(Hash) ? convert_to_karaden_object(v, ) : v end end |
.convert_to_karaden_object(contents, request_options) ⇒ Object
13 14 15 16 |
# File 'lib/karaden/utility.rb', line 13 def self.convert_to_karaden_object(contents, ) clazz = OBJECT_TYPES[contents['object']] || Karaden::Model::KaradenObject construct_from(clazz, contents, ) end |
.get_timeout(request_options = nil) ⇒ Object
61 62 63 |
# File 'lib/karaden/utility.rb', line 61 def self.get_timeout( = nil) [&.connection_timeout || DEFAULT_CONNECTION_TIMEOUT, &.read_timeout || DEFAULT_READ_TIMEOUT] end |
.put_signed_url(signed_url, filename, content_type = 'application/octet-stream', request_options = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/karaden/utility.rb', line 39 def self.put_signed_url(signed_url, filename, content_type = 'application/octet-stream', = nil) uri = URI.parse(signed_url) http = ::Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.open_timeout, http.read_timeout = get_timeout() request = ::Net::HTTP::Put.new(uri.request_uri) response = File.open(filename, 'rb') do |f| request.body_stream = f request['Content-Length'] = f.size request['Content-Type'] = content_type http.request(request) end raise Karaden::Exception::FileUploadFailedException unless response.code == '200' rescue Karaden::Exception::FileUploadFailedException raise rescue StandardError raise Karaden::Exception::FileUploadFailedException end |