Class: Mailgun::Client

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

Overview

A Mailgun::Client object is used to communicate with the Mailgun API. It is a wrapper around RestClient so you don’t have to worry about the HTTP aspect of communicating with our API.

See the Github documentation for full examples.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_host = "api.mailgun.net", api_version = "v3", ssl = true) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mailgun.rb', line 23

def initialize(api_key,
               api_host="api.mailgun.net",
               api_version="v3",
               ssl=true)

  endpoint = endpoint_generator(api_host, api_version, ssl)
  @http_client = RestClient::Resource.new(endpoint,
                                          :user => "api",
                                          :password => api_key,
                                          :user_agent => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
                                          :ssl_version => "TLSv1")
end

Instance Method Details

#delete(resource_path) ⇒ Mailgun::Response

Generic Mailgun DELETE Handler

with. Be sure to include your domain, where necessary.

Parameters:

  • resource_path (String)

    This is the API resource you wish to interact

Returns:



129
130
131
132
133
134
135
136
# File 'lib/mailgun.rb', line 129

def delete(resource_path)
  begin
    response = @http_client[resource_path].delete()
    Response.new(response)
  rescue Exception => e
    communication_error e
  end
end

#get(resource_path, params = nil, accept = "*/*") ⇒ Mailgun::Response

Generic Mailgun GET Handler

with. Be sure to include your domain, where necessary. containing required parameters for the requested resource.

Parameters:

  • resource_path (String)

    This is the API resource you wish to interact

  • query_string (Hash)

    This should be a standard Hash

Returns:



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

def get(resource_path, params=nil, accept="*/*")
  begin
    if params
      response = @http_client[resource_path].get(:params => params, :accept => accept)
    else
      response = @http_client[resource_path].get(:accept => accept)
    end
    Response.new(response)
  rescue Exception => e
    communication_error e
  end
end

#post(resource_path, data) ⇒ Mailgun::Response

Generic Mailgun POST Handler

with. Be sure to include your domain, where necessary. containing required parameters for the requested resource.

Parameters:

  • resource_path (String)

    This is the API resource you wish to interact

  • data (Hash)

    This should be a standard Hash

Returns:



76
77
78
79
80
81
82
83
# File 'lib/mailgun.rb', line 76

def post(resource_path, data)
  begin
    response = @http_client[resource_path].post(data)
    Response.new(response)
  rescue Exception => e
    communication_error e
  end
end

#put(resource_path, data) ⇒ Mailgun::Response

Generic Mailgun PUT Handler

with. Be sure to include your domain, where necessary. containing required parameters for the requested resource.

Parameters:

  • resource_path (String)

    This is the API resource you wish to interact

  • data (Hash)

    This should be a standard Hash

Returns:



114
115
116
117
118
119
120
121
# File 'lib/mailgun.rb', line 114

def put(resource_path, data)
  begin
    response = @http_client[resource_path].put(data)
    Response.new(response)
  rescue Exception => e
    communication_error e
  end
end

#send_message(working_domain, data) ⇒ Mailgun::Response

Simple Message Sending

containing required parameters for the requested resource.

Parameters:

  • working_domain (String)

    This is the domain you wish to send from.

  • data (Hash)

    This should be a standard Hash

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mailgun.rb', line 43

def send_message(working_domain, data)
  case data
  when Hash
    if data.has_key?(:message)
      if data[:message].is_a?(String)
        data[:message] = convert_string_to_file(data[:message])
      end
      post("#{working_domain}/messages.mime", data)
    else
      post("#{working_domain}/messages", data)
    end
  when MessageBuilder
    post("#{working_domain}/messages", data.message)
  else
    raise ParameterError.new("Unknown data type for data parameter.", data)
  end
end

#validate_email(email) ⇒ Object

Validate an email address

Parameters:

  • email (String)

    This is the email address you wish to validate



64
65
66
# File 'lib/mailgun.rb', line 64

def validate_email(email)
  get('/address/validate', {address: email})
end