Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_unsubscribe_tracking api method support #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mailgun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 8 additions & 2 deletions lib/mailgun/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/mailgun/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions lib/mailgun/events.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions lib/mailgun/secure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/mailgun/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/domain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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