Class: Karaden::Service::BulkMessageService

Inherits:
Object
  • Object
show all
Defined in:
lib/karaden/service/bulk_message_service.rb

Constant Summary collapse

REGEX_PATTERN =
/filename="([^"]+)"/.freeze

Class Method Summary collapse

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.check_bulk_message_status(retry_count, retry_interval, params, request_options)
  result = false
  (0..retry_count).each do |count|
    # sleep retry_interval if count.positive?
    bulk_message = Karaden::Model::BulkMessage.show(params, request_options)
    raise Exception::BulkMessageCreateFailedException if bulk_message.status == Karaden::Model::BulkMessage::STATUS_ERROR

    if bulk_message.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, request_options = nil)
  raise Karaden::Exception::FileNotFoundException unless File.file?(filename)

  bulk_file = Karaden::Model::BulkFile.create(request_options)

  Karaden::Utility.put_signed_url(bulk_file.url, filename, 'text/csv', request_options)

  params = Karaden::Param::Message::Bulk::BulkMessageCreateParams
             .new_builder
             .with_bulk_file_id(bulk_file.id)
             .build
  Karaden::Model::BulkMessage.create(params, request_options)
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, request_options = nil)
  params.validate
  show_params = Karaden::Param::Message::Bulk::BulkMessageShowParams.new_builder
    .with_id(params.id)
    .build
  unless Karaden::Service::BulkMessageService.check_bulk_message_status(params.max_retries, params.retry_interval, show_params, request_options)
    raise Exception::BulkMessageShowRetryLimitExceedException
  end

  list_message_params = 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, list_message_params, request_options)
  raise Exception::BulkMessageListMessageRetryLimitExceedException if download_url.nil?

  begin
    Karaden::Service::BulkMessageService.get_contents(download_url, params.directory_path, request_options)
  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, request_options = 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(request_options)
  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, request_options)
  result = nil
  (0..retry_count).each do |count|
    # sleep retry_interval if count.positive?
    output = Karaden::Model::BulkMessage.list_message(params, request_options)
    unless output.nil?
      result = output
      break
    end
  end
  result
end