1+ # frozen_string_literal: true
2+
13module 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