Skip to content

Instantly share code, notes, and snippets.

@d-konovalov
Created March 24, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-konovalov/526ebf3e623deddf45ac to your computer and use it in GitHub Desktop.
Save d-konovalov/526ebf3e623deddf45ac to your computer and use it in GitHub Desktop.
class MyMailgun
def initialize
@api_key = 'key-********************************'
@mg_client = Mailgun::Client.new @api_key
@domain = 'sandbox********************.mailgun.org'
@mg_events = Mailgun::Events.new(@mg_client, @domain)
end
def get_activation_link(email)
last_email(email)['body-plain'].match(/(https?:.+confirm-email\/[a-f0-9]{32})/).to_s
end
def get_pwd_change_link(email)
last_email(email)['body-plain'].match(/(https?:.+reset-password\/.+#{email}.+[a-f0-9]{32})/).to_s
end
def inbox(email)
results = []
while results.empty?
results = @mg_events.get({event:'stored'}).to_h['items']
results.select! { |result| result['message']['headers']['to'] == email }
end
results
end
def emails_count(email)
inbox(email).count
end
def last_email(email)
_inbox = inbox(email)
email_id = _inbox.first['storage']['key']
@mg_client.get("domains/#{@domain}/messages/#{email_id}").to_h
end
def last_email_subject(email)
inbox(email).first['message']['headers']['subject']
end
def generate_email
UUID.generate(:compact).to_s + '@sandbox******************.mailgun.org'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment