Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem install mailgun-ruby
Gemfile:

```ruby
gem 'mailgun-ruby', '~>1.1.1'
gem 'mailgun-ruby', '~>1.1.2'
```

Usage
Expand All @@ -45,7 +45,7 @@ Or obtain the last couple log items:

```ruby
# First, instantiate the Mailgun Client with your API key
mg_client = Mailgun::Client.new 'your-api-key'
mg_client = Mailgun::Client.new 'your-secret-api-key'

# Define the domain you wish to query
domain = 'example.com'
Expand All @@ -60,7 +60,7 @@ Rails
The library can be initialized with a Rails initializer containing similar:
```ruby
Mailgun.configure do |config|
config.api_key = 'your-secret-key'
config.api_key = 'your-secret-api-key'
end
```
Or have the initializer read your environment setting if you perfer.
Expand Down
34 changes: 33 additions & 1 deletion lib/mailgun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,36 @@ class Client
def initialize(api_key = Mailgun.api_key,
api_host = 'api.mailgun.net',
api_version = 'v3',
ssl = true)
ssl = true,
test_mode = false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this default to Mailgun.test_mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpandya91 So actually that's a really good point. It looks like Mailgun.test_mode was defined in an earlier commit but was never actually used? So this actually could make use of that field. I'll dig through the code again to make sure it's unused and add that as the default value. Thanks!


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

# Enable test mode
#
# Prevents sending of any messages.
def enable_test_mode!
@test_mode = true
end

# Disable test mode
#
# Reverts the test_mode flag and allows the client to send messages.
def disable_test_mode!
@test_mode = false
end

# Client is in test mode?
#
# @return [Boolean] Is the client set in test mode?
def test_mode?
@test_mode
end

# Simple Message Sending
Expand All @@ -28,6 +51,15 @@ def initialize(api_key = Mailgun.api_key,
# containing required parameters for the requested resource.
# @return [Mailgun::Response] A Mailgun::Response object.
def send_message(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)
Expand Down
7 changes: 7 additions & 0 deletions lib/mailgun/response.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'ostruct'

module Mailgun
# A Mailgun::Response object is instantiated for each response generated
# by the Client request. The Response object supports deserialization of
Expand All @@ -10,6 +12,11 @@ class Response
# slightly different
attr_accessor :body, :code

def self.from_hash(h)
# Create a "fake" response object with the data passed from h
self.new OpenStruct.new(h)
end

def initialize(response)
@body = response.body
@code = response.code
Expand Down
2 changes: 1 addition & 1 deletion lib/mailgun/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# It's the version. Yeay!
module Mailgun
VERSION = '1.1.1'
VERSION = '1.1.2'
end
22 changes: 22 additions & 0 deletions spec/integration/mailgun_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@
expect(result.body).to include("id")
end

it 'fakes message send while in *client* test mode' do
@mg_obj.enable_test_mode!

expect(@mg_obj.test_mode?).to eq(true)

data = { :from => "joe@#{@domain}",
:to => "bob@#{@domain}",
:subject => "Test",
:text => "Test Data" }

result = @mg_obj.send_message(@domain, data)

result.to_h!

expect(result.body).to include("message")
expect(result.body).to include("id")

expect(result.code).to eq(200)
expect(result.body['id']).to eq("test-mode-mail@localhost")
expect(result.body['message']).to eq("Queued. Thank you.")
end

it 'sends a message builder message in test mode.' do
mb_obj = Mailgun::MessageBuilder.new()
mb_obj.from("sender@#{@domain}", {'first' => 'Sending', 'last' => 'User'})
Expand Down
16 changes: 8 additions & 8 deletions spec/unit/mailgun_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

it 'sends a message' do
data = { 'from' => 'joe@test.com',
'to' => 'bob@example.com',
'subject' => 'Test',
'text' => 'Test Data'}
'to' => 'bob@example.com',
'subject' => 'Test',
'text' => 'Test Data' }
result = @mg_obj.send_message("testdomain.com", data)

result.to_h!
Expand All @@ -42,7 +42,7 @@
result = @mg_obj.send_message("testdomain.com", data)

result.to_h!

expect(result.body).to include("message")
expect(result.body).to include("id")
end
Expand All @@ -61,7 +61,7 @@
result = @mg_obj.post("#{@domain}/messages", data)

result.to_h!

expect(result.body).to include("message")
expect(result.body).to include("id")
end
Expand All @@ -81,7 +81,7 @@
result = @mg_obj.put("lists/#{@list_address}/members#{@member_address}", data)

result.to_h!

expect(result.body).to include("member")
expect(result.body["member"]).to include("vars")
expect(result.body["member"]["vars"]).to include("age")
Expand All @@ -104,7 +104,7 @@
result = @mg_obj.get("#{@domain}/bounces", query_string)

result.to_h!

expect(result.body).to include("total_count")
expect(result.body).to include("items")
end
Expand All @@ -120,7 +120,7 @@
result = @mg_obj.delete("#{@domain}/campaigns/ABC123")

result.to_h!

expect(result.body).to include("message")
expect(result.body).to include("id")
end
Expand Down