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.
-
#from ⇒ Object
Returns the value of attribute from.
-
#html ⇒ Object
Returns the value of attribute html.
-
#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 |
# 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 = '' @attachments = [] end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def api_key @api_key end |
#api_user ⇒ Object
Returns the value of attribute api_user.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def api_user @api_user end |
#attachments ⇒ Object
Returns the value of attribute attachments.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def @attachments end |
#from ⇒ Object
Returns the value of attribute from.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def from @from end |
#html ⇒ Object
Returns the value of attribute html.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def html @html end |
#subject ⇒ Object
Returns the value of attribute subject.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def subject @subject end |
#text ⇒ Object
Returns the value of attribute text.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
24 25 26 |
# File 'lib/customers_mail_cloud/client.rb', line 24 def to @to end |
Instance Method Details
#block ⇒ Object
47 48 49 |
# File 'lib/customers_mail_cloud/client.rb', line 47 def block Transaction.new 'blocks', self end |
#bounce ⇒ Object
39 40 41 |
# File 'lib/customers_mail_cloud/client.rb', line 39 def bounce Transaction.new 'bounces', self end |
#delivery ⇒ Object
43 44 45 |
# File 'lib/customers_mail_cloud/client.rb', line 43 def delivery Transaction.new 'deliveries', self end |
#pro(subdomain) ⇒ Object
34 35 36 37 |
# File 'lib/customers_mail_cloud/client.rb', line 34 def pro(subdomain) raise Error.new 'サブドメインは必須です' if subdomain == nil || subdomain == '' @url = @endpoints[:pro].gsub('SUBDOMAIN', subdomain) end |
#send ⇒ Object
51 52 53 54 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 |
# File 'lib/customers_mail_cloud/client.rb', line 51 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.html = @html if self.html != '' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } uri = URI.parse(@url) if @attachments.size > 0 params[:attachments] = @attachments.size [:to, :from].each do |k| params[k] = params[k].to_json end @attachments.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
30 31 32 |
# File 'lib/customers_mail_cloud/client.rb', line 30 def standard @url = @endpoints[:standard] end |
#trial ⇒ Object
26 27 28 |
# File 'lib/customers_mail_cloud/client.rb', line 26 def trial @url = @endpoints[:trial] end |