Class: Mailgun::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mailgun_api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
# File 'lib/mailgun_api.rb', line 93

def initialize(params={})
  raise ArgumentError.new(":api_key is a required argument to initialize Mailgun") if params.fetch(:api_key).nil?

  @host         = params.fetch(:host)
  @protocol     = params.fetch(:protocol)
  @api_version  = params.fetch(:api_version)
  @test_mode    = params.fetch(:test_mode)
  @api_key      = params.fetch(:api_key)
  @domain       = params.fetch(:domain, nil)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def api_key
  @api_key
end

#api_versionObject

Returns the value of attribute api_version.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def api_version
  @api_version
end

#domainObject

Returns the value of attribute domain.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def domain
  @domain
end

#hostObject

Returns the value of attribute host.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def host
  @host
end

#protocolObject

Returns the value of attribute protocol.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def protocol
  @protocol
end

#responseObject

Returns the value of attribute response.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def response
  @response
end

#test_modeObject

Returns the value of attribute test_mode.



87
88
89
# File 'lib/mailgun_api.rb', line 87

def test_mode
  @test_mode
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Mailgun::Base)

    the object that the method was called on



89
90
91
# File 'lib/mailgun_api.rb', line 89

def self.configure
  yield self
end

.fire(method, url, parameters = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mailgun_api.rb', line 138

def self.fire(method, url, parameters={})
  begin
    parameters = {:params => parameters} if method == :get    
    return JSON(RestClient.send(method, url, parameters))
  rescue => e
    error_message = nil
    if e.respond_to? :http_body
      begin
        error_message = JSON(e.http_body)["message"]
      rescue
        raise e
      end
      raise Mailgun::Error.new(error_message)
    end
    raise e
  end
end

Instance Method Details

#api_urlObject

Returns the api url used in all Mailgun API calls



133
134
135
# File 'lib/mailgun_api.rb', line 133

def api_url
  "#{protocol}://api:#{api_key}@#{host}/#{api_version}"
end

#create_list(list_name, options = {}) ⇒ Object



122
123
124
# File 'lib/mailgun_api.rb', line 122

def create_list(list_name, options={})
  Mailgun::List.new(self).create("#{list_name}@#{domain}", options)
end

#create_message(params) ⇒ Object



127
128
129
# File 'lib/mailgun_api.rb', line 127

def create_message(params)
  Mailgun::Message.new(params)
end

#domainsObject



114
115
116
# File 'lib/mailgun_api.rb', line 114

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

#find_domain(domain_name) ⇒ Object



118
119
120
# File 'lib/mailgun_api.rb', line 118

def find_domain(domain_name)
  Mailgun::Domain.new(self).list(domain_name)
end

#find_list(list_name) ⇒ Object



104
105
106
107
108
# File 'lib/mailgun_api.rb', line 104

def find_list(list_name)
  list = Mailgun::List.new(self)
  list.find("#{list_name}@#{domain}")
  list
end

#listsObject



110
111
112
# File 'lib/mailgun_api.rb', line 110

def lists
  Mailgun::List.new(self).list
end