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.
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) ⇒ Client
constructor
A new instance of Client.
-
#post(resource_path, data) ⇒ 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.
-
#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) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mailgun/client.rb', line 12 def initialize(api_key = Mailgun.api_key, api_host = 'api.mailgun.net', api_version = 'v3', ssl = true, test_mode = false) 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}") @test_mode = test_mode end |
Instance Method Details
#delete(resource_path) ⇒ Mailgun::Response
Generic Mailgun DELETE Handler
with. Be sure to include your domain, where necessary.
130 131 132 133 134 135 |
# File 'lib/mailgun/client.rb', line 130 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.
36 37 38 |
# File 'lib/mailgun/client.rb', line 36 def disable_test_mode! @test_mode = false end |
#enable_test_mode! ⇒ Object
Enable test mode
Prevents sending of any messages.
29 30 31 |
# File 'lib/mailgun/client.rb', line 29 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.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/mailgun/client.rb', line 100 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) ⇒ Mailgun::Response
Generic Mailgun POST Handler
with. Be sure to include your domain, where necessary. containing required parameters for the requested resource.
86 87 88 89 90 91 |
# File 'lib/mailgun/client.rb', line 86 def post(resource_path, data) response = @http_client[resource_path].post(data) 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.
118 119 120 121 122 123 |
# File 'lib/mailgun/client.rb', line 118 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mailgun/client.rb', line 53 def (working_domain, data) if test_mode? then return Response.from_hash( { :body => '{"id": "test-mode-mail@localhost", "message": "Queued. Thank you."}', :code => 200, } ) end case data when Hash 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 |
#test_mode? ⇒ Boolean
Client is in test mode?
43 44 45 |
# File 'lib/mailgun/client.rb', line 43 def test_mode? @test_mode end |