Class: Mailgun::Client
- Inherits:
-
Object
- Object
- Mailgun::Client
- Defined in:
- lib/mailgun/client.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.
Class Method Summary collapse
-
.deliveries ⇒ Hash
Provides a store of all the emails sent in test mode so you can check them.
Instance Method Summary collapse
-
#delete(resource_path) ⇒ Mailgun::Response
Generic Mailgun DELETE Handler.
-
#disable_test_mode! ⇒ Object
Disable test mode.
-
#enable_test_mode! ⇒ Object
Enable test mode.
-
#get(resource_path, params = nil, accept = '*/*') ⇒ Mailgun::Response
Generic Mailgun GET Handler.
-
#initialize(api_key = Mailgun.api_key, api_host = 'api.mailgun.net', api_version = 'v3', ssl = true, test_mode = false, timeout = nil, proxy_url = nil) ⇒ Client
constructor
A new instance of Client.
-
#post(resource_path, data, headers = {}) ⇒ Mailgun::Response
Generic Mailgun POST Handler.
-
#put(resource_path, data) ⇒ Mailgun::Response
Generic Mailgun PUT Handler.
-
#send_message(working_domain, data) ⇒ Mailgun::Response
Simple Message Sending.
-
#suppressions(domain) ⇒ Mailgun::Suppressions
Constructs a Suppressions client for the given domain.
-
#test_mode? ⇒ Boolean
Client is in test mode?.
Constructor Details
#initialize(api_key = Mailgun.api_key, api_host = 'api.mailgun.net', api_version = 'v3', ssl = true, test_mode = false, timeout = nil, proxy_url = nil) ⇒ Client
Returns a new instance of Client.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mailgun/client.rb', line 13 def initialize(api_key = Mailgun.api_key, api_host = 'api.mailgun.net', api_version = 'v3', ssl = true, test_mode = false, timeout = nil, proxy_url = nil) rest_client_params = { user: 'api', password: api_key, user_agent: "mailgun-sdk-ruby/#{Mailgun::VERSION}" } rest_client_params[:timeout] = timeout if timeout endpoint = endpoint_generator(api_host, api_version, ssl) RestClient.proxy = proxy_url @http_client = RestClient::Resource.new(endpoint, rest_client_params) @test_mode = test_mode end |
Class Method Details
.deliveries ⇒ Hash
Provides a store of all the emails sent in test mode so you can check them.
58 59 60 |
# File 'lib/mailgun/client.rb', line 58 def self.deliveries @@deliveries ||= [] end |
Instance Method Details
#delete(resource_path) ⇒ Mailgun::Response
Generic Mailgun DELETE Handler
with. Be sure to include your domain, where necessary.
155 156 157 158 159 160 |
# File 'lib/mailgun/client.rb', line 155 def delete(resource_path) response = @http_client[resource_path].delete Response.new(response) rescue => err raise communication_error err end |
#disable_test_mode! ⇒ Object
Disable test mode
Reverts the test_mode flag and allows the client to send messages.
44 45 46 |
# File 'lib/mailgun/client.rb', line 44 def disable_test_mode! @test_mode = false end |
#enable_test_mode! ⇒ Object
Enable test mode
Prevents sending of any messages.
37 38 39 |
# File 'lib/mailgun/client.rb', line 37 def enable_test_mode! @test_mode = true 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.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/mailgun/client.rb', line 125 def get(resource_path, params = nil, accept = '*/*') 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 => err raise communication_error err end |
#post(resource_path, data, headers = {}) ⇒ Mailgun::Response
Generic Mailgun POST Handler
with. Be sure to include your domain, where necessary. containing required parameters for the requested resource.
110 111 112 113 114 115 |
# File 'lib/mailgun/client.rb', line 110 def post(resource_path, data, headers = {}) response = @http_client[resource_path].post(data, headers) Response.new(response) rescue => err raise communication_error err 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.
143 144 145 146 147 148 |
# File 'lib/mailgun/client.rb', line 143 def put(resource_path, data) response = @http_client[resource_path].put(data) Response.new(response) rescue => err raise communication_error err end |
#send_message(working_domain, data) ⇒ Mailgun::Response
Simple Message Sending
containing required parameters for the requested resource.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mailgun/client.rb', line 68 def (working_domain, data) perform_data_validation(working_domain, data) if test_mode? then Mailgun::Client.deliveries << data return Response.from_hash( { :body => "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}", :code => 200, } ) end case data when Hash # Remove nil values from the data hash # Submitting nils to the API will likely cause an error. # See also: https://github.com/mailgun/mailgun-ruby/issues/32 data = data.select { |k, v| v != nil } if data.key?(:message) if data[:message].is_a?(String) data[:message] = convert_string_to_file(data[:message]) end return post("#{working_domain}/messages.mime", data) end post("#{working_domain}/messages", data) when MessageBuilder post("#{working_domain}/messages", data.) else fail ParameterError.new('Unknown data type for data parameter.', data) end end |
#suppressions(domain) ⇒ Mailgun::Suppressions
Constructs a Suppressions client for the given domain.
166 167 168 |
# File 'lib/mailgun/client.rb', line 166 def suppressions(domain) Suppressions.new(self, domain) end |
#test_mode? ⇒ Boolean
Client is in test mode?
51 52 53 |
# File 'lib/mailgun/client.rb', line 51 def test_mode? @test_mode end |