Created
October 25, 2019 18:12
-
-
Save tcaddy/85afc1bcb96c2400b93e7c85f8f39da6 to your computer and use it in GitHub Desktop.
GMail usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
callback = lambda do |result, err| | |
if err | |
# handle error | |
else | |
require_relative './mail' | |
mail = ::CheckEmail::Mail.new(result) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative './check_email' | |
class Mail < CheckEmail | |
attr_accessor :result | |
def message_id | |
result.id | |
end | |
def from | |
parse_header 'From' | |
end | |
def date | |
parse_header 'Date' | |
end | |
def subject | |
parse_header 'Subject' | |
end | |
def body | |
text || html || payload.body.data || '' | |
end | |
def text | |
parse_payload CheckEmail::MIME_TYPES[:text] | |
end | |
def html | |
parsed = parse_payload(CheckEmail::MIME_TYPES[:html]) | |
return parsed if parsed | |
text | |
end | |
private | |
def initialize(result) | |
@result = result | |
end | |
def payload | |
result.payload | |
end | |
def headers | |
payload.headers | |
end | |
def parse_payload(mime_type) | |
item = (payload.parts || []).find do |part| | |
part.mime_type == mime_type | |
end | |
return nil if item.nil? || item.body.nil? | |
item.body.data | |
end | |
def parse_header(key) | |
headers.any? { |h| h.name == key } ? headers.find { |h| h.name == key }.value : '' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason when I try to edit this Gist, the content is blank.
CheckEmail::MIME_TYPES[:html]
should really be'text/html'
CheckEmail::MIME_TYPES[:text]
should really be'text/plain'