diff --git a/lib/mailgun.rb b/lib/mailgun.rb index d8a1b77..e394d73 100644 --- a/lib/mailgun.rb +++ b/lib/mailgun.rb @@ -6,6 +6,7 @@ require "mailgun/mailgun_error" require "mailgun/base" require "mailgun/domain" +require "mailgun/events" require "mailgun/route" require "mailgun/mailbox" require "mailgun/bounce" diff --git a/lib/mailgun/base.rb b/lib/mailgun/base.rb index 0811a7b..37d9959 100644 --- a/lib/mailgun/base.rb +++ b/lib/mailgun/base.rb @@ -73,6 +73,10 @@ def log(domain=Mailgun.domain) Mailgun::Log.new(self, domain) end + def events(domain=Mailgun.domain) + Mailgun::Events.new(self, domain) + end + def lists @lists ||= Mailgun::MailingList.new(self) end @@ -92,20 +96,22 @@ def self.submit(method, url, parameters={}) begin JSON.parse(Client.new(url).send(method, parameters)) rescue => e - error_code = e.http_code + error_code = e.http_code if e.respond_to?(:http_code) error_message = begin JSON(e.http_body)["message"] rescue JSON::ParserError '' end + error = Mailgun::Error.new( :code => error_code || nil, :message => error_message || nil ) + if error.handle.kind_of? Mailgun::ErrorBase raise error.handle else - raise error + raise error.error end end end diff --git a/lib/mailgun/domain.rb b/lib/mailgun/domain.rb index dd9fb3d..2eca41b 100644 --- a/lib/mailgun/domain.rb +++ b/lib/mailgun/domain.rb @@ -18,9 +18,9 @@ def find(domain) end # Add domain to account - def create(domain, opts = {}) - opts = {name: domain}.merge(opts) - Mailgun.submit :post, domain_url, opts + def create(domain, options = {}) + options = {name: domain}.merge(options) + Mailgun.submit :post, domain_url, options end # Remves a domain from account @@ -35,6 +35,11 @@ def verify(domain) Mailgun.submit :put, "#{domain_url(domain)}/verify" end + # Updates unsubscribe tracking settings for a domain + def update_unsubscribe_tracking(domain, options = {}) + Mailgun.submit :put, "#{domain_url(domain)}/tracking/unsubscribe", options + end + private # Helper method to generate the proper url for Mailgun domain API calls diff --git a/lib/mailgun/events.rb b/lib/mailgun/events.rb new file mode 100644 index 0000000..758391c --- /dev/null +++ b/lib/mailgun/events.rb @@ -0,0 +1,20 @@ +module Mailgun + class Events + # Used internally, called from Mailgun::Base + def initialize(mailgun, domain) + @mailgun = mailgun + @domain = domain + end + + # List all events for a given domain + def list(options={}) + Mailgun.submit(:get, events_url, options) + end + + private + + def events_url + "#{@mailgun.base_url}/#{@domain}/events" + end + end +end diff --git a/lib/mailgun/secure.rb b/lib/mailgun/secure.rb index 057bcb2..94b81b0 100644 --- a/lib/mailgun/secure.rb +++ b/lib/mailgun/secure.rb @@ -2,7 +2,7 @@ # var offset = Math.round((new Date()).getTime() / 1000) - (minutes_offset || 5) * 60; # if(timestamp < offset) # return false; -# +# # var hmac = crypto.createHmac('sha256', api_key); # hmac.update(timestamp + token); # return signature == hmac.digest('hex'); @@ -13,16 +13,16 @@ class Secure def initialize(mailgun) @mailgun = mailgun end - + # check request auth def check_request_auth(timestamp, token, signature, offset=-5) if offset != 0 offset = Time.now.to_i + offset * 60 return false if timestamp < offset end - + return signature == OpenSSL::HMAC.hexdigest( - OpenSSL::Digest::Digest.new('sha256'), + OpenSSL::Digest.new('sha256'), Mailgun.api_key, '%s%s' % [timestamp, token]) end diff --git a/lib/mailgun/webhook.rb b/lib/mailgun/webhook.rb index dee9f29..a99d96b 100644 --- a/lib/mailgun/webhook.rb +++ b/lib/mailgun/webhook.rb @@ -13,12 +13,13 @@ def initialize(mailgun, domain, url) # List of currently available webhooks def available_ids - %w(bounce deliver drop spam unsubscribe click open).map(&:to_sym) + %w(bounce deliver drop spam unsubscribe click open).map(&:to_sym) + + %w(delivered opened clicked temporary_fail permanent_fail complained unsubscribed).map(&:to_sym) end # Returns a list of webhooks set for the specified domain def list - Mailgun.submit(:get, webhook_url)["webhooks"] || [] + Mailgun.submit(:get, webhook_url)["webhooks"] || {} end # Returns details about the webhook specified diff --git a/spec/domain_spec.rb b/spec/domain_spec.rb index 744cd36..b920d15 100644 --- a/spec/domain_spec.rb +++ b/spec/domain_spec.rb @@ -77,4 +77,17 @@ @mailgun.domains.verify(@sample[:domain]) end end + + describe 'update domain unsubscribe tracking' do + it 'should make a PUT request to update unsubscribe tracking with correct params' do + sample_response = { 'message' => 'Domain tracking settings have been updated', 'unsubscribe' => { 'active' => true, 'html_footer' => ' ', 'text_footer' => ' ' } } + verify_domain_url = "#{@mailgun.domains.send(:domain_url, @sample[:domain])}/tracking/unsubscribe" + + expect(Mailgun).to receive(:submit) + .with(:put, verify_domain_url, { active: true, html_footer: ' ' }) + .and_return(sample_response) + + @mailgun.domains.update_unsubscribe_tracking(@sample[:domain], { active: true, html_footer: ' ' }) + end + end end