Class: SendCloud::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/sendcloud-mail.rb

Class Method Summary collapse

Class Method Details

.auth(mail_api_user, mail_api_key) ⇒ Object



14
15
16
17
# File 'lib/sendcloud-mail.rb', line 14

def self.auth(mail_api_user, mail_api_key)
  @mail_api_user = mail_api_user
  @mail_api_key = mail_api_key
end

.create(invokeName, name, html, subject, templateType) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sendcloud-mail.rb', line 78

def self.create(invokeName, name, html, subject, templateType)
  response = RestClient.post 'http://api.sendcloud.net/apiv2/template/add?',
                              apiUser: @mail_api_user,
                              apiKey: @mail_api_key,
                              invokeName: invokeName,
                              name: name,
                              html: html,
                              subject: subject,
                              templateType: templateType

  JSON.parse(response.to_s)
end

.delete(invokeName) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/sendcloud-mail.rb', line 103

def self.delete(invokeName)
  response = RestClient.post 'https://api.sendcloud.net/apiv2/template/delete?',
                              apiUser: @mail_api_user,
                              apiKey: @mail_api_key,
                              invokeName: invokeName
  JSON.parse(response.to_s)['statusCode']
end

.get(invokeName) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/sendcloud-mail.rb', line 59

def self.get(invokeName)
  params = {
    apiUser: @mail_api_user,
    apiKey: @mail_api_key,
    invokeName: invokeName,
  }
  response = RestClient.get 'https://api.sendcloud.net/apiv2/template/get?', params: params
  JSON.parse(response.to_s)
end

.listObject



69
70
71
72
73
74
75
76
# File 'lib/sendcloud-mail.rb', line 69

def self.list
  params = {
    apiUser: @mail_api_user,
    apiKey: @mail_api_key
  }
  response = RestClient.get 'https://api.sendcloud.net/apiv2/template/list?', params: params
  JSON.parse(response.to_s)
end

.load!(file, environment) ⇒ Object



9
10
11
12
# File 'lib/sendcloud-mail.rb', line 9

def self.load!(file, environment)
  config = YAML.load_file(file)
  self.auth(config[environment.to_s]['mail_api_user'], config[environment.to_s]['mail_api_key'])
end

.send(from, fromName, subject, xsmtpapi, content, summary = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sendcloud-mail.rb', line 19

def self.send(from, fromName, subject, xsmtpapi, content, summary=nil)
  # xsmtpapi comes in the following format:
  #{
  #  "to": ["[email protected]",'[email protected]'],
  #  "sub": {
  #      "%content%": ['nihao0', 'nihao1']
  #  }
  #}
  response = RestClient.post 'https://api.sendcloud.net/apiv2/mail/send?',
                             apiUser: @mail_api_user,
                             apiKey: @mail_api_key,
                             from: from,
                             fromName: fromName,
                             xsmtpapi: xsmtpapi.to_json,
                             subject: subject,
                             html: content,
                             contentSummary: summary
  JSON.parse(response.to_s)['statusCode']
end

.send_template(invokeName, from, fromName, xsmtpapi, subject = nil, summary = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sendcloud-mail.rb', line 39

def self.send_template(invokeName, from, fromName, xsmtpapi, subject=nil, summary=nil)
  # xsmtpapi comes in the following format:
  #{
  #  "to": ["[email protected]",'[email protected]'],
  #  "sub": {
  #      "%content%": ['nihao0', 'nihao1']
  #  }
  #}
  response = RestClient.post 'https://api.sendcloud.net/apiv2/mail/sendtemplate?',
                             apiUser: @mail_api_user,
                             apiKey: @mail_api_key,
                             from: from,
                             fromName: fromName,
                             xsmtpapi: xsmtpapi.to_json,
                             subject: subject,
                             templateInvokeName: invokeName,
                             contentSummary: summary
  JSON.parse(response.to_s)['statusCode']
end

.update(invokeName, name, html, subject, templateType) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sendcloud-mail.rb', line 91

def self.update(invokeName, name, html, subject, templateType)
  response = RestClient.post 'https://api.sendcloud.net/apiv2/template/update?',
                              apiUser: @mail_api_user,
                              apiKey: @mail_api_key,
                              invokeName: invokeName,
                              name: name,
                              html: html,
                              subject: subject,
                              templateType: templateType
  JSON.parse(response.to_s)['statusCode']
end