Created
March 24, 2016 14:07
-
-
Save d-konovalov/526ebf3e623deddf45ac to your computer and use it in GitHub Desktop.
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
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