Class: Postgarnet
- Inherits:
-
Object
- Object
- Postgarnet
- Defined in:
- lib/postgarnet.rb
Constant Summary collapse
- @@version =
"1.0.7"
Instance Method Summary collapse
- #connect(host: "smtp.gmail.com", port: 587) ⇒ Object
- #content(text = nil, payload = nil) ⇒ Object
- #disconnect ⇒ Object
- #from(author = nil) ⇒ Object
- #header(key, value = nil) ⇒ Object
- #headers(value = nil) ⇒ Object
-
#initialize(username: nil, password: nil) ⇒ Postgarnet
constructor
A new instance of Postgarnet.
- #login(username:, password:) ⇒ Object
- #logout ⇒ Object
- #send(client = "localhost", &block) ⇒ Object
- #smtp ⇒ Object
- #subject(text = nil, payload = nil) ⇒ Object
- #to(recipient = nil) ⇒ Object
Constructor Details
#initialize(username: nil, password: nil) ⇒ Postgarnet
Returns a new instance of Postgarnet.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/postgarnet.rb', line 18 def initialize(username: nil, password: nil) @username, @password, @subject, @content, @recipient, = @username, @smtp = nil unless username.nil? || password.nil? login(username: username, password: password) end @headers = { "mime-version" => "1", "content-type" => "text/html" } end |
Instance Method Details
#connect(host: "smtp.gmail.com", port: 587) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/postgarnet.rb', line 104 def connect(host: "smtp.gmail.com", port: 587) if @username.nil? || @password.nil? raise TransactionError, "You need to login first" end begin @smtp = Net::SMTP.new(host, port) @smtp.enable_starttls if block_given? block.call end rescue Exception => e raise TransactionError, "Error when connecting: #{e}" else true end end |
#content(text = nil, payload = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/postgarnet.rb', line 43 def content(text=nil, payload=nil) unless text.nil? || text.is_a?(String) raise FormatError, "Subject must be a string" end unless payload.nil? || payload.is_a?(Hash) raise FormatError, "Payload must be a hash" end if text.nil? return @content else @content = payload.nil? ? text : use_payload(payload, text) end end |
#disconnect ⇒ Object
144 145 146 147 148 149 |
# File 'lib/postgarnet.rb', line 144 def disconnect if @smtp.nil? raise TransactionError, "No connection to disconnect" end @smtp = nil end |
#from(author = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/postgarnet.rb', line 68 def from(=nil) unless .nil? || .is_a?(String) raise FormatError, "Author (from) must be a string" end if .nil? return else = end end |
#header(key, value = nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/postgarnet.rb', line 79 def header(key, value=nil) unless key.is_a?(String) || key.is_a?(Symbol) raise FormatError, "Header key must be a string or a symbol" end unless value.nil? || value.is_a?(String) raise FormatError, "Header value must be a string" end if value.nil? return @headers[key] else @headers[key.downcase] = value end end |
#headers(value = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/postgarnet.rb', line 93 def headers(value=nil) unless value.nil? || value.is_a?(Hash) raise FormatError, "Header value must be a hash" end if value.nil? return @headers.clone else @headers = value.clone end end |
#login(username:, password:) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/postgarnet.rb', line 121 def login(username:, password:) unless @smtp.nil? raise TransactionError, "A connection already exists. Disconnect to the server first to login a new account." end if username.include?("@") username = username.split("@")[0] end @username = "#{username}@gmail.com" @password = password return @username.clone end |
#logout ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/postgarnet.rb', line 133 def logout if @username.nil? && @password.nil? raise TransactionError, "No account to log out" end unless @smtp.nil? raise TransactionError, "To log out, disconnect to the server first." end @username = nil @password = nil end |
#send(client = "localhost", &block) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/postgarnet.rb', line 158 def send(client="localhost", &block) if [@username, @password, , @recipient, @subject, @content].any?(&:nil?) raise FormatError, "Incomplete parameters" end if @smtp.nil? raise TransactionError, "Connect to the server first" end begin = self.create_msg @smtp.start(client, @username, @password, :plain) do @smtp.(, @username, @recipient) if block_given? block.call end true end rescue Exception => e raise TransactionError, "Error when sending: #{e}" end end |
#smtp ⇒ Object
151 152 153 154 155 156 |
# File 'lib/postgarnet.rb', line 151 def smtp if @smtp.nil? raise TransactionError, "Connect to the server first" end return @smtp end |
#subject(text = nil, payload = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/postgarnet.rb', line 29 def subject(text=nil, payload=nil) unless text.nil? || text.is_a?(String) raise FormatError, "Subject must be a string" end unless payload.nil? || payload.is_a?(Hash) raise FormatError, "Payload must be a hash" end if text.nil? return @subject else @subject = payload.nil? ? text : use_payload(payload, text) end end |
#to(recipient = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/postgarnet.rb', line 57 def to(recipient=nil) unless recipient.nil? || recipient.is_a?(String) raise FormatError, "Recipient (to) must be a string" end if recipient.nil? return @recipient else @recipient = recipient end end |