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 = "v2", ssl = true) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_key,
               api_host="api.mailgun.net",
               api_version="v2",
               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}")
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:



121
122
123
124
125
126
127
128
# File 'lib/mailgun.rb', line 121

def delete(resource_path)
  begin
    response = @http_client[resource_path].delete()
    Response.new(response)
  rescue Exception => e
    raise CommunicationError.new(e), e.response
  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:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mailgun.rb', line 85

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
    raise CommunicationError.new(e), e.response
  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:



68
69
70
71
72
73
74
75
# File 'lib/mailgun.rb', line 68

def post(resource_path, data)
  begin
    response = @http_client[resource_path].post(data)
    Response.new(response)
  rescue Exception => e
    raise CommunicationError.new(e), e.response
  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:



106
107
108
109
110
111
112
113
# File 'lib/mailgun.rb', line 106

def put(resource_path, data)
  begin
    response = @http_client[resource_path].put(data)
    Response.new(response)
  rescue Exception => e
    raise CommunicationError.new(e), e.response
  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:



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

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