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
# 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_keyObject

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_userObject

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

#attachmentsObject

Returns the value of attribute attachments.


24
25
26
# File 'lib/customers_mail_cloud/client.rb', line 24

def attachments
  @attachments
end

#fromObject

Returns the value of attribute from.


24
25
26
# File 'lib/customers_mail_cloud/client.rb', line 24

def from
  @from
end

#htmlObject

Returns the value of attribute html.


24
25
26
# File 'lib/customers_mail_cloud/client.rb', line 24

def html
  @html
end

#subjectObject

Returns the value of attribute subject.


24
25
26
# File 'lib/customers_mail_cloud/client.rb', line 24

def subject
  @subject
end

#textObject

Returns the value of attribute text.


24
25
26
# File 'lib/customers_mail_cloud/client.rb', line 24

def text
  @text
end

#toObject

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

#blockObject


47
48
49
# File 'lib/customers_mail_cloud/client.rb', line 47

def block
  Transaction.new 'blocks', self
end

#bounceObject


39
40
41
# File 'lib/customers_mail_cloud/client.rb', line 39

def bounce
  Transaction.new 'bounces', self
end

#deliveryObject


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

#sendObject


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

#standardObject


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

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

#trialObject


26
27
28
# File 'lib/customers_mail_cloud/client.rb', line 26

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