Class: Blastengine::Mail

Inherits:
Base
  • Object
show all
Includes:
Blastengine
Defined in:
lib/blastengine/mail.rb

Constant Summary

Constants included from Blastengine

BASE_PATH, DOMAIN, VERSION

Instance Attribute Summary collapse

Attributes inherited from Base

#created_time, #delivery_time, #delivery_type, #drop_count, #hard_error_count, #open_count, #sent_count, #soft_error_count, #status, #total_count, #updated_time

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Blastengine

#email, initialize

Methods inherited from Base

#cancel, #client, client, #delete, #get, #set, #sets, #unsubscribe

Constructor Details

#initializeMail

Returns a new instance of Mail.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/blastengine/mail.rb', line 8

def initialize
  @to = []
  @cc = []
  @bcc = []
  @attachments = []
  @encode = "UTF-8"
  @list_unsubscribe = {
    url: "",
    email: ""
  }
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def attachments
  @attachments
end

#bccObject

Returns the value of attribute bcc.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def bcc
  @bcc
end

#ccObject

Returns the value of attribute cc.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def cc
  @cc
end

#delivery_idObject

Returns the value of attribute delivery_id.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def delivery_id
  @delivery_id
end

#encodeObject

Returns the value of attribute encode.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def encode
  @encode
end

#html_partObject

Returns the value of attribute html_part.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def html_part
  @html_part
end

#list_unsubscribeObject

Returns the value of attribute list_unsubscribe.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def list_unsubscribe
  @list_unsubscribe
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def subject
  @subject
end

#text_partObject

Returns the value of attribute text_part.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def text_part
  @text_part
end

#toObject

Returns the value of attribute to.



6
7
8
# File 'lib/blastengine/mail.rb', line 6

def to
  @to
end

Class Method Details

.all(params = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/blastengine/mail.rb', line 112

def self.all(params = {})
  Hash[ params.map{|k,v| [k.to_sym, v] } ]
  if params[:delivery_start] != nil
    params[:delivery_start] = params[:delivery_start].iso8601
  end
  if params[:delivery_end] != nil
    params[:delivery_end] = params[:delivery_end].iso8601
  end
  query_string = URI.encode_www_form(params)
  url = "/deliveries/all?#{query_string}";
  res = Mail.client.get url
  return res['data'].map {|params| Mail.from_hash params }
end

.find(params = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/blastengine/mail.rb', line 98

def self.find(params = {})
  Hash[ params.map{|k,v| [k.to_sym, v] } ]
  if params[:delivery_start] != nil
    params[:delivery_start] = params[:delivery_start].iso8601
  end
  if params[:delivery_end] != nil
    params[:delivery_end] = params[:delivery_end].iso8601
  end
  query_string = URI.encode_www_form(params)
  url = "/deliveries?#{query_string}";
  res = Mail.client.get url
  return res['data'].map {|params| Mail.from_hash params }
end

.from_hash(params) ⇒ Object



92
93
94
95
96
# File 'lib/blastengine/mail.rb', line 92

def self.from_hash(params)
  mail = params["delivery_type"] == "TRANSACTION" ? Transaction.new : Bulk.new
  mail.sets(params);
  mail
end

Instance Method Details

#addTo(email:, insert_code: {}) ⇒ Object

受信者の追加



30
31
32
33
34
35
# File 'lib/blastengine/mail.rb', line 30

def addTo(email:, insert_code: {})
  @to << {
    email: email,
    insert_code: insert_code
  }
end

#from(email:, name: "") ⇒ Object

送信主の追加



23
24
25
# File 'lib/blastengine/mail.rb', line 23

def from(email:, name: "")
  @_from = {email: email, name: name}
end

#send(date = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/blastengine/mail.rb', line 37

def send(date = nil)
  # CCまたはBCCがある場合はTransaction × Toの分
  # Toが複数の場合はBulk、Toが1つの場合はTransaction
  if @cc.size > 0 || @bcc.size > 0
    # CCまたはBCCがある場合は、指定時刻送信はできない
    raise "CC or BCC is not supported when sending at a specified time." if date != nil
    raise "CC or BCC is not supported when sending to multiple recipients." if @to.size > 1
  end
  if date == nil
    return self.sendTransaction();
  end
  return self.sendBulk(date)
end

#sendBulk(date = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/blastengine/mail.rb', line 51

def sendBulk(date = nil)
  bulk = Bulk.new
  bulk.from email: @_from[:email], name: @_from[:name]
  bulk.subject = @subject
  bulk.encode = @encode
  bulk.text_part = @text_part
  bulk.html_part = @html_part
  bulk.unsubscribe url: @list_unsubscribe[:url], email: @list_unsubscribe[:email]
  if @attachments.size > 0
    bulk.attachments = @attachments
  end
  bulk.register
  @to.each do |to|
    bulk.addTo(to[:email], to[:insert_code])
  end
  bulk.update
  bulk.send date
  @delivery_id = bulk.delivery_id
  return true;
end

#sendTransactionObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/blastengine/mail.rb', line 72

def sendTransaction
  transaction = Transaction.new
  transaction.from email: @_from[:email], name: @_from[:name]
  transaction.subject = @subject
  transaction.encode = @encode
  transaction.text_part = @text_part
  transaction.html_part = @html_part
  transaction.to = @to[0][:email]
  transaction.insert_code = @to[0][:insert_code] if @to[0][:insert_code].size > 0
  transaction.cc = @cc if @cc.size > 0
  transaction.bcc = @bcc if @bcc.size > 0
  transaction.unsubscribe url: @list_unsubscribe[:url], email: @list_unsubscribe[:email]
  if @attachments.size > 0
    transaction.attachments = @attachments
  end
  transaction.send
  @delivery_id = transaction.delivery_id
  return true;
end