Skip to content

Instantly share code, notes, and snippets.

@tcaddy
Created October 25, 2019 18:12
Show Gist options
  • Save tcaddy/85afc1bcb96c2400b93e7c85f8f39da6 to your computer and use it in GitHub Desktop.
Save tcaddy/85afc1bcb96c2400b93e7c85f8f39da6 to your computer and use it in GitHub Desktop.
GMail usage example
callback = lambda do |result, err|
if err
# handle error
else
require_relative './mail'
mail = ::CheckEmail::Mail.new(result)
end
end
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
@tcaddy
Copy link
Author

tcaddy commented Oct 25, 2019

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment