Class: CustomersMailCloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/customers_mail_cloud/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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'
  @attachments = []
end

Instance Attribute Details

#api_keyObject

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_userObject

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

#attachmentsObject

Returns the value of attribute attachments.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def attachments
  @attachments
end

#charsetObject

Returns the value of attribute charset.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def charset
  @charset
end

#envfromObject

Returns the value of attribute envfrom.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def envfrom
  @envfrom
end

#fromObject

Returns the value of attribute from.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def from
  @from
end

#headersObject

Returns the value of attribute headers.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def headers
  @headers
end

#htmlObject

Returns the value of attribute html.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def html
  @html
end

#replytoObject

Returns the value of attribute replyto.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def replyto
  @replyto
end

#subjectObject

Returns the value of attribute subject.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def subject
  @subject
end

#textObject

Returns the value of attribute text.



28
29
30
# File 'lib/customers_mail_cloud/client.rb', line 28

def text
  @text
end

#toObject

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

#blockObject



51
52
53
# File 'lib/customers_mail_cloud/client.rb', line 51

def block
  Transaction.new 'blocks', self
end

#bounceObject



43
44
45
# File 'lib/customers_mail_cloud/client.rb', line 43

def bounce
  Transaction.new 'bounces', self
end

#deliveryObject



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

#sendObject



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 @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
    message = JSON.parse(response.body)['errors'].map do |error|
      "#{error['message']} (#{error['code']})"
    end.join(" ")
    raise Error.new message
  end
end

#standardObject



34
35
36
# File 'lib/customers_mail_cloud/client.rb', line 34

def standard
  @url = @endpoints[:standard]
end

#trialObject



30
31
32
# File 'lib/customers_mail_cloud/client.rb', line 30

def trial
  @url = @endpoints[:trial]
end