alexmchale-commerce-bank-client 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/commercebank.rb +1 -1
- metadata +1 -2
- data/lib/commercebank/gmail.rb +0 -156
data/VERSION.yml
CHANGED
data/lib/commercebank.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alexmchale-commerce-bank-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex McHale
|
@@ -27,7 +27,6 @@ files:
|
|
27
27
|
- lib/commercebank.rb
|
28
28
|
- lib/commercebank
|
29
29
|
- lib/commercebank/monkey.rb
|
30
|
-
- lib/commercebank/gmail.rb
|
31
30
|
- lib/commercebank/appconfig.rb
|
32
31
|
- test/monkeypatch_test.rb
|
33
32
|
- test/test_helper.rb
|
data/lib/commercebank/gmail.rb
DELETED
@@ -1,156 +0,0 @@
|
|
1
|
-
require 'openssl'
|
2
|
-
require 'net/smtp'
|
3
|
-
require 'base64'
|
4
|
-
|
5
|
-
class GMail
|
6
|
-
def initialize(username, password)
|
7
|
-
@username = username
|
8
|
-
@password = password
|
9
|
-
end
|
10
|
-
|
11
|
-
def start(to, subject, body, body_type = 'text/plain')
|
12
|
-
@to = to.to_s
|
13
|
-
@subject = subject.to_s
|
14
|
-
@body = body.to_s
|
15
|
-
@body_type = body_type
|
16
|
-
@attachments = []
|
17
|
-
|
18
|
-
self
|
19
|
-
end
|
20
|
-
|
21
|
-
def add_data(name, data, type)
|
22
|
-
@attachments << { :name => name, :data => data, :type => type }
|
23
|
-
|
24
|
-
self
|
25
|
-
end
|
26
|
-
|
27
|
-
def add_file(filename, content_type)
|
28
|
-
add_data File.basename(File.expand_path(filename)), File.read(filename), content_type
|
29
|
-
end
|
30
|
-
|
31
|
-
def add_jpeg(filename, data = nil)
|
32
|
-
name = File.basename filename
|
33
|
-
data ||= File.read(filename)
|
34
|
-
type = 'image/jpeg'
|
35
|
-
|
36
|
-
add_data name, data, type
|
37
|
-
end
|
38
|
-
|
39
|
-
def compose
|
40
|
-
boundary = rand(2**128).to_s(16)
|
41
|
-
|
42
|
-
if @attachments.length > 0
|
43
|
-
attachments = @attachments.map do |attachment|
|
44
|
-
"--#{boundary}\r\n" +
|
45
|
-
"Content-Type: #{attachment[:type]}; name=\"#{attachment[:name]}\"\r\n" +
|
46
|
-
"Content-Disposition: attachment; filename=\"#{attachment[:name]}\"\r\n" +
|
47
|
-
"Content-Transfer-Encoding: base64\r\n" +
|
48
|
-
"\r\n" +
|
49
|
-
Base64.encode64(attachment[:data])
|
50
|
-
end.compact.join
|
51
|
-
|
52
|
-
"Date: #{Time.now.to_s}\r\n" +
|
53
|
-
"From: #{@username}\r\n" +
|
54
|
-
"To: #{@to}\r\n" +
|
55
|
-
"Subject: #{@subject}\r\n" +
|
56
|
-
"MIME-Version: 1.0\r\n" +
|
57
|
-
"Content-Type: multipart/mixed; boundary=\"#{boundary}\"\r\n" +
|
58
|
-
"\r\n" +
|
59
|
-
"--#{boundary}\r\n" +
|
60
|
-
"Content-Type: text/plain\r\n" +
|
61
|
-
"\r\n" +
|
62
|
-
"#{@body}\r\n" +
|
63
|
-
"\r\n" +
|
64
|
-
attachments +
|
65
|
-
"--#{boundary}--\r\n" +
|
66
|
-
"\r\n.\r\n"
|
67
|
-
else
|
68
|
-
"Date: #{Time.now.to_s}\r\n" +
|
69
|
-
"From: #{@username}\r\n" +
|
70
|
-
"To: #{@to}\r\n" +
|
71
|
-
"Subject: #{@subject}\r\n" +
|
72
|
-
"Content-Type: text/plain\r\n" +
|
73
|
-
"\r\n" +
|
74
|
-
"#{@body}\r\n" +
|
75
|
-
"\r\n.\r\n"
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
def dispatch
|
80
|
-
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @username, @password, :plain) do |smtp|
|
81
|
-
smtp.send_message compose, @username, @to
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def send(to, subject, body, content_type = 'text/plan')
|
86
|
-
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @username, @password, :plain) do |smtp|
|
87
|
-
msg = "From: #{@username}\r\nTo: #{to}\r\nSubject: #{subject}\r\nContent-Type: #{content_type}\r\n\r\n#{body}"
|
88
|
-
smtp.send_message msg, @username, to
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# Net::SMTP monkeypatching was taken from:
|
94
|
-
# http://www.stephenchu.com/2006/06/how-to-use-gmail-smtp-server-to-send.html
|
95
|
-
Net::SMTP.class_eval do
|
96
|
-
private
|
97
|
-
def do_start(helodomain, user, secret, authtype)
|
98
|
-
raise IOError, 'SMTP session already started' if @started
|
99
|
-
check_auth_args(user, secret) if (user or secret)
|
100
|
-
|
101
|
-
sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
|
102
|
-
@socket = Net::InternetMessageIO.new(sock)
|
103
|
-
@socket.read_timeout = 60 #@read_timeout
|
104
|
-
@socket.debug_output = STDERR #@debug_output
|
105
|
-
|
106
|
-
check_response(critical { recv_response() })
|
107
|
-
do_helo(helodomain)
|
108
|
-
|
109
|
-
raise 'openssl library not installed' unless defined?(OpenSSL)
|
110
|
-
starttls
|
111
|
-
ssl = OpenSSL::SSL::SSLSocket.new(sock)
|
112
|
-
ssl.sync_close = true
|
113
|
-
ssl.connect
|
114
|
-
@socket = Net::InternetMessageIO.new(ssl)
|
115
|
-
@socket.read_timeout = 60 #@read_timeout
|
116
|
-
@socket.debug_output = STDERR #@debug_output
|
117
|
-
do_helo(helodomain)
|
118
|
-
|
119
|
-
authenticate user, secret, authtype if user
|
120
|
-
@started = true
|
121
|
-
ensure
|
122
|
-
unless @started
|
123
|
-
# authentication failed, cancel connection.
|
124
|
-
@socket.close if not @started and @socket and not @socket.closed?
|
125
|
-
@socket = nil
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
def do_helo(helodomain)
|
130
|
-
begin
|
131
|
-
if @esmtp
|
132
|
-
ehlo helodomain
|
133
|
-
else
|
134
|
-
helo helodomain
|
135
|
-
end
|
136
|
-
rescue Net::ProtocolError
|
137
|
-
if @esmtp
|
138
|
-
@esmtp = false
|
139
|
-
@error_occured = false
|
140
|
-
retry
|
141
|
-
end
|
142
|
-
raise
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def starttls
|
147
|
-
getok('STARTTLS')
|
148
|
-
end
|
149
|
-
|
150
|
-
def quit
|
151
|
-
begin
|
152
|
-
getok('QUIT')
|
153
|
-
rescue EOFError
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|