Class: Mailgun::Base

Inherits:
Object show all
Defined in:
lib/mailgun/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Options taken from documentation.mailgun.net/quickstart.html#authentication

  • Mailgun host - location of mailgun api servers

  • Procotol - http or https [default to https]

  • API key and version

  • Test mode - if enabled, doesn’t actually send emails (see documentation.mailgun.net/user_manual.html#sending-in-test-mode)

  • Domain - domain to use

  • Webhook URL - default url to use if one is not specified in each request

  • Public API Key - used for address endpoint



12
13
14
15
16
17
18
19
20
21
# File 'lib/mailgun/base.rb', line 12

def initialize(options)
  Mailgun.mailgun_host    = options.fetch(:mailgun_host)    { "api.mailgun.net" }
  Mailgun.protocol        = options.fetch(:protocol)        { "https" }
  Mailgun.api_version     = options.fetch(:api_version)     { "v3" }
  Mailgun.test_mode       = options.fetch(:test_mode)       { false }
  Mailgun.api_key         = options.fetch(:api_key)         { raise ArgumentError.new(":api_key is a required argument to initialize Mailgun") if Mailgun.api_key.nil? }
  Mailgun.domain          = options.fetch(:domain)          { nil }
  Mailgun.webhook_url     = options.fetch(:webhook_url)     { nil }
  Mailgun.public_api_key  = options.fetch(:public_api_key)  { nil }
end

Instance Method Details

#addresses(domain = Mailgun.domain) ⇒ Object



61
62
63
64
65
66
# File 'lib/mailgun/base.rb', line 61

def addresses(domain = Mailgun.domain)
  if Mailgun.public_api_key.nil?
    raise ArgumentError.new(":public_api_key is a required argument to validate addresses")
  end
  Mailgun::Address.new(self)
end

#base_urlObject

Returns the base url used in all Mailgun API calls



24
25
26
# File 'lib/mailgun/base.rb', line 24

def base_url
  "#{Mailgun.protocol}://api:#{Mailgun.api_key}@#{Mailgun.mailgun_host}/#{Mailgun.api_version}"
end

#bounces(domain = Mailgun.domain) ⇒ Object



45
46
47
# File 'lib/mailgun/base.rb', line 45

def bounces(domain = Mailgun.domain)
  Mailgun::Bounce.new(self, domain)
end

#complaints(domain = Mailgun.domain) ⇒ Object



68
69
70
# File 'lib/mailgun/base.rb', line 68

def complaints(domain = Mailgun.domain)
  Mailgun::Complaint.new(self, domain)
end

#domainsObject



49
50
51
# File 'lib/mailgun/base.rb', line 49

def domains
  Mailgun::Domain.new(self)
end

#list_members(address) ⇒ Object



80
81
82
# File 'lib/mailgun/base.rb', line 80

def list_members(address)
  Mailgun::MailingList::Member.new(self, address)
end

#listsObject



76
77
78
# File 'lib/mailgun/base.rb', line 76

def lists
  @lists ||= Mailgun::MailingList.new(self)
end

#log(domain = Mailgun.domain) ⇒ Object



72
73
74
# File 'lib/mailgun/base.rb', line 72

def log(domain=Mailgun.domain)
  Mailgun::Log.new(self, domain)
end

#mailboxes(domain = Mailgun.domain) ⇒ Object

Returns an instance of Mailgun::Mailbox configured for the current API user



33
34
35
# File 'lib/mailgun/base.rb', line 33

def mailboxes(domain = Mailgun.domain)
  Mailgun::Mailbox.new(self, domain)
end

#messages(domain = Mailgun.domain) ⇒ Object



37
38
39
# File 'lib/mailgun/base.rb', line 37

def messages(domain = Mailgun.domain)
  @messages ||= Mailgun::Message.new(self, domain)
end

#public_base_urlObject



28
29
30
# File 'lib/mailgun/base.rb', line 28

def public_base_url
  "#{Mailgun.protocol}://api:#{Mailgun.public_api_key}@#{Mailgun.mailgun_host}/#{Mailgun.api_version}"
end

#routesObject



41
42
43
# File 'lib/mailgun/base.rb', line 41

def routes
  @routes ||= Mailgun::Route.new(self)
end

#secureObject



84
85
86
# File 'lib/mailgun/base.rb', line 84

def secure
  Mailgun::Secure.new(self)
end

#unsubscribes(domain = Mailgun.domain) ⇒ Object



53
54
55
# File 'lib/mailgun/base.rb', line 53

def unsubscribes(domain = Mailgun.domain)
  Mailgun::Unsubscribe.new(self, domain)
end

#webhooks(domain = Mailgun.domain, webhook_url = Mailgun.webhook_url) ⇒ Object



57
58
59
# File 'lib/mailgun/base.rb', line 57

def webhooks(domain = Mailgun.domain, webhook_url = Mailgun.webhook_url)
  Mailgun::Webhook.new(self, domain, webhook_url)
end