mailgunner 2.5.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require 'net/http'
3
+ require 'json'
4
+
5
+ module Mailgunner
6
+ class Client
7
+ def initialize(domain: nil, api_key: nil, api_host: nil)
8
+ @domain = domain || Mailgunner.config.domain
9
+
10
+ @api_key = api_key || Mailgunner.config.api_key
11
+
12
+ @api_host = api_host || Mailgunner.config.api_host
13
+
14
+ @http = Net::HTTP.new(@api_host, Net::HTTP.https_default_port)
15
+
16
+ @http.use_ssl = true
17
+ end
18
+
19
+ private
20
+
21
+ ATTRIBUTES = PARAMS = HEADERS = {}.freeze
22
+
23
+ def get(path, query: PARAMS, headers: HEADERS)
24
+ uri = URI(path)
25
+ uri.query = Params.encode(query) unless query.empty?
26
+
27
+ request = Net::HTTP::Get.new(uri.to_s)
28
+
29
+ headers.each { |k, v| request[k] = v }
30
+
31
+ transmit(request)
32
+ end
33
+
34
+ def post(path, attributes = ATTRIBUTES)
35
+ transmit(Net::HTTP::Post.new(path)) { |message| message.set_form_data(attributes) }
36
+ end
37
+
38
+ def multipart_post(path, data)
39
+ transmit(Net::HTTP::Post.new(path)) { |message| message.set_form(data, 'multipart/form-data') }
40
+ end
41
+
42
+ def put(path, attributes = ATTRIBUTES)
43
+ transmit(Net::HTTP::Put.new(path)) { |message| message.set_form_data(attributes) }
44
+ end
45
+
46
+ def delete(path)
47
+ transmit(Net::HTTP::Delete.new(path))
48
+ end
49
+
50
+ def transmit(message)
51
+ message.basic_auth('api', @api_key)
52
+ message['User-Agent'] = Mailgunner.config.user_agent
53
+
54
+ yield message if block_given?
55
+
56
+ response = @http.request(message)
57
+
58
+ unless response.is_a?(Net::HTTPSuccess)
59
+ raise Error.parse(response)
60
+ end
61
+
62
+ if response['Content-Type']&.start_with?('application/json')
63
+ return JSON.parse(response.body, object_class: Struct)
64
+ end
65
+
66
+ response.body
67
+ end
68
+
69
+ def escape(component)
70
+ Params.escape(component)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_domains(params = PARAMS)
6
+ get('/v3/domains', query: params)
7
+ end
8
+
9
+ def get_domain(name)
10
+ get("/v3/domains/#{escape name}")
11
+ end
12
+
13
+ def add_domain(attributes = ATTRIBUTES)
14
+ post('/v3/domains', attributes)
15
+ end
16
+
17
+ def delete_domain(name)
18
+ delete("/v3/domains/#{escape name}")
19
+ end
20
+
21
+ def verify_domain(name)
22
+ put("/v3/domains/#{escape name}/verify")
23
+ end
24
+
25
+ def get_credentials
26
+ get("/v3/domains/#{escape @domain}/credentials")
27
+ end
28
+
29
+ def add_credentials(attributes)
30
+ post("/v3/domains/#{escape @domain}/credentials", attributes)
31
+ end
32
+
33
+ def update_credentials(login, attributes)
34
+ put("/v3/domains/#{escape @domain}/credentials/#{escape login}", attributes)
35
+ end
36
+
37
+ def delete_credentials(login)
38
+ delete("/v3/domains/#{escape @domain}/credentials/#{escape login}")
39
+ end
40
+
41
+ def get_connection_settings
42
+ get("/v3/domains/#{escape @domain}/connection")
43
+ end
44
+
45
+ def update_connection_settings(attributes)
46
+ put("/v3/domains/#{escape @domain}/connection", attributes)
47
+ end
48
+
49
+ def get_tracking_settings
50
+ get("/v3/domains/#{escape @domain}/tracking")
51
+ end
52
+
53
+ def update_open_tracking_settings(params = PARAMS)
54
+ put("/v3/domains/#{escape @domain}/tracking/open", params)
55
+ end
56
+
57
+ def update_click_tracking_settings(params = PARAMS)
58
+ put("/v3/domains/#{escape @domain}/tracking/click", params)
59
+ end
60
+
61
+ def update_unsubscribe_tracking_settings(params = PARAMS)
62
+ put("/v3/domains/#{escape @domain}/tracking/unsubscribe", params)
63
+ end
64
+
65
+ def update_dkim_authority(params = PARAMS)
66
+ put("/v3/domains/#{escape @domain}/dkim_authority", params)
67
+ end
68
+
69
+ def update_dkim_selector(params = PARAMS)
70
+ put("/v3/domains/#{escape @domain}/dkim_selector", params)
71
+ end
72
+
73
+ def update_web_prefix(params = PARAMS)
74
+ put("/v3/domains/#{escape @domain}/web_prefix", params)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def validate_address(value)
6
+ get('/v4/address/validate', query: {address: value})
7
+ end
8
+
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}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_events(params = PARAMS)
6
+ get("/v3/#{escape @domain}/events", query: params)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_all_ips(params = PARAMS)
6
+ get('/v3/ips', query: params)
7
+ end
8
+
9
+ def get_ip(address)
10
+ get("/v3/ips/#{escape address}")
11
+ end
12
+
13
+ def get_ips
14
+ get("/v3/domains/#{escape @domain}/ips")
15
+ end
16
+
17
+ def add_ip(address)
18
+ post("/v3/domains/#{escape @domain}/ips", ip: address)
19
+ end
20
+
21
+ def delete_ip(address)
22
+ delete("/v3/domains/#{escape @domain}/ips/#{escape address}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_lists(params = PARAMS)
6
+ get('/v3/lists/pages', query: params)
7
+ end
8
+
9
+ def get_list(address)
10
+ get("/v3/lists/#{escape address}")
11
+ end
12
+
13
+ def add_list(attributes = ATTRIBUTES)
14
+ post('/v3/lists', attributes)
15
+ end
16
+
17
+ def update_list(address, attributes = ATTRIBUTES)
18
+ put("/v3/lists/#{escape address}", attributes)
19
+ end
20
+
21
+ def delete_list(address)
22
+ delete("/v3/lists/#{escape address}")
23
+ end
24
+
25
+ def get_list_members(list_address, params = PARAMS)
26
+ get("/v3/lists/#{escape list_address}/members/pages", query: params)
27
+ end
28
+
29
+ def get_list_member(list_address, member_address)
30
+ get("/v3/lists/#{escape list_address}/members/#{escape member_address}")
31
+ end
32
+
33
+ def add_list_member(list_address, member_attributes)
34
+ post("/v3/lists/#{escape list_address}/members", member_attributes)
35
+ end
36
+
37
+ def update_list_member(list_address, member_address, member_attributes)
38
+ put("/v3/lists/#{escape list_address}/members/#{escape member_address}", member_attributes)
39
+ end
40
+
41
+ def delete_list_member(list_address, member_address)
42
+ delete("/v3/lists/#{escape list_address}/members/#{escape member_address}")
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_message(key)
6
+ get("/v3/domains/#{escape @domain}/messages/#{escape key}")
7
+ end
8
+
9
+ def get_mime_message(key)
10
+ get("/v3/domains/#{escape @domain}/messages/#{escape key}", headers: {'Accept' => 'message/rfc2822'})
11
+ end
12
+
13
+ def send_message(attributes = ATTRIBUTES)
14
+ post("/v3/#{escape @domain}/messages", attributes)
15
+ end
16
+
17
+ def send_mime(mail)
18
+ to = ['to', Array(mail.destinations).join(',')]
19
+
20
+ message = ['message', mail.encoded, {filename: 'message.mime'}]
21
+
22
+ multipart_post("/v3/#{escape @domain}/messages.mime", [to, message])
23
+ end
24
+
25
+ def delete_message(key)
26
+ delete("/v3/domains/#{escape @domain}/messages/#{escape key}")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_routes(params = PARAMS)
6
+ get('/v3/routes', query: params)
7
+ end
8
+
9
+ def get_route(id)
10
+ get("/v3/routes/#{escape id}")
11
+ end
12
+
13
+ def add_route(attributes = ATTRIBUTES)
14
+ post('/v3/routes', attributes)
15
+ end
16
+
17
+ def update_route(id, attributes = ATTRIBUTES)
18
+ put("/v3/routes/#{escape id}", attributes)
19
+ end
20
+
21
+ def delete_route(id)
22
+ delete("/v3/routes/#{escape id}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_total_stats(params = PARAMS)
6
+ get("/v3/#{escape @domain}/stats/total", query: params)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_bounces(params = PARAMS)
6
+ get("/v3/#{escape @domain}/bounces", query: params)
7
+ end
8
+
9
+ def get_bounce(address)
10
+ get("/v3/#{escape @domain}/bounces/#{escape address}")
11
+ end
12
+
13
+ def add_bounce(attributes = ATTRIBUTES)
14
+ post("/v3/#{escape @domain}/bounces", attributes)
15
+ end
16
+
17
+ def delete_bounce(address)
18
+ delete("/v3/#{escape @domain}/bounces/#{escape address}")
19
+ end
20
+
21
+ def delete_bounces
22
+ delete("/v3/#{escape @domain}/bounces")
23
+ end
24
+
25
+ def get_unsubscribes(params = PARAMS)
26
+ get("/v3/#{escape @domain}/unsubscribes", query: params)
27
+ end
28
+
29
+ def get_unsubscribe(address)
30
+ get("/v3/#{escape @domain}/unsubscribes/#{escape address}")
31
+ end
32
+
33
+ def delete_unsubscribe(address_or_id)
34
+ delete("/v3/#{escape @domain}/unsubscribes/#{escape address_or_id}")
35
+ end
36
+
37
+ def add_unsubscribe(attributes = ATTRIBUTES)
38
+ post("/v3/#{escape @domain}/unsubscribes", attributes)
39
+ end
40
+
41
+ def get_complaints(params = PARAMS)
42
+ get("/v3/#{escape @domain}/complaints", query: params)
43
+ end
44
+
45
+ def get_complaint(address)
46
+ get("/v3/#{escape @domain}/complaints/#{escape address}")
47
+ end
48
+
49
+ def add_complaint(attributes = ATTRIBUTES)
50
+ post("/v3/#{escape @domain}/complaints", attributes)
51
+ end
52
+
53
+ def delete_complaint(address)
54
+ delete("/v3/#{escape @domain}/complaints/#{escape address}")
55
+ end
56
+
57
+ def get_whitelists(params = PARAMS)
58
+ get("/v3/#{escape @domain}/whitelists", query: 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
72
+ end
73
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_tags(params = PARAMS)
6
+ get("/v3/#{escape @domain}/tags", query: params)
7
+ end
8
+
9
+ def get_tag(id)
10
+ get("/v3/#{escape @domain}/tags/#{escape id}")
11
+ end
12
+
13
+ def update_tag(id, attributes)
14
+ put("/v3/#{escape @domain}/tags/#{escape id}", attributes)
15
+ end
16
+
17
+ def get_tag_stats(id, params)
18
+ get("/v3/#{escape @domain}/tags/#{escape id}/stats", query: params)
19
+ end
20
+
21
+ def delete_tag(id)
22
+ delete("/v3/#{escape @domain}/tags/#{escape id}")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailgunner
4
+ class Client
5
+ def get_webhooks
6
+ get("/v3/domains/#{escape @domain}/webhooks")
7
+ end
8
+
9
+ def get_webhook(id)
10
+ get("/v3/domains/#{escape @domain}/webhooks/#{escape id}")
11
+ end
12
+
13
+ def add_webhook(attributes = ATTRIBUTES)
14
+ post("/v3/domains/#{escape @domain}/webhooks", attributes)
15
+ end
16
+
17
+ def update_webhook(id, attributes = ATTRIBUTES)
18
+ put("/v3/domains/#{escape @domain}/webhooks/#{escape id}", attributes)
19
+ end
20
+
21
+ def delete_webhook(id)
22
+ delete("/v3/domains/#{escape @domain}/webhooks/#{escape id}")
23
+ end
24
+ end
25
+ end