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
8 changes: 0 additions & 8 deletions .rubocop.yml

This file was deleted.

22 changes: 0 additions & 22 deletions .rubocop_todo.yml

This file was deleted.

4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in mailgun.gemspec
gemspec

gem 'mini_mime'
gem 'json', '~> 2.1', platform: :mri_19
gem 'mini_mime'
8 changes: 5 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake'
require 'rspec/core/rake_task'
Expand All @@ -9,21 +11,21 @@ end

desc 'Run unit specs'
RSpec::Core::RakeTask.new('spec:unit') do |t|
t.rspec_opts = %w(--colour --format documentation)
t.rspec_opts = %w[--colour --format documentation]
t.pattern = 'spec/unit/*_spec.rb', 'spec/unit/*/*_spec.rb'
end

desc 'Run integration specs'
# Before running integration tests, you need to specify
# a valid API KEY in the spec/spec_helper.rb file.
RSpec::Core::RakeTask.new('spec:integration') do |t|
t.rspec_opts = %w(--colour --format documentation)
t.rspec_opts = %w[--colour --format documentation]
t.pattern = 'spec/integration/*_spec.rb'
end

desc 'Run all tests'
RSpec::Core::RakeTask.new('spec:all') do |t|
t.rspec_opts = %w(--colour --format documentation)
t.rspec_opts = %w[--colour --format documentation]
t.pattern = 'spec/**/*_spec.rb'
end

Expand Down
2 changes: 2 additions & 0 deletions lib/mailgun-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

require_relative 'mailgun'
require_relative 'railgun' if defined?(Rails) && defined?(ActionMailer)
8 changes: 4 additions & 4 deletions lib/mailgun.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# require ruby dependencies
require 'json'
require 'openssl'
Expand All @@ -16,7 +18,7 @@
require 'mailgun/helpers/api_version_checker'

# load zeitwerk
Zeitwerk::Loader.for_gem.tap do |loader| # rubocop:disable Style/SymbolProc
Zeitwerk::Loader.for_gem.tap do |loader|
loader.ignore("#{__dir__}/mailgun-ruby.rb")
loader.ignore("#{__dir__}/railgun.rb")
loader.ignore("#{__dir__}/railgun")
Expand All @@ -41,7 +43,6 @@
#
# See the Github documentation for full examples.
module Mailgun

class << self
attr_accessor :api_host,
:api_key,
Expand All @@ -56,7 +57,6 @@ def configure
yield self
true
end
alias_method :config, :configure
alias config configure
end

end
10 changes: 5 additions & 5 deletions lib/mailgun/address.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Mailgun
# frozen_string_literal: true

module Mailgun
# Mailgun::Address is a simple interface to the Email Validation API.
class Address
def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
Expand All @@ -10,12 +11,11 @@ def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
#
# @param [String] address Email address to validate (max 512 chars.)
def validate(address, mailbox_verification = false)
params = {address: address}
params = { address: address }
params[:mailbox_verification] = true if mailbox_verification

res = @client.get "address/validate", params
return res.to_h!
res = @client.get 'address/validate', params
res.to_h!
end
end

end
5 changes: 2 additions & 3 deletions lib/mailgun/chains.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Mailgun
# frozen_string_literal: true

module Mailgun
# Public constants used throughout
class Chains

# maximum campaign ids per message
MAX_CAMPAIGN_IDS = 3

Expand All @@ -11,6 +11,5 @@ class Chains

# maximum recipients per message or batch
MAX_RECIPIENTS = 1000

end
end
86 changes: 45 additions & 41 deletions lib/mailgun/client.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# frozen_string_literal: true

module Mailgun
# A Mailgun::Client object is used to communicate with the Mailgun API. It is a
# wrapper around Faraday so you don't have to worry about the HTTP aspect
# of communicating with our API.
#
# See the Github documentation for full examples.
class Client
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'.freeze
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'

def initialize(api_key = Mailgun.api_key,
api_host = Mailgun.api_host || 'api.mailgun.net',
api_version = Mailgun.api_version || 'v3',
api_version = Mailgun.api_version || 'v3',
ssl = true,
test_mode = !!Mailgun.test_mode,
test_mode = !Mailgun.test_mode.nil?,
timeout = nil,
proxy_url = Mailgun.proxy_url)

endpoint = endpoint_generator(api_host, api_version, ssl)

request_options = {
url: endpoint,
proxy: proxy_url,
ssl: {verify: ssl},
ssl: { verify: ssl },
headers: {
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
'Accept' =>'*/*'
}
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
'Accept' => '*/*'
}
}
request_options.merge!(request: {timeout: timeout}) if timeout
request_options.merge!(request: { timeout: timeout }) if timeout

@http_client = build_http_client(api_key, request_options)

Expand Down Expand Up @@ -55,7 +56,7 @@ def set_api_key(api_key)

# Add subaccount id to headers
def set_subaccount(subaccount_id)
@http_client.headers = @http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
@http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
end

# Reset subaccount for primary usage
Expand All @@ -71,9 +72,7 @@ def test_mode?
end

# @return [String] client api version
def api_version
@api_version
end
attr_reader :api_version

# Provides a store of all the emails sent in test mode so you can check them.
#
Expand All @@ -91,12 +90,12 @@ def self.deliveries
def send_message(working_domain, data)
perform_data_validation(working_domain, data)

if test_mode? then
if test_mode?
Mailgun::Client.deliveries << data.dup
return Response.from_hash(
{
:body => "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
:status => 200,
body: "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
status: 200
}
)
end
Expand All @@ -106,7 +105,7 @@ def send_message(working_domain, data)
# 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 }
data = data.reject { |_k, v| v.nil? }

if data.key?(:message)
if data[:message].is_a?(String)
Expand All @@ -119,7 +118,7 @@ def send_message(working_domain, data)
when MessageBuilder
post("#{working_domain}/messages", data.message)
else
fail ParameterError.new('Unknown data type for data parameter.', data)
raise ParameterError.new('Unknown data type for data parameter.', data)
end
end

Expand All @@ -134,8 +133,8 @@ def send_message(working_domain, data)
def post(resource_path, data, headers = {})
response = @http_client.post(resource_path, data, headers)
Response.new(response)
rescue => err
raise communication_error err
rescue StandardError => e
raise communication_error e
end

# Generic Mailgun GET Handler
Expand All @@ -151,8 +150,8 @@ def get(resource_path, params = {}, accept = '*/*')
response = @http_client.get(resource_path, params, headers)

Response.new(response)
rescue => err
raise communication_error(err)
rescue StandardError => e
raise communication_error(e)
end

# Generic Mailgun PUT Handler
Expand All @@ -165,8 +164,8 @@ def get(resource_path, params = {}, accept = '*/*')
def put(resource_path, data, headers = {})
response = @http_client.put(resource_path, data, headers)
Response.new(response)
rescue => err
raise communication_error err
rescue StandardError => e
raise communication_error e
end

# Generic Mailgun DELETE Handler
Expand All @@ -188,8 +187,8 @@ def delete(resource_path, params = nil, body_params = false)
@http_client.delete(resource_path)
end
Response.new(response)
rescue => err
raise communication_error err
rescue StandardError => e
raise communication_error e
end

# Constructs a Suppressions client for the given domain.
Expand Down Expand Up @@ -221,7 +220,7 @@ def convert_string_to_file(string)
# @param [Boolean] ssl True, SSL. False, No SSL.
# @return [string] concatenated URL string
def endpoint_generator(api_host, api_version, ssl)
ssl ? scheme = 'https' : scheme = 'http'
scheme = ssl ? 'https' : 'http'
if api_version
"#{scheme}://#{api_host}/#{api_version}"
else
Expand All @@ -235,28 +234,33 @@ def endpoint_generator(api_host, api_version, ssl)
def communication_error(e)
if e.respond_to?(:response) && e.response
return case e.response_status
when Unauthorized::CODE
Unauthorized.new(e.message, e.response)
when BadRequest::CODE
BadRequest.new(e.message, e.response)
else
CommunicationError.new(e.message, e.response)
end
when Unauthorized::CODE
Unauthorized.new(e.message, e.response)
when BadRequest::CODE
BadRequest.new(e.message, e.response)
else
CommunicationError.new(e.message, e.response)
end
end
CommunicationError.new(e.message)
end

def perform_data_validation(working_domain, data)
message = data.respond_to?(:message) ? data.message : data
fail ParameterError.new('Missing working domain', working_domain) unless working_domain
fail ParameterError.new(
'Missing `to` recipient, message should contain at least 1 recipient',
working_domain
) if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
fail ParameterError.new(
raise ParameterError.new('Missing working domain', working_domain) unless working_domain

if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
raise ParameterError.new(
'Missing `to` recipient, message should contain at least 1 recipient',
working_domain
)
end
return unless message.fetch('from', []).empty? && message.fetch(:from, []).empty?

raise ParameterError.new(
'Missing a `from` sender, message should contain at least 1 `from` sender',
working_domain
) if message.fetch('from', []).empty? && message.fetch(:from, []).empty?
)
end

def build_http_client(api_key, request_options)
Expand Down
Loading