Skip to content

Commit 3c9c497

Browse files
authored
DE-1714: Add rubocop gem (#378)
* DE-1714: Add rubocop gem Signed-off-by: Alex Lebedev <6421109+alex-leb@users.noreply.github.com> * DE-1714: force autocorrect Signed-off-by: Alex Lebedev <6421109+alex-leb@users.noreply.github.com> * DE-1714: fix specs Signed-off-by: Alex Lebedev <6421109+alex-leb@users.noreply.github.com> --------- Signed-off-by: Alex Lebedev <6421109+alex-leb@users.noreply.github.com>
1 parent 8a038ad commit 3c9c497

62 files changed

Lines changed: 1262 additions & 1210 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.rubocop.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.rubocop_todo.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in mailgun.gemspec
46
gemspec
57

6-
gem 'mini_mime'
78
gem 'json', '~> 2.1', platform: :mri_19
9+
gem 'mini_mime'

Rakefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/gem_tasks'
24
require 'rake'
35
require 'rspec/core/rake_task'
@@ -9,21 +11,21 @@ end
911

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

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

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

lib/mailgun-ruby.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'mailgun'
24
require_relative 'railgun' if defined?(Rails) && defined?(ActionMailer)

lib/mailgun.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# require ruby dependencies
24
require 'json'
35
require 'openssl'
@@ -16,7 +18,7 @@
1618
require 'mailgun/helpers/api_version_checker'
1719

1820
# load zeitwerk
19-
Zeitwerk::Loader.for_gem.tap do |loader| # rubocop:disable Style/SymbolProc
21+
Zeitwerk::Loader.for_gem.tap do |loader|
2022
loader.ignore("#{__dir__}/mailgun-ruby.rb")
2123
loader.ignore("#{__dir__}/railgun.rb")
2224
loader.ignore("#{__dir__}/railgun")
@@ -41,7 +43,6 @@
4143
#
4244
# See the Github documentation for full examples.
4345
module Mailgun
44-
4546
class << self
4647
attr_accessor :api_host,
4748
:api_key,
@@ -56,7 +57,6 @@ def configure
5657
yield self
5758
true
5859
end
59-
alias_method :config, :configure
60+
alias config configure
6061
end
61-
6262
end

lib/mailgun/address.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
module Mailgun
1+
# frozen_string_literal: true
22

3+
module Mailgun
34
# Mailgun::Address is a simple interface to the Email Validation API.
45
class Address
56
def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
@@ -10,12 +11,11 @@ def initialize(api_key = Mailgun.api_key, api_host = Mailgun.api_host)
1011
#
1112
# @param [String] address Email address to validate (max 512 chars.)
1213
def validate(address, mailbox_verification = false)
13-
params = {address: address}
14+
params = { address: address }
1415
params[:mailbox_verification] = true if mailbox_verification
1516

16-
res = @client.get "address/validate", params
17-
return res.to_h!
17+
res = @client.get 'address/validate', params
18+
res.to_h!
1819
end
1920
end
20-
2121
end

lib/mailgun/chains.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
module Mailgun
1+
# frozen_string_literal: true
22

3+
module Mailgun
34
# Public constants used throughout
45
class Chains
5-
66
# maximum campaign ids per message
77
MAX_CAMPAIGN_IDS = 3
88

@@ -11,6 +11,5 @@ class Chains
1111

1212
# maximum recipients per message or batch
1313
MAX_RECIPIENTS = 1000
14-
1514
end
1615
end

lib/mailgun/client.rb

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1+
# frozen_string_literal: true
2+
13
module Mailgun
24
# A Mailgun::Client object is used to communicate with the Mailgun API. It is a
35
# wrapper around Faraday so you don't have to worry about the HTTP aspect
46
# of communicating with our API.
57
#
68
# See the Github documentation for full examples.
79
class Client
8-
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'.freeze
10+
SUBACCOUNT_HEADER = 'X-Mailgun-On-Behalf-Of'
911

1012
def initialize(api_key = Mailgun.api_key,
1113
api_host = Mailgun.api_host || 'api.mailgun.net',
12-
api_version = Mailgun.api_version || 'v3',
14+
api_version = Mailgun.api_version || 'v3',
1315
ssl = true,
14-
test_mode = !!Mailgun.test_mode,
16+
test_mode = !Mailgun.test_mode.nil?,
1517
timeout = nil,
1618
proxy_url = Mailgun.proxy_url)
17-
1819
endpoint = endpoint_generator(api_host, api_version, ssl)
1920

2021
request_options = {
2122
url: endpoint,
2223
proxy: proxy_url,
23-
ssl: {verify: ssl},
24+
ssl: { verify: ssl },
2425
headers: {
25-
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
26-
'Accept' =>'*/*'
27-
}
26+
'User-Agent' => "mailgun-sdk-ruby/#{Mailgun::VERSION}",
27+
'Accept' => '*/*'
28+
}
2829
}
29-
request_options.merge!(request: {timeout: timeout}) if timeout
30+
request_options.merge!(request: { timeout: timeout }) if timeout
3031

3132
@http_client = build_http_client(api_key, request_options)
3233

@@ -55,7 +56,7 @@ def set_api_key(api_key)
5556

5657
# Add subaccount id to headers
5758
def set_subaccount(subaccount_id)
58-
@http_client.headers = @http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
59+
@http_client.headers.merge!({ SUBACCOUNT_HEADER => subaccount_id })
5960
end
6061

6162
# Reset subaccount for primary usage
@@ -71,9 +72,7 @@ def test_mode?
7172
end
7273

7374
# @return [String] client api version
74-
def api_version
75-
@api_version
76-
end
75+
attr_reader :api_version
7776

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

94-
if test_mode? then
93+
if test_mode?
9594
Mailgun::Client.deliveries << data.dup
9695
return Response.from_hash(
9796
{
98-
:body => "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
99-
:status => 200,
97+
body: "{\"id\": \"test-mode-mail-#{SecureRandom.uuid}@localhost\", \"message\": \"Queued. Thank you.\"}",
98+
status: 200
10099
}
101100
)
102101
end
@@ -106,7 +105,7 @@ def send_message(working_domain, data)
106105
# Remove nil values from the data hash
107106
# Submitting nils to the API will likely cause an error.
108107
# See also: https://github.com/mailgun/mailgun-ruby/issues/32
109-
data = data.select { |k, v| v != nil }
108+
data = data.reject { |_k, v| v.nil? }
110109

111110
if data.key?(:message)
112111
if data[:message].is_a?(String)
@@ -119,7 +118,7 @@ def send_message(working_domain, data)
119118
when MessageBuilder
120119
post("#{working_domain}/messages", data.message)
121120
else
122-
fail ParameterError.new('Unknown data type for data parameter.', data)
121+
raise ParameterError.new('Unknown data type for data parameter.', data)
123122
end
124123
end
125124

@@ -134,8 +133,8 @@ def send_message(working_domain, data)
134133
def post(resource_path, data, headers = {})
135134
response = @http_client.post(resource_path, data, headers)
136135
Response.new(response)
137-
rescue => err
138-
raise communication_error err
136+
rescue StandardError => e
137+
raise communication_error e
139138
end
140139

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

153152
Response.new(response)
154-
rescue => err
155-
raise communication_error(err)
153+
rescue StandardError => e
154+
raise communication_error(e)
156155
end
157156

158157
# Generic Mailgun PUT Handler
@@ -165,8 +164,8 @@ def get(resource_path, params = {}, accept = '*/*')
165164
def put(resource_path, data, headers = {})
166165
response = @http_client.put(resource_path, data, headers)
167166
Response.new(response)
168-
rescue => err
169-
raise communication_error err
167+
rescue StandardError => e
168+
raise communication_error e
170169
end
171170

172171
# Generic Mailgun DELETE Handler
@@ -188,8 +187,8 @@ def delete(resource_path, params = nil, body_params = false)
188187
@http_client.delete(resource_path)
189188
end
190189
Response.new(response)
191-
rescue => err
192-
raise communication_error err
190+
rescue StandardError => e
191+
raise communication_error e
193192
end
194193

195194
# Constructs a Suppressions client for the given domain.
@@ -221,7 +220,7 @@ def convert_string_to_file(string)
221220
# @param [Boolean] ssl True, SSL. False, No SSL.
222221
# @return [string] concatenated URL string
223222
def endpoint_generator(api_host, api_version, ssl)
224-
ssl ? scheme = 'https' : scheme = 'http'
223+
scheme = ssl ? 'https' : 'http'
225224
if api_version
226225
"#{scheme}://#{api_host}/#{api_version}"
227226
else
@@ -235,28 +234,33 @@ def endpoint_generator(api_host, api_version, ssl)
235234
def communication_error(e)
236235
if e.respond_to?(:response) && e.response
237236
return case e.response_status
238-
when Unauthorized::CODE
239-
Unauthorized.new(e.message, e.response)
240-
when BadRequest::CODE
241-
BadRequest.new(e.message, e.response)
242-
else
243-
CommunicationError.new(e.message, e.response)
244-
end
237+
when Unauthorized::CODE
238+
Unauthorized.new(e.message, e.response)
239+
when BadRequest::CODE
240+
BadRequest.new(e.message, e.response)
241+
else
242+
CommunicationError.new(e.message, e.response)
243+
end
245244
end
246245
CommunicationError.new(e.message)
247246
end
248247

249248
def perform_data_validation(working_domain, data)
250249
message = data.respond_to?(:message) ? data.message : data
251-
fail ParameterError.new('Missing working domain', working_domain) unless working_domain
252-
fail ParameterError.new(
253-
'Missing `to` recipient, message should contain at least 1 recipient',
254-
working_domain
255-
) if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
256-
fail ParameterError.new(
250+
raise ParameterError.new('Missing working domain', working_domain) unless working_domain
251+
252+
if message.fetch('to', []).empty? && message.fetch(:to, []).empty?
253+
raise ParameterError.new(
254+
'Missing `to` recipient, message should contain at least 1 recipient',
255+
working_domain
256+
)
257+
end
258+
return unless message.fetch('from', []).empty? && message.fetch(:from, []).empty?
259+
260+
raise ParameterError.new(
257261
'Missing a `from` sender, message should contain at least 1 `from` sender',
258262
working_domain
259-
) if message.fetch('from', []).empty? && message.fetch(:from, []).empty?
263+
)
260264
end
261265

262266
def build_http_client(api_key, request_options)

0 commit comments

Comments
 (0)