Class: Karaden::Service::BulkMessageService
- Inherits:
-
Object
- Object
- Karaden::Service::BulkMessageService
- Defined in:
- lib/karaden/service/bulk_message_service.rb
Constant Summary collapse
- REGEX_PATTERN =
/filename="([^"]+)"/.freeze
Class Method Summary collapse
- .check_bulk_message_status(retry_count, retry_interval, params, request_options) ⇒ Object
- .create(filename, request_options = nil) ⇒ Object
- .download(params, request_options = nil) ⇒ Object
- .get_contents(download_url, directory_path, request_options = nil) ⇒ Object
- .get_download_url(retry_count, retry_interval, params, request_options) ⇒ Object
Class Method Details
.check_bulk_message_status(retry_count, retry_interval, params, request_options) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/karaden/service/bulk_message_service.rb', line 61 def self.(retry_count, retry_interval, params, ) result = false (0..retry_count).each do |count| # sleep retry_interval if count.positive? = Karaden::Model::BulkMessage.show(params, ) raise Exception::BulkMessageCreateFailedException if .status == Karaden::Model::BulkMessage::STATUS_ERROR if .status == Karaden::Model::BulkMessage::STATUS_DONE result = true break end end result end |
.create(filename, request_options = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/karaden/service/bulk_message_service.rb', line 6 def self.create(filename, = nil) raise Karaden::Exception::FileNotFoundException unless File.file?(filename) bulk_file = Karaden::Model::BulkFile.create() Karaden::Utility.put_signed_url(bulk_file.url, filename, 'text/csv', ) params = Karaden::Param::Message::Bulk::BulkMessageCreateParams .new_builder .with_bulk_file_id(bulk_file.id) .build Karaden::Model::BulkMessage.create(params, ) end |
.download(params, request_options = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/karaden/service/bulk_message_service.rb', line 20 def self.download(params, = nil) params.validate show_params = Karaden::Param::Message::Bulk::BulkMessageShowParams.new_builder .with_id(params.id) .build unless Karaden::Service::BulkMessageService.(params.max_retries, params.retry_interval, show_params, ) raise Exception::BulkMessageShowRetryLimitExceedException end = Param::Message::Bulk::BulkMessageListMessageParams.new_builder .with_id(params.id) .build download_url = Karaden::Service::BulkMessageService.get_download_url(params.max_retries, params.retry_interval, , ) raise Exception::BulkMessageListMessageRetryLimitExceedException if download_url.nil? begin Karaden::Service::BulkMessageService.get_contents(download_url, params.directory_path, ) rescue StandardError raise Exception::FileDownloadFailedException end end |
.get_contents(download_url, directory_path, request_options = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/karaden/service/bulk_message_service.rb', line 42 def self.get_contents(download_url, directory_path, = nil) uri = URI.parse(download_url) http = ::Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http.open_timeout, http.read_timeout = Karaden::Utility.get_timeout() http.request_get(uri.request_uri) do |response| match = response['content-disposition'].match(Karaden::Service::BulkMessageService::REGEX_PATTERN) raise Exception::FileDownloadFailedException unless match filename = File.join(File.realpath(directory_path), match[1]) File.open(filename, 'w') do |file| response.read_body do |chunk| file.write(chunk) end end end true end |
.get_download_url(retry_count, retry_interval, params, request_options) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/karaden/service/bulk_message_service.rb', line 76 def self.get_download_url(retry_count, retry_interval, params, ) result = nil (0..retry_count).each do |count| # sleep retry_interval if count.positive? output = Karaden::Model::BulkMessage.(params, ) unless output.nil? result = output break end end result end |