htmlgun 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/htmlgun.rb +26 -0
  2. data/lib/mailgun.rb +174 -0
  3. metadata +76 -0
data/lib/htmlgun.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'mailgun'
2
+ require 'mail'
3
+
4
+ class HTMLgun
5
+
6
+ def self.init(api_key, api_url = "https://mailgun.net/api/")
7
+ Mailgun::init api_key, api_url
8
+ end
9
+
10
+ def self.send_html the_sender, the_recipient, the_subject,the_body
11
+ mail = Mail.new do
12
+ from the_sender
13
+ to the_recipient
14
+ subject the_subject
15
+
16
+ html_part do
17
+ content_type "text/html; charset=UTF-8"
18
+ body the_body
19
+ end
20
+ end
21
+
22
+ MailgunMessage::send_raw( the_sender, the_recipient, mail.to_s )
23
+ end
24
+
25
+
26
+ end
data/lib/mailgun.rb ADDED
@@ -0,0 +1,174 @@
1
+ require 'rubygems'
2
+ require 'active_resource'
3
+
4
+ class Mailgun
5
+
6
+ # Initializes Mailgun API
7
+ # Must be called before any other method
8
+ #
9
+ # Parameters:
10
+ # api_key - Your API key
11
+ # api_url - API base URL
12
+ #
13
+ def self.init(api_key, api_url = "https://mailgun.net/api/")
14
+ MailgunResource.password = api_key
15
+ api_url = api_url.gsub(/\/$/, '') + "/"
16
+ MailgunResource.site = api_url
17
+ end
18
+
19
+ # This is a patch of private ActiveResource method.
20
+ # It takes HTTPResponse and raise AR-like error if response code is not 2xx
21
+ def self.handle_response(response)
22
+ case response.code.to_i
23
+ when 301,302
24
+ raise(Redirection.new(response))
25
+ when 200...400
26
+ response
27
+ when 400
28
+ raise(ActiveResource::BadRequest.new(response))
29
+ when 401
30
+ raise(ActiveResource::UnauthorizedAccess.new(response))
31
+ when 403
32
+ raise(ActiveResource::ForbiddenAccess.new(response))
33
+ when 404
34
+ raise(ActiveResource::ResourceNotFound.new(response))
35
+ when 405
36
+ raise(ActiveResource::MethodNotAllowed.new(response))
37
+ when 409
38
+ raise(ActiveResource::ResourceConflict.new(response))
39
+ when 410
40
+ raise(ActiveResource::ResourceGone.new(response))
41
+ when 422
42
+ raise(ActiveResource::ResourceInvalid.new(response))
43
+ when 401...500
44
+ raise(ActiveResource::ClientError.new(response))
45
+ when 500...600
46
+ raise(ActiveResource::ServerError.new(response))
47
+ else
48
+ raise(ConnectionError.new(response, "Unknown response code: #{response.code}"))
49
+ end
50
+ end
51
+
52
+ module RequestBuilder
53
+ def prepare_request(url_string)
54
+ uri = URI.parse(url_string)
55
+ http = Net::HTTP.new(uri.host, uri.port)
56
+ http.use_ssl = true if uri.port == 443
57
+ return [http, (uri.path + '?' + uri.query)]
58
+ end
59
+ end
60
+ end
61
+
62
+
63
+
64
+
65
+ class MailgunMessage
66
+ extend Mailgun::RequestBuilder
67
+
68
+ MAILGUN_TAG = 'X-Mailgun-Tag'
69
+
70
+ # Sends a MIME-formatted message
71
+ #
72
+ # raw_mime =
73
+ # "Content-Type: text/plain;charset=utf-8\n" +
74
+ # "From: me@host\n" +
75
+ # "To: you@host\n" +
76
+ # "Subject: Hello!\n\n" +
77
+ # "Body"
78
+ # MailgunMessage::send_raw("me@host", "you@host", raw_mime)
79
+ #
80
+ def self.send_raw(sender, recipients, raw_body, servername='')
81
+ uri_str = "#{MailgunResource.site}messages.eml?api_key=#{MailgunResource.password}&servername=#{servername}"
82
+ http, url = prepare_request(uri_str)
83
+ data = "#{sender}\n#{recipients}\n\n#{raw_body}"
84
+ res = http.post(url, data, {"Content-type" => "text/plain" })
85
+ Mailgun::handle_response(res)
86
+ end
87
+
88
+ # Sends a plain-text message
89
+ #
90
+ # MailgunMessage::send_text("me@host.com",
91
+ # "you@host.com",
92
+ # "Subject",
93
+ # "Hi!\nThis is message body")
94
+ #
95
+ def self.send_text(sender, recipients, subject, text, servername='', options = nil)
96
+ uri_str = "#{MailgunResource.site}messages.txt?api_key=#{MailgunResource.password}&servername=#{servername}"
97
+ params = { :sender => sender, :recipients => recipients, :subject => subject, :body => text}
98
+ unless options.nil?
99
+ params['options'] = ActiveSupport::JSON.encode(options)
100
+ end
101
+ http, url = prepare_request(uri_str)
102
+ req = Net::HTTP::Post.new(url)
103
+ req.set_form_data(params)
104
+ res = http.request(req)
105
+ Mailgun::handle_response(res)
106
+ end
107
+ end
108
+
109
+
110
+
111
+ # Base class for Mailgun resource classes
112
+ # It adds upsert() method on top of ActiveResource::Base
113
+ #
114
+ class MailgunResource < ActiveResource::Base
115
+ self.user = "api_key"
116
+ extend Mailgun::RequestBuilder
117
+
118
+ # Create new resource or update it if resource already exist.
119
+ # There are 2 differences between upsert() and save():
120
+ # - Upsert does not raise an exception if a resource already exist.
121
+ # - Upsert does not return the id of freshly inserted resource
122
+ #
123
+ # >> route = Route.new
124
+ # >> route.pattern = '*@mydomain.com'
125
+ # >> route.destination = 'http://mydomain.com/addcomment'
126
+ # >> route.upsert()
127
+ #
128
+ def upsert()
129
+ self.class.post('upsert', {}, self.to_xml())
130
+ end
131
+ end
132
+
133
+
134
+
135
+ # All mail arriving to email addresses that have mailboxes associated
136
+ # will be stored on the server and can be later accessed via IMAP or POP3
137
+ # protocols.
138
+ #
139
+ # Mailbox has several properties:
140
+ #
141
+ # alex@gmail.com
142
+ # ^ ^
143
+ # | |
144
+ # user domain
145
+ #
146
+ class Mailbox < MailgunResource
147
+ # Example of a CSV file:
148
+ #
149
+ # john@domain.com, password
150
+ # doe@domain.com, password2
151
+ #
152
+ def self.upsert_from_csv(mailboxes)
153
+ uri_str = "#{MailgunResource.site}mailboxes.txt?api_key=#{MailgunResource.password}"
154
+ http, url = prepare_request(uri_str)
155
+ res = http.post(url, mailboxes, {"Content-type" => "text/plain" })
156
+ Mailgun::handle_response(res)
157
+ end
158
+ end
159
+
160
+
161
+ # This class represents Mailgun route.
162
+ # A route has 2 properties: pattern and destination
163
+ #
164
+ # Examples of patterns:
165
+ # - '*' - match all
166
+ # - exact match (foo@bar.com)
167
+ # - domain pattern, i.e. a pattern like "@example.com" - it will match all emails going to example.com
168
+ # - any regular expression
169
+ #
170
+ # Destination can be one of:
171
+ # - an email address to forward to
172
+ # - a URL for HTTP POST
173
+ class Route < MailgunResource
174
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: htmlgun
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Paul Santa Clara
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-20 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mail
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 0
31
+ version: 2.3.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: ""
35
+ email: kesserich1@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - lib/mailgun.rb
44
+ - lib/htmlgun.rb
45
+ has_rdoc: true
46
+ homepage: ""
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: ""
75
+ test_files: []
76
+