mailgunner 2.6.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffd33ded9bd33ca044bbe54fc8d89a876bb9949fb91c98ef0f677cc0ca82f4f5
4
- data.tar.gz: 7d4ba505f5b1de914fc360b23f64d14497e40e6ca01ecb4e8421560c0404e36d
3
+ metadata.gz: a596ae861173d54342b029a7c17985553cc7f7222722ec55e05e0344beacf08f
4
+ data.tar.gz: 481ac3d5b13702ca41598db5868d8b12c4f7a6302264bb216937938c60e1b3c7
5
5
  SHA512:
6
- metadata.gz: bfea517bd1e392add5a730c417a8aa479279503a574b9855e22a37a8bf1da56c8d972900173fbecb1cc39b73cf0c5eb6772a44b3681c3f2566c5ce9ff2aa4559
7
- data.tar.gz: 739a82133f746ad9117dbd6655dec76324de3b1cf3394c2c35672a44306e9cab643e7b29174f7cd2823a477d82e174cbb68a40cf21c54f4dd007dc0c528c1ac0
6
+ metadata.gz: eb7904e35154121959f7c7f392c72b6f472579ea02baec660487203674b601d9cedb039800fb0847f097f94283d93e52088c6bb00eb3641ce22d7643204f7e38
7
+ data.tar.gz: 3eb5b428d621fee6ea3605353869e479e8b765e858e1fe1f3e596ebcf7a095f0892d292be22e9cf956f6d9e5e294ec79dd6300b8addf193d36a01584af94bf43
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-2018 TIMCRAFT
1
+ Copyright (c) 2012-2019 TIMCRAFT
2
2
 
3
3
  This is an Open Source project licensed under the terms of the LGPLv3 license.
4
4
  Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
data/README.md CHANGED
@@ -11,20 +11,20 @@ Ruby client for the [Mailgun API](https://documentation.mailgun.com/en/latest/ap
11
11
  $ gem install mailgunner
12
12
 
13
13
 
14
- ## Quick start
14
+ ## Usage
15
15
 
16
16
  ```ruby
17
17
  require 'mailgunner'
18
18
 
19
- mailgun = Mailgunner::Client.new({
20
- domain: 'samples.mailgun.org',
21
- api_key: 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0'
22
- })
19
+ Mailgunner.configure do |config|
20
+ config.domain = 'samples.mailgun.org'
21
+ config.api_key = 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0'
22
+ end
23
23
 
24
- response = mailgun.get_domains
24
+ mailgun = Mailgunner::Client.new
25
25
 
26
- response['items'].each do |item|
27
- p item.values_at('id', 'name')
26
+ mailgun.get_domains.items.each do |item|
27
+ puts "#{item.id} #{item.name}"
28
28
  end
29
29
  ```
30
30
 
@@ -62,30 +62,12 @@ Outside of Rails you can set `ActionMailer::Base.delivery_method` directly.
62
62
 
63
63
  ## Specifying the region
64
64
 
65
- Mailgun offers both a US and EU region to send your e-mail from. Mailgunner uses
65
+ Mailgun offers both a US and EU region to send your email from. Mailgunner uses
66
66
  the US region by default. If you wish to use the EU region set the `api_host`
67
67
  config option like so:
68
68
 
69
69
  ```ruby
70
- mailgun = Mailgunner::Client.new({
71
- domain: 'eu.samples.mailgun.org',
72
- api_key: 'key-3ax6xnjp29jd6fds4gc373sgvjxteol0',
73
- api_host: 'api.eu.mailgun.net'
74
- })
75
- ```
76
-
77
-
78
- ## Email validation
79
-
80
- If you only need to use Mailgun's [email address validation service](http://documentation.mailgun.com/api-email-validation.html),
81
- you can instead use your Mailgun public key to authenticate like this:
82
-
83
- ```ruby
84
- require 'mailgunner'
85
-
86
- public_key = 'pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7'
87
-
88
- mailgun = Mailgunner::Client.new(api_key: public_key)
89
-
90
- response = mailgun.validate_address('john@gmail.com')
70
+ Mailgunner.configure do |config|
71
+ config.api_host = 'api.eu.mailgun.net'
72
+ end
91
73
  ```
@@ -1,5 +1,8 @@
1
1
  require 'mailgunner/version'
2
2
  require 'mailgunner/errors'
3
+ require 'mailgunner/params'
4
+ require 'mailgunner/config'
5
+ require 'mailgunner/struct'
3
6
  require 'mailgunner/client'
4
7
  require 'mailgunner/client/domains'
5
8
  require 'mailgunner/client/email_validation'
@@ -14,3 +17,13 @@ require 'mailgunner/client/tags'
14
17
  require 'mailgunner/client/webhooks'
15
18
  require 'mailgunner/delivery_method' if defined?(Mail)
16
19
  require 'mailgunner/railtie' if defined?(Rails)
20
+
21
+ module Mailgunner
22
+ def self.config
23
+ @config ||= Config.new
24
+ end
25
+
26
+ def self.configure
27
+ yield config
28
+ end
29
+ end
@@ -1,18 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
  require 'net/http'
3
3
  require 'json'
4
- require 'cgi'
5
4
 
6
5
  module Mailgunner
7
6
  class Client
8
- attr_accessor :domain, :api_key, :http
7
+ def initialize(domain: nil, api_key: nil, api_host: nil)
8
+ @domain = domain || Mailgunner.config.domain
9
9
 
10
- def initialize(options = {})
11
- @domain = options.fetch(:domain) { default_domain }
10
+ @api_key = api_key || Mailgunner.config.api_key
12
11
 
13
- @api_key = options.fetch(:api_key) { ENV.fetch('MAILGUN_API_KEY') }
14
-
15
- @api_host = options.fetch(:api_host) { 'api.mailgun.net' }
12
+ @api_host = api_host || Mailgunner.config.api_host
16
13
 
17
14
  @http = Net::HTTP.new(@api_host, Net::HTTP.https_default_port)
18
15
 
@@ -21,21 +18,20 @@ module Mailgunner
21
18
 
22
19
  private
23
20
 
24
- def default_domain
25
- return NoDomainProvided unless ENV.key?('MAILGUN_SMTP_LOGIN')
21
+ ATTRIBUTES = PARAMS = HEADERS = {}.freeze
26
22
 
27
- ENV['MAILGUN_SMTP_LOGIN'].to_s.split('@').last
28
- end
23
+ def get(path, query: PARAMS, headers: HEADERS)
24
+ uri = URI(path)
25
+ uri.query = Params.encode(query) unless query.empty?
29
26
 
30
- def get(path, params = {}, headers = {})
31
- request = Net::HTTP::Get.new(request_uri(path, params))
27
+ request = Net::HTTP::Get.new(uri.to_s)
32
28
 
33
29
  headers.each { |k, v| request[k] = v }
34
30
 
35
31
  transmit(request)
36
32
  end
37
33
 
38
- def post(path, attributes = {})
34
+ def post(path, attributes = ATTRIBUTES)
39
35
  transmit(Net::HTTP::Post.new(path)) { |message| message.set_form_data(attributes) }
40
36
  end
41
37
 
@@ -43,7 +39,7 @@ module Mailgunner
43
39
  transmit(Net::HTTP::Post.new(path)) { |message| message.set_form(data, 'multipart/form-data') }
44
40
  end
45
41
 
46
- def put(path, attributes = {})
42
+ def put(path, attributes = ATTRIBUTES)
47
43
  transmit(Net::HTTP::Put.new(path)) { |message| message.set_form_data(attributes) }
48
44
  end
49
45
 
@@ -51,52 +47,27 @@ module Mailgunner
51
47
  transmit(Net::HTTP::Delete.new(path))
52
48
  end
53
49
 
54
- USER_AGENT = "Ruby/#{RUBY_VERSION} Mailgunner/#{VERSION}"
55
-
56
50
  def transmit(message)
57
51
  message.basic_auth('api', @api_key)
58
- message['User-Agent'] = USER_AGENT
52
+ message['User-Agent'] = Mailgunner.config.user_agent
59
53
 
60
54
  yield message if block_given?
61
55
 
62
- parse(@http.request(message))
63
- end
56
+ response = @http.request(message)
64
57
 
65
- def parse(response)
66
- case response
67
- when Net::HTTPSuccess
68
- parse_success(response)
69
- when Net::HTTPUnauthorized
70
- raise AuthenticationError, "HTTP #{response.code}"
71
- when Net::HTTPClientError
72
- raise ClientError, "HTTP #{response.code}"
73
- when Net::HTTPServerError
74
- raise ServerError, "HTTP #{response.code}"
75
- else
76
- raise Error, "HTTP #{response.code}"
58
+ unless response.is_a?(Net::HTTPSuccess)
59
+ raise Error.parse(response)
77
60
  end
78
- end
79
61
 
80
- def parse_success(response)
81
- return JSON.parse(response.body) if json?(response)
62
+ if response['Content-Type']&.start_with?('application/json')
63
+ return JSON.parse(response.body, object_class: Struct)
64
+ end
82
65
 
83
66
  response.body
84
67
  end
85
68
 
86
- def json?(response)
87
- content_type = response['Content-Type']
88
-
89
- content_type && content_type.split(';').first == 'application/json'
90
- end
91
-
92
- def request_uri(path, params)
93
- return path if params.empty?
94
-
95
- path + '?' + params.flat_map { |k, vs| Array(vs).map { |v| "#{escape(k)}=#{escape(v)}" } }.join('&')
96
- end
97
-
98
69
  def escape(component)
99
- CGI.escape(component.to_s)
70
+ Params.escape(component)
100
71
  end
101
72
  end
102
73
  end
@@ -2,15 +2,15 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_domains(params = {})
6
- get('/v3/domains', params)
5
+ def get_domains(params = PARAMS)
6
+ get('/v3/domains', query: params)
7
7
  end
8
8
 
9
9
  def get_domain(name)
10
10
  get("/v3/domains/#{escape name}")
11
11
  end
12
12
 
13
- def add_domain(attributes = {})
13
+ def add_domain(attributes = ATTRIBUTES)
14
14
  post('/v3/domains', attributes)
15
15
  end
16
16
 
@@ -41,5 +41,21 @@ module Mailgunner
41
41
  def update_connection_settings(attributes)
42
42
  put("/v3/domains/#{escape @domain}/connection", attributes)
43
43
  end
44
+
45
+ def get_tracking_settings
46
+ get("/v3/domains/#{escape @domain}/tracking")
47
+ end
48
+
49
+ def update_open_tracking_settings(params = PARAMS)
50
+ put("/v3/domains/#{escape @domain}/tracking/open", params)
51
+ end
52
+
53
+ def update_click_tracking_settings(params = PARAMS)
54
+ put("/v3/domains/#{escape @domain}/tracking/click", params)
55
+ end
56
+
57
+ def update_unsubscribe_tracking_settings(params = PARAMS)
58
+ put("/v3/domains/#{escape @domain}/tracking/unsubscribe", params)
59
+ end
44
60
  end
45
61
  end
@@ -3,11 +3,23 @@
3
3
  module Mailgunner
4
4
  class Client
5
5
  def validate_address(value)
6
- get('/v3/address/validate', address: value)
6
+ get('/v4/address/validate', query: {address: value})
7
7
  end
8
8
 
9
- def parse_addresses(values)
10
- get('/v3/address/parse', addresses: Array(values).join(','))
9
+ def get_bulk_validations
10
+ get('/v4/address/validate/bulk')
11
+ end
12
+
13
+ def create_bulk_validation(list_id)
14
+ post("/v4/address/validate/bulk/#{escape list_id}")
15
+ end
16
+
17
+ def get_bulk_validation(list_id)
18
+ get("/v4/address/validate/bulk/#{escape list_id}")
19
+ end
20
+
21
+ def cancel_bulk_validation(list_id)
22
+ delete("/v4/address/validate/bulk/#{escape list_id}")
11
23
  end
12
24
  end
13
25
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_events(params = {})
6
- get("/v3/#{escape @domain}/events", params)
5
+ def get_events(params = PARAMS)
6
+ get("/v3/#{escape @domain}/events", query: params)
7
7
  end
8
8
  end
9
9
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_all_ips(params = {})
6
- get('/v3/ips', params)
5
+ def get_all_ips(params = PARAMS)
6
+ get('/v3/ips', query: params)
7
7
  end
8
8
 
9
9
  def get_ip(address)
@@ -2,19 +2,19 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_lists(params = {})
6
- get('/v3/lists', params)
5
+ def get_lists(params = PARAMS)
6
+ get('/v3/lists/pages', query: params)
7
7
  end
8
8
 
9
9
  def get_list(address)
10
10
  get("/v3/lists/#{escape address}")
11
11
  end
12
12
 
13
- def add_list(attributes = {})
13
+ def add_list(attributes = ATTRIBUTES)
14
14
  post('/v3/lists', attributes)
15
15
  end
16
16
 
17
- def update_list(address, attributes = {})
17
+ def update_list(address, attributes = ATTRIBUTES)
18
18
  put("/v3/lists/#{escape address}", attributes)
19
19
  end
20
20
 
@@ -22,8 +22,8 @@ module Mailgunner
22
22
  delete("/v3/lists/#{escape address}")
23
23
  end
24
24
 
25
- def get_list_members(list_address, params = {})
26
- get("/v3/lists/#{escape list_address}/members", params)
25
+ def get_list_members(list_address, params = PARAMS)
26
+ get("/v3/lists/#{escape list_address}/members/pages", query: params)
27
27
  end
28
28
 
29
29
  def get_list_member(list_address, member_address)
@@ -7,10 +7,10 @@ module Mailgunner
7
7
  end
8
8
 
9
9
  def get_mime_message(key)
10
- get("/v3/domains/#{escape @domain}/messages/#{escape key}", {}, {'Accept' => 'message/rfc2822'})
10
+ get("/v3/domains/#{escape @domain}/messages/#{escape key}", headers: {'Accept' => 'message/rfc2822'})
11
11
  end
12
12
 
13
- def send_message(attributes = {})
13
+ def send_message(attributes = ATTRIBUTES)
14
14
  post("/v3/#{escape @domain}/messages", attributes)
15
15
  end
16
16
 
@@ -2,19 +2,19 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_routes(params = {})
6
- get('/v3/routes', params)
5
+ def get_routes(params = PARAMS)
6
+ get('/v3/routes', query: params)
7
7
  end
8
8
 
9
9
  def get_route(id)
10
10
  get("/v3/routes/#{escape id}")
11
11
  end
12
12
 
13
- def add_route(attributes = {})
13
+ def add_route(attributes = ATTRIBUTES)
14
14
  post('/v3/routes', attributes)
15
15
  end
16
16
 
17
- def update_route(id, attributes = {})
17
+ def update_route(id, attributes = ATTRIBUTES)
18
18
  put("/v3/routes/#{escape id}", attributes)
19
19
  end
20
20
 
@@ -2,14 +2,8 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_stats(params = {})
6
- Kernel.warn 'Mailgunner::Client#get_stats is deprecated'
7
-
8
- get("/v3/#{escape @domain}/stats", params)
9
- end
10
-
11
- def get_total_stats(params = {})
12
- get("/v3/#{escape @domain}/stats/total", params)
5
+ def get_total_stats(params = PARAMS)
6
+ get("/v3/#{escape @domain}/stats/total", query: params)
13
7
  end
14
8
  end
15
9
  end
@@ -2,15 +2,15 @@
2
2
 
3
3
  module Mailgunner
4
4
  class Client
5
- def get_bounces(params = {})
6
- get("/v3/#{escape @domain}/bounces", params)
5
+ def get_bounces(params = PARAMS)
6
+ get("/v3/#{escape @domain}/bounces", query: params)
7
7
  end
8
8
 
9
9
  def get_bounce(address)
10
10
  get("/v3/#{escape @domain}/bounces/#{escape address}")
11
11
  end
12
12
 
13
- def add_bounce(attributes = {})
13
+ def add_bounce(attributes = ATTRIBUTES)
14
14
  post("/v3/#{escape @domain}/bounces", attributes)
15
15
  end
16
16
 
@@ -22,8 +22,8 @@ module Mailgunner
22
22
  delete("/v3/#{escape @domain}/bounces")
23
23
  end
24
24
 
25
- def get_unsubscribes(params = {})
26
- get("/v3/#{escape @domain}/unsubscribes", params)
25
+ def get_unsubscribes(params = PARAMS)
26
+ get("/v3/#{escape @domain}/unsubscribes", query: params)
27
27
  end
28
28
 
29
29
  def get_unsubscribe(address)
@@ -34,24 +34,40 @@ module Mailgunner
34
34
  delete("/v3/#{escape @domain}/unsubscribes/#{escape address_or_id}")
35
35
  end
36
36
 
37
- def add_unsubscribe(attributes = {})
37
+ def add_unsubscribe(attributes = ATTRIBUTES)
38
38
  post("/v3/#{escape @domain}/unsubscribes", attributes)
39
39
  end
40
40
 
41
- def get_complaints(params = {})
42
- get("/v3/#{escape @domain}/complaints", params)
41
+ def get_complaints(params = PARAMS)
42
+ get("/v3/#{escape @domain}/complaints", query: params)
43
43
  end
44
44
 
45
45
  def get_complaint(address)
46
46
  get("/v3/#{escape @domain}/complaints/#{escape address}")
47
47
  end
48
48
 
49
- def add_complaint(attributes = {})
49
+ def add_complaint(attributes = ATTRIBUTES)
50
50
  post("/v3/#{escape @domain}/complaints", attributes)
51
51
  end
52
52
 
53
53
  def delete_complaint(address)
54
54
  delete("/v3/#{escape @domain}/complaints/#{escape address}")
55
55
  end
56
+
57
+ def get_whitelists(params = PARAMS)
58
+ get("/v3/#{escape @domain}/whitelists", params)
59
+ end
60
+
61
+ def get_whitelist(address_or_domain)
62
+ get("/v3/#{escape @domain}/whitelists/#{escape address_or_domain}")
63
+ end
64
+
65
+ def add_whitelist(attributes = ATTRIBUTES)
66
+ post("/v3/#{escape @domain}/whitelists", attributes)
67
+ end
68
+
69
+ def delete_whitelist(address_or_domain)
70
+ delete("/v3/#{escape @domain}/whitelists/#{escape address_or_domain}")
71
+ end
56
72
  end
57
73
  end