Class: CustomersMailCloud::Client
- Inherits:
-
Object
- Object
- CustomersMailCloud::Client
- Defined in:
- lib/customers_mail_cloud/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_user ⇒ Object
Returns the value of attribute api_user.
-
#attachments ⇒ Object
Returns the value of attribute attachments.
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#envfrom ⇒ Object
Returns the value of attribute envfrom.
-
#from ⇒ Object
Returns the value of attribute from.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#html ⇒ Object
Returns the value of attribute html.
-
#replyto ⇒ Object
Returns the value of attribute replyto.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#text ⇒ Object
Returns the value of attribute text.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #block ⇒ Object
- #bounce ⇒ Object
- #delivery ⇒ Object
-
#initialize(api_user, api_key) ⇒ Client
constructor
A new instance of Client.
- #pro(subdomain) ⇒ Object
- #send ⇒ Object
- #standard ⇒ Object
- #trial ⇒ Object
Constructor Details
#initialize(api_user, api_key) ⇒ Client
Returns a new instance of Client.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/customers_mail_cloud/client.rb', line 8 def initialize api_user, api_key @api_user = api_user @api_key = api_key @endpoints = { trial: "https://sandbox.smtps.jp/api/v2/emails/send.json", standard: "https://te.smtps.jp/api/v2/emails/send.json", pro: "https://SUBDOMAIN.smtps.jp/api/v2/emails/send.json" } @url = "" @to = [] @from = nil @subject = '' @text = '' @html = '' @env_from = nil @reply_to = nil @headers = {} @charset = 'UTF-8' = [] end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def api_key @api_key end |
#api_user ⇒ Object
Returns the value of attribute api_user.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def api_user @api_user end |
#attachments ⇒ Object
Returns the value of attribute attachments.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def end |
#charset ⇒ Object
Returns the value of attribute charset.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def charset @charset end |
#envfrom ⇒ Object
Returns the value of attribute envfrom.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def envfrom @envfrom end |
#from ⇒ Object
Returns the value of attribute from.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def from @from end |
#headers ⇒ Object
Returns the value of attribute headers.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def headers @headers end |
#html ⇒ Object
Returns the value of attribute html.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def html @html end |
#replyto ⇒ Object
Returns the value of attribute replyto.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def replyto @replyto end |
#subject ⇒ Object
Returns the value of attribute subject.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def subject @subject end |
#text ⇒ Object
Returns the value of attribute text.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
28 29 30 |
# File 'lib/customers_mail_cloud/client.rb', line 28 def to @to end |
Instance Method Details
#block ⇒ Object
51 52 53 |
# File 'lib/customers_mail_cloud/client.rb', line 51 def block Transaction.new 'blocks', self end |
#bounce ⇒ Object
43 44 45 |
# File 'lib/customers_mail_cloud/client.rb', line 43 def bounce Transaction.new 'bounces', self end |
#delivery ⇒ Object
47 48 49 |
# File 'lib/customers_mail_cloud/client.rb', line 47 def delivery Transaction.new 'deliveries', self end |
#pro(subdomain) ⇒ Object
38 39 40 41 |
# File 'lib/customers_mail_cloud/client.rb', line 38 def pro(subdomain) raise Error.new 'サブドメインは必須です' if subdomain == nil || subdomain == '' @url = @endpoints[:pro].gsub('SUBDOMAIN', subdomain) end |
#send ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/customers_mail_cloud/client.rb', line 55 def send raise Error.new '契約プランを選択してください(trial/standard/pro)' if @url == nil || @url == '' raise Error.new '送信元アドレスは必須です' if @from == nil raise Error.new '送り先が指定されていません' if @to.size == 0 raise Error.new '件名は必須です' if @subject == '' raise Error.new 'メール本文は必須です' if @text == '' params = { api_user: @api_user, api_key: @api_key, to: @to.map(&:to_h), from: @from.to_h, subject: @subject, text: @text } params[:envfrom] = @env_from if @env_from params[:replyto] = @reply_to.to_h if @reply_to if (@headers.empty?) params[:headers] = @headers.map do |key, value| { name: key, value: value } end end params[:charset] = @charset params[:html] = @html if @html != '' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } uri = URI.parse(@url) if .size > 0 params[:attachments] = .size [:to, :from].each do |k| params[k] = params[k].to_json end .each_with_index do |a, i| if a.is_a? String a = File.new(a) end mimeType = MIME::Types.type_for(a.path)[0] params["attachment#{i + 1}"] = UploadIO.new a, mimeType ? mimeType.to_s : 'application/octet-stream', File.basename(a) end req = Net::HTTP::Post::Multipart.new(uri.path, params) else req = Net::HTTP::Post.new(uri.path) req.body = params.to_json headers.each do |k, v| req[k] = v end end http = Net::HTTP.new uri.host, uri.port http.use_ssl = true response = http.request req if response.code == "200" return JSON.parse response.body else = JSON.parse(response.body)['errors'].map do |error| "#{error['message']} (#{error['code']})" end.join(" ") raise Error.new end end |
#standard ⇒ Object
34 35 36 |
# File 'lib/customers_mail_cloud/client.rb', line 34 def standard @url = @endpoints[:standard] end |
#trial ⇒ Object
30 31 32 |
# File 'lib/customers_mail_cloud/client.rb', line 30 def trial @url = @endpoints[:trial] end |