Class: Blastengine::Bulk

Inherits:
Base
  • Object
show all
Includes:
Blastengine
Defined in:
lib/blastengine/bulk.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

Instance Method Summary collapse

Methods included from Blastengine

#email, initialize

Methods inherited from Base

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

Constructor Details

#initializeBulk

Returns a new instance of Bulk.



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

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

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def attachments
  @attachments
end

#delivery_idObject

Returns the value of attribute delivery_id.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def delivery_id
  @delivery_id
end

#encodeObject

Returns the value of attribute encode.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def encode
  @encode
end

#html_partObject

Returns the value of attribute html_part.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def html_part
  @html_part
end

#jobObject

Returns the value of attribute job.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def job
  @job
end

#list_unsubscribeObject

Returns the value of attribute list_unsubscribe.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def list_unsubscribe
  @list_unsubscribe
end

#subjectObject

Returns the value of attribute subject.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def subject
  @subject
end

#text_partObject

Returns the value of attribute text_part.



9
10
11
# File 'lib/blastengine/bulk.rb', line 9

def text_part
  @text_part
end

Instance Method Details

#addTo(email, params = {}) ⇒ Object

受信者の追加



56
57
58
59
60
61
62
63
64
# File 'lib/blastengine/bulk.rb', line 56

def addTo(email, params = {})
	@to << {
		email: email,
		insert_code: params.map{|key, value| {
			key: "__#{key}__",
			value: value
		}}
	}
end

#csv_updateObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/blastengine/bulk.rb', line 100

def csv_update
	f = Tempfile.create(['blastengine', '.csv'])
	f.close
	csv = CSV.open(f.path, "w")
	# ヘッダーの作成
	headers = ["email"]
	@to.each do |to|
		to[:insert_code].each do |insert_code|
			headers << insert_code[:key] if headers.index(insert_code[:key]) == nil
		end
	end
	csv.puts headers
	@to.each do |to|
		lines = [to[:email]]
		headers.each do |header|
			next if header == "email"
			insert_code = to[:insert_code].find{|insert_code| insert_code[:key] == header}
			lines << insert_code[:value] if insert_code != nil
		end
		csv.puts lines
	end
	csv.close
	job = self.import f.path
	while !job.finish?
		sleep 1
	end
	File.unlink f.path
	@delivery_id
end

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

送信主の追加



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

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

#import(file, ignore_errors = false) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/blastengine/bulk.rb', line 147

def import(file, ignore_errors = false)
	# APIリクエスト用のパス
	path = "/deliveries/#{@delivery_id}/emails/import"
	# API実行
	res = @@client.post path, {ignore_errors: ignore_errors}, [file]
	@job = Blastengine::Job.new res["job_id"]
	return @job
end

#normal_updateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/blastengine/bulk.rb', line 77

def normal_update
	# APIリクエスト用のパス
	path = "/deliveries/bulk/update/#{@delivery_id}"
	data = {
		to: @to
	}
	# subject, text_part, from, html_partはあれば追加
	data[:subject] = @subject unless @subject.nil?
	data[:from] = @_from unless @_from.nil?
	data[:text_part] = @text_part unless @text_part.nil?
	data[:html_part] = @html_part unless @html_part.nil?

	# 退会リンクがある場合は追加
	unless @list_unsubscribe.nil?
		data[:list_unsubscribe] = {}
		data[:list_unsubscribe][:url] = @list_unsubscribe[:url] if !@list_unsubscribe[:url].nil? && @list_unsubscribe[:url] != ""
		data[:list_unsubscribe][:mailto] = "mailto:#{@list_unsubscribe[:email]}" if !@list_unsubscribe[:email].nil? && @list_unsubscribe[:email] != ""
	end
	# API実行
	res = @@client.put path, data
	return res["delivery_id"]
end

#registerObject

バルクメールの登録



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blastengine/bulk.rb', line 30

def register
	# APIリクエスト用のパス
	path = "/deliveries/bulk/begin"
	data = {
		from: @_from,
		subject: @subject,
		encode: @encode,
		text_part: @text_part,
	}
	# HTMLパートがある場合は追加
	data[:html_part] = @html_part unless @html_part.nil?
	# 退会リンクがある場合は追加
	unless @list_unsubscribe.nil?
		data[:list_unsubscribe] = {}
		data[:list_unsubscribe][:url] = @list_unsubscribe[:url] if !@list_unsubscribe[:url].nil? && @list_unsubscribe[:url] != ""
		data[:list_unsubscribe][:mailto] = "mailto:#{@list_unsubscribe[:email]}" if !@list_unsubscribe[:email].nil? && @list_unsubscribe[:email] != ""
	end
	# API実行
	res = @@client.post path, data, @attachments
	@delivery_id = res["delivery_id"]
	return res["delivery_id"]
end

#send(date = nil) ⇒ Object

バルクメールの送信



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/blastengine/bulk.rb', line 133

def send(date = nil)
	# APIリクエスト用のパス
	path = date ? 
		"/deliveries/bulk/commit/#{@delivery_id}" :
		"/deliveries/bulk/commit/#{@delivery_id}/immediate"
	# APIリクエスト用のデータ
	data = date ? {reservation_time: date.iso8601} : {}
	# API実行
	res = @@client.patch path, data
	# エラーがあったら例外を投げるので、この場合は通常終了
	@delivery_id = res["delivery_id"]
	return res["delivery_id"]
end

#updateObject

バルクメールの更新



69
70
71
72
73
74
75
# File 'lib/blastengine/bulk.rb', line 69

def update
	if @to.size > 50
		return self.csv_update
	else
		return self.normal_update
	end
end