mailerlite-ruby 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.env.example +1 -0
  3. data/.github/workflows/main.yml +17 -0
  4. data/.github/workflows/publish_gem.yml +21 -0
  5. data/.gitignore +60 -0
  6. data/.rspec +1 -0
  7. data/.rubocop.yml +36 -0
  8. data/CHANGELOG.md +1 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +801 -0
  12. data/Rakefile +8 -0
  13. data/bin/console +8 -0
  14. data/bin/setup +8 -0
  15. data/fixtures/automations/fetch.yml +61 -0
  16. data/fixtures/automations/get.yml +68 -0
  17. data/fixtures/automations/get_subscriber_activity.yml +71 -0
  18. data/fixtures/batch/request.yml +59 -0
  19. data/fixtures/campaigns/activity.yml +64 -0
  20. data/fixtures/campaigns/create.yml +65 -0
  21. data/fixtures/campaigns/delete.yml +53 -0
  22. data/fixtures/campaigns/fetch.yml +64 -0
  23. data/fixtures/campaigns/get.yml +68 -0
  24. data/fixtures/campaigns/languages.yml +60 -0
  25. data/fixtures/campaigns/schedule.yml +160 -0
  26. data/fixtures/campaigns/update.yml +159 -0
  27. data/fixtures/fields/create.yml +57 -0
  28. data/fixtures/fields/delete.yml +53 -0
  29. data/fixtures/fields/get.yml +60 -0
  30. data/fixtures/fields/update.yml +57 -0
  31. data/fixtures/forms/delete.yml +53 -0
  32. data/fixtures/forms/fetch.yml +60 -0
  33. data/fixtures/forms/fetch_subscribers.yml +59 -0
  34. data/fixtures/forms/list.yml +62 -0
  35. data/fixtures/forms/update.yml +60 -0
  36. data/fixtures/groups/assign_subscriber.yml +58 -0
  37. data/fixtures/groups/create.yml +58 -0
  38. data/fixtures/groups/delete.yml +53 -0
  39. data/fixtures/groups/get.yml +63 -0
  40. data/fixtures/groups/get_subscribers.yml +62 -0
  41. data/fixtures/groups/unassign_subscriber.yml +53 -0
  42. data/fixtures/groups/update.yml +58 -0
  43. data/fixtures/segments/delete.yml +53 -0
  44. data/fixtures/segments/get_subscribers.yml +61 -0
  45. data/fixtures/segments/list.yml +60 -0
  46. data/fixtures/segments/update.yml +58 -0
  47. data/fixtures/subscribers/create.yml +57 -0
  48. data/fixtures/subscribers/delete.yml +51 -0
  49. data/fixtures/subscribers/fetch.yml +68 -0
  50. data/fixtures/subscribers/fetch_count.yml +55 -0
  51. data/fixtures/subscribers/get.yml +62 -0
  52. data/fixtures/timezones/list.yml +575 -0
  53. data/fixtures/webhooks/create.yml +58 -0
  54. data/fixtures/webhooks/delete.yml +53 -0
  55. data/fixtures/webhooks/get.yml +58 -0
  56. data/fixtures/webhooks/list.yml +61 -0
  57. data/fixtures/webhooks/update.yml +58 -0
  58. data/lib/mailerlite/automations/automations.rb +62 -0
  59. data/lib/mailerlite/batch/batch.rb +24 -0
  60. data/lib/mailerlite/campaigns/campaigns.rb +216 -0
  61. data/lib/mailerlite/client.rb +35 -0
  62. data/lib/mailerlite/fields/fields.rb +61 -0
  63. data/lib/mailerlite/forms/forms.rb +73 -0
  64. data/lib/mailerlite/groups/groups.rb +90 -0
  65. data/lib/mailerlite/segments/segments.rb +62 -0
  66. data/lib/mailerlite/subscribers/subscribers.rb +116 -0
  67. data/lib/mailerlite/timezones/timezones.rb +22 -0
  68. data/lib/mailerlite/version.rb +5 -0
  69. data/lib/mailerlite/webhooks/webhooks.rb +67 -0
  70. data/lib/mailerlite-ruby.rb +3 -0
  71. data/lib/mailerlite.rb +13 -0
  72. data/mailerlite-ruby.gemspec +42 -0
  73. data/renovate.json +5 -0
  74. data/spec/automations_rspec.rb +63 -0
  75. data/spec/batches_rspec.rb +41 -0
  76. data/spec/campaigns_rspec.rb +155 -0
  77. data/spec/fields_rspec.rb +70 -0
  78. data/spec/forms_rspec.rb +81 -0
  79. data/spec/groups_rspec.rb +97 -0
  80. data/spec/segments_rspec.rb +70 -0
  81. data/spec/spec_helper.rb +101 -0
  82. data/spec/subscribers_rspec.rb +84 -0
  83. data/spec/timezones_rspec.rb +36 -0
  84. data/spec/webhooks_rspec.rb +86 -0
  85. metadata +303 -0
@@ -0,0 +1,58 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://connect.mailerlite.com/api/webhooks
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"events":["subscriber.created"],"url":"http://foobar.hook","name":"test_webhook"}'
9
+ headers:
10
+ Authorization:
11
+ - "<AUTH>"
12
+ User-Agent:
13
+ - MailerLite-client-ruby/0.1.1
14
+ Accept:
15
+ - application/json
16
+ Content-Type:
17
+ - application/json
18
+ Connection:
19
+ - close
20
+ Host:
21
+ - connect.mailerlite.com
22
+ response:
23
+ status:
24
+ code: 201
25
+ message: Created
26
+ headers:
27
+ Date:
28
+ - Fri, 23 Dec 2022 21:51:54 GMT
29
+ Content-Type:
30
+ - application/json
31
+ Transfer-Encoding:
32
+ - chunked
33
+ Connection:
34
+ - close
35
+ Cache-Control:
36
+ - no-cache, private
37
+ X-Locale:
38
+ - en
39
+ X-Ratelimit-Limit:
40
+ - '120'
41
+ X-Ratelimit-Remaining:
42
+ - '117'
43
+ Access-Control-Allow-Origin:
44
+ - "*"
45
+ Strict-Transport-Security:
46
+ - max-age=15724800; includeSubDomains
47
+ Cf-Cache-Status:
48
+ - DYNAMIC
49
+ Server:
50
+ - cloudflare
51
+ Cf-Ray:
52
+ - 77e45238dbecde53-MCT
53
+ body:
54
+ encoding: UTF-8
55
+ string: '{"data":{"id":"75321640600209302","name":"test_webhook","url":"http:\/\/foobar.hook","events":["subscriber.created"],"enabled":true,"secret":"HigkdhMVbp","created_at":"2022-12-23
56
+ 21:51:54","updated_at":"2022-12-23 21:51:54"}}'
57
+ recorded_at: Fri, 23 Dec 2022 21:51:53 GMT
58
+ recorded_with: VCR 6.1.0
@@ -0,0 +1,53 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: delete
5
+ uri: https://connect.mailerlite.com/api/webhooks/75321640600209302
6
+ body:
7
+ encoding: UTF-8
8
+ string: ''
9
+ headers:
10
+ Authorization:
11
+ - "<AUTH>"
12
+ User-Agent:
13
+ - MailerLite-client-ruby/0.1.1
14
+ Accept:
15
+ - application/json
16
+ Content-Type:
17
+ - application/json
18
+ Connection:
19
+ - close
20
+ Host:
21
+ - connect.mailerlite.com
22
+ response:
23
+ status:
24
+ code: 204
25
+ message: No Content
26
+ headers:
27
+ Date:
28
+ - Fri, 23 Dec 2022 21:52:46 GMT
29
+ Connection:
30
+ - close
31
+ Cache-Control:
32
+ - no-cache, private
33
+ X-Locale:
34
+ - en
35
+ X-Ratelimit-Limit:
36
+ - '120'
37
+ X-Ratelimit-Remaining:
38
+ - '119'
39
+ Access-Control-Allow-Origin:
40
+ - "*"
41
+ Strict-Transport-Security:
42
+ - max-age=15724800; includeSubDomains
43
+ Cf-Cache-Status:
44
+ - DYNAMIC
45
+ Server:
46
+ - cloudflare
47
+ Cf-Ray:
48
+ - 77e453821df6de47-MCT
49
+ body:
50
+ encoding: ASCII-8BIT
51
+ string: ''
52
+ recorded_at: Fri, 23 Dec 2022 21:52:45 GMT
53
+ recorded_with: VCR 6.1.0
@@ -0,0 +1,58 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://connect.mailerlite.com/api/webhooks/75321551702984317
6
+ body:
7
+ encoding: UTF-8
8
+ string: ''
9
+ headers:
10
+ Authorization:
11
+ - "<AUTH>"
12
+ User-Agent:
13
+ - MailerLite-client-ruby/0.1.1
14
+ Accept:
15
+ - application/json
16
+ Content-Type:
17
+ - application/json
18
+ Connection:
19
+ - close
20
+ Host:
21
+ - connect.mailerlite.com
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Date:
28
+ - Fri, 23 Dec 2022 21:51:22 GMT
29
+ Content-Type:
30
+ - application/json
31
+ Transfer-Encoding:
32
+ - chunked
33
+ Connection:
34
+ - close
35
+ Cache-Control:
36
+ - no-cache, private
37
+ X-Locale:
38
+ - en
39
+ X-Ratelimit-Limit:
40
+ - '120'
41
+ X-Ratelimit-Remaining:
42
+ - '119'
43
+ Access-Control-Allow-Origin:
44
+ - "*"
45
+ Strict-Transport-Security:
46
+ - max-age=15724800; includeSubDomains
47
+ Cf-Cache-Status:
48
+ - DYNAMIC
49
+ Server:
50
+ - cloudflare
51
+ Cf-Ray:
52
+ - 77e45174ed1fd1f0-MCT
53
+ body:
54
+ encoding: UTF-8
55
+ string: '{"data":{"id":"75321551702984317","name":"test_webhook","url":"http:\/\/foobar.hook","events":["subscriber.created"],"enabled":true,"secret":"pv04URxAE4","created_at":"2022-12-23
56
+ 21:50:29","updated_at":"2022-12-23 21:50:29"}}'
57
+ recorded_at: Fri, 23 Dec 2022 21:51:21 GMT
58
+ recorded_with: VCR 6.1.0
@@ -0,0 +1,61 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://connect.mailerlite.com/api/webhooks
6
+ body:
7
+ encoding: UTF-8
8
+ string: ''
9
+ headers:
10
+ Authorization:
11
+ - "<AUTH>"
12
+ User-Agent:
13
+ - MailerLite-client-ruby/0.1.1
14
+ Accept:
15
+ - application/json
16
+ Content-Type:
17
+ - application/json
18
+ Connection:
19
+ - close
20
+ Host:
21
+ - connect.mailerlite.com
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Date:
28
+ - Fri, 23 Dec 2022 21:52:21 GMT
29
+ Content-Type:
30
+ - application/json
31
+ Transfer-Encoding:
32
+ - chunked
33
+ Connection:
34
+ - close
35
+ Cache-Control:
36
+ - no-cache, private
37
+ X-Locale:
38
+ - en
39
+ X-Ratelimit-Limit:
40
+ - '120'
41
+ X-Ratelimit-Remaining:
42
+ - '116'
43
+ Access-Control-Allow-Origin:
44
+ - "*"
45
+ Strict-Transport-Security:
46
+ - max-age=15724800; includeSubDomains
47
+ Cf-Cache-Status:
48
+ - DYNAMIC
49
+ Server:
50
+ - cloudflare
51
+ Cf-Ray:
52
+ - 77e452e6ae74de4f-MCT
53
+ body:
54
+ encoding: UTF-8
55
+ string: '{"data":[{"id":"75321640600209302","name":"test_webhook","url":"http:\/\/foobar.hook","events":["subscriber.created"],"enabled":true,"secret":"HigkdhMVbp","created_at":"2022-12-23
56
+ 21:51:54","updated_at":"2022-12-23 21:51:54"},{"id":"75321551702984317","name":"test_webhook","url":"http:\/\/foobar.hook","events":["subscriber.created"],"enabled":true,"secret":"pv04URxAE4","created_at":"2022-12-23
57
+ 21:50:29","updated_at":"2022-12-23 21:50:29"}],"links":{"first":"https:\/\/connect.mailerlite.com\/api\/webhooks?page=1","last":"https:\/\/connect.mailerlite.com\/api\/webhooks?page=1","prev":null,"next":null},"meta":{"current_page":1,"from":1,"last_page":1,"links":[{"url":null,"label":"&laquo;
58
+ Previous","active":false},{"url":"https:\/\/connect.mailerlite.com\/api\/webhooks?page=1","label":"1","active":true},{"url":null,"label":"Next
59
+ &raquo;","active":false}],"path":"https:\/\/connect.mailerlite.com\/api\/webhooks","per_page":50,"to":2,"total":2}}'
60
+ recorded_at: Fri, 23 Dec 2022 21:52:20 GMT
61
+ recorded_with: VCR 6.1.0
@@ -0,0 +1,58 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: put
5
+ uri: https://connect.mailerlite.com/api/webhooks/75233700096247795
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"name":"test_webhook2"}'
9
+ headers:
10
+ Authorization:
11
+ - "<AUTH>"
12
+ User-Agent:
13
+ - MailerLite-client-ruby/0.1.1
14
+ Accept:
15
+ - application/json
16
+ Content-Type:
17
+ - application/json
18
+ Connection:
19
+ - close
20
+ Host:
21
+ - connect.mailerlite.com
22
+ response:
23
+ status:
24
+ code: 200
25
+ message: OK
26
+ headers:
27
+ Date:
28
+ - Thu, 22 Dec 2022 22:35:04 GMT
29
+ Content-Type:
30
+ - application/json
31
+ Transfer-Encoding:
32
+ - chunked
33
+ Connection:
34
+ - close
35
+ Cache-Control:
36
+ - no-cache, private
37
+ X-Locale:
38
+ - en
39
+ X-Ratelimit-Limit:
40
+ - '120'
41
+ X-Ratelimit-Remaining:
42
+ - '117'
43
+ Access-Control-Allow-Origin:
44
+ - "*"
45
+ Strict-Transport-Security:
46
+ - max-age=15724800; includeSubDomains
47
+ Cf-Cache-Status:
48
+ - DYNAMIC
49
+ Server:
50
+ - cloudflare
51
+ Cf-Ray:
52
+ - 77dc54153eb8d1d8-MCT
53
+ body:
54
+ encoding: UTF-8
55
+ string: '{"data":{"id":"75233700096247795","name":"test_webhook2","url":"http:\/\/foobar.hook","events":["subscriber.created"],"enabled":true,"secret":"Hym6hUN1bO","created_at":"2022-12-22
56
+ 22:34:07","updated_at":"2022-12-22 22:35:04"}}'
57
+ recorded_at: Thu, 22 Dec 2022 22:35:03 GMT
58
+ recorded_with: VCR 6.1.0
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Automation from MailerLite API.
5
+ class Automations
6
+ attr_reader :client
7
+
8
+ # Inits the `Automations` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns a list of Automations that match the specified filter criteria.
16
+ #
17
+ # @param filter[:status,:name,:group] [Array] filters for automation
18
+ # @param limit [Integer] the maximum number of Automations to return
19
+ # @param page [Integer] the page number of the results to return
20
+ # @return [HTTP::Response] the response from the API
21
+ def get(limit: nil, page: nil, filter: {})
22
+ params = {}
23
+ params['filter[status]'] = filter[:status] if filter.key?(:status)
24
+ params['filter[name]'] = filter[:name] if filter.key?(:name)
25
+ params['filter[group]'] = filter[:group] if filter.key?(:group)
26
+ params['limit'] = limit if limit
27
+ params['page'] = page if page
28
+ uri = URI("#{API_URL}/automations")
29
+ uri.query = URI.encode_www_form(params.compact)
30
+ client.http.get(uri)
31
+ end
32
+
33
+ # fetch the specified Automation
34
+ #
35
+ # @param automation_id [String] the ID of the Automation to fetch
36
+ # @return [HTTP::Response] the response from the API
37
+ def fetch(automation_id)
38
+ client.http.get("#{API_URL}/automations/#{automation_id}")
39
+ end
40
+
41
+ # get_subscriber_activity the subscriber activity for specified Automation
42
+ #
43
+ # @param automation_id [Integer] the ID of the Automation to get_subscriber_activity
44
+ # @param filter[:status,:date_from,:date_to,:scheduled_from,:scheduled_to,:keyword] [Array] Must be one of the following: completed, active, canceled, failed
45
+ # @param limit [Integer] the maximum number of Automations to return
46
+ # @param page [Integer] the page number of the results to return
47
+ # @return [HTTP::Response] the response from the API
48
+ def get_subscriber_activity(automation_id:, filter: {}, page: nil, limit: nil)
49
+ params = { 'filter[status]' => filter[:status] }
50
+ params['filter[date_from]'] = filter[:date_from] if filter.key?(:date_from)
51
+ params['filter[date_to]'] = filter[:date_to] if filter.key?(:date_to)
52
+ params['filter[scheduled_from]'] = filter[:scheduled_from] if filter.key?(:scheduled_from)
53
+ params['filter[scheduled_to]'] = filter[:scheduled_to] if filter.key?(:scheduled_to)
54
+ params['filter[keyword]'] = filter[:keyword] if filter.key?(:keyword)
55
+ params['page'] = page if page
56
+ params['limit'] = limit if limit
57
+ uri = URI("#{API_URL}/automations/#{automation_id}/activity")
58
+ uri.query = URI.encode_www_form(params.compact)
59
+ client.http.get(uri)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Batch from MailerLite API.
5
+ class Batch
6
+ attr_reader :client
7
+
8
+ # Inits the `Batch` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Create a Batch Request
16
+ #
17
+ # @param requests [Array] Array of objects containing required method and path properties and optional body
18
+ # @return [HTTP::Response] the response from the API
19
+ def request(requests:)
20
+ params = { requests: requests }
21
+ client.http.post("#{API_URL}/batch", body: params.to_json)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,216 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Campaigns from MailerLite API.
5
+ class Campaigns
6
+ attr_reader :client
7
+
8
+ # Inits the `Campaigns` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns a list of Campaigns that match the specified filter criteria.
16
+ #
17
+ # @param filter[:status,:type] [String] the status of the Campaigns to include in the results, must be one of [sent, draft, ready], Defaults to ready.
18
+ # The filter type of the Campaigns to include in the results, must be one of [regular, ab, resend, rss], Defaults to all
19
+ # @param limit [Integer] the maximum number of Campaigns to return
20
+ # @param page [Integer] the page number of the results to return
21
+ # @return [HTTP::Response] the response from the API
22
+ def get(filter:, limit: nil, page: nil)
23
+ params = { 'filter[status]' => filter[:status] }
24
+ params['filter[type]'] = filter[:type] if filter.key?(:type)
25
+ params['limit'] = limit if limit
26
+ params['page'] = page if page
27
+ uri = URI("#{API_URL}/campaigns")
28
+ uri.query = URI.encode_www_form(params.compact)
29
+ client.http.get(uri)
30
+ end
31
+
32
+ # Creates a new campaign with the specified details.
33
+ #
34
+ # @param name [String] Maximum string length of 255 characters
35
+ # @param language_id [integer] Used to define the language in the unsubscribe template. Must be a valid language id. Defaults to english
36
+ # @param type [String] Must be one of the following: regular, ab, resend. Type resend is only available for accounts on growing or advanced plans
37
+ # @param emails [Array] Must contain 1 email object item
38
+ # @param groups [Array] Must contain valid group ids belonging to the account
39
+ # @param segments [Array] Must contain valid segment ids belonging to the account. If both groups and segments are provided, only segments are used
40
+ # @param ab_settings [Array<Array>] only if type is ab - All items of the array are required
41
+ # @param resend_settings [:test_type,:select_winner_by,:b_value] https://developers.mailerlite.com/docs/campaigns.html#create-a-campaign
42
+ # @return [HTTP::Response] the response from the API
43
+ def create(name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil)
44
+ params = { 'name' => name }
45
+ params['type'] = type
46
+ params['emails'] = emails
47
+ params['language_id'] = language_id if language_id
48
+ params['groups'] = groups if groups
49
+ params['segments'] = segments if segments
50
+ case type
51
+ when 'ab'
52
+ params['ab_settings'] = {
53
+ test_type: ab_settings[:test_type],
54
+ select_winner_by: ab_settings[:select_winner_by],
55
+ after_time_amount: ab_settings[:after_time_amount],
56
+ after_time_unit: ab_settings[:after_time_unit],
57
+ test_split: ab_settings[:test_split]
58
+ }
59
+ case ab_settings[:test_type]
60
+ when 'subject'
61
+ params['ab_settings']['b_value'] = {
62
+ subject: ab_settings[:b_value][:subject]
63
+ }
64
+
65
+ when 'sender'
66
+ params['ab_settings']['b_value'] = {
67
+ from_name: ab_settings[:b_value][:from_name],
68
+ from: ab_settings[:b_value][:from]
69
+ }
70
+ end
71
+ when 'resend'
72
+ params['resend_settings'] = {
73
+ test_type: resend_settings[:test_type],
74
+ select_winner_by: resend_settings[:select_winner_by],
75
+ b_value: {
76
+ subject: resend_settings[:b_value][:subject]
77
+ }
78
+ }
79
+ end
80
+
81
+ client.http.post("#{API_URL}/campaigns", body: params.compact.to_json)
82
+ end
83
+
84
+ # Update a new campaign with the specified details.
85
+ #
86
+ # @param campaign_id [Integer] the ID of the campaign to update
87
+ # @param name [String] Maximum string length of 255 characters
88
+ # @param language_id [integer] Used to define the language in the unsubscribe template. Must be a valid language id. Defaults to english
89
+ # @param type [String] Must be one of the following: regular, ab, resend. Type resend is only available for accounts on growing or advanced plans
90
+ # @param emails [Array<Array>] Must contain 1 email object item
91
+ # @param groups [Array] Must contain valid group ids belonging to the account
92
+ # @param segments [Array] Must contain valid segment ids belonging to the account. If both groups and segments are provided, only segments are used
93
+ # @param ab_settings [Array<Array>] only if type is ab - All items of the array are required
94
+ # @param resend_settings [:test_type,:select_winner_by,:b_value] https://developers.mailerlite.com/docs/campaigns.html#update-campaign
95
+ # @return [HTTP::Response] the response from the API
96
+ def update(campaign_id:, name:, type:, emails:, language_id: nil, groups: nil, segments: nil, ab_settings: nil, resend_settings: nil)
97
+ params = { 'name' => name }
98
+ params['emails'] = emails
99
+ params['language_id'] = language_id if language_id
100
+ params['groups'] = groups if groups
101
+ params['segments'] = segments if segments
102
+ case type
103
+ when 'ab'
104
+ params['ab_settings'] = {
105
+ test_type: ab_settings[:test_type],
106
+ select_winner_by: ab_settings[:select_winner_by],
107
+ after_time_amount: ab_settings[:after_time_amount],
108
+ after_time_unit: ab_settings[:after_time_unit],
109
+ test_split: ab_settings[:test_split]
110
+ }
111
+ case ab_settings[:test_type]
112
+ when 'subject'
113
+ params['ab_settings']['b_value'] = {
114
+ subject: ab_settings[:b_value][:subject]
115
+ }
116
+
117
+ when 'sender'
118
+ params['ab_settings']['b_value'] = {
119
+ from_name: ab_settings[:b_value][:from_name],
120
+ from: ab_settings[:b_value][:from]
121
+ }
122
+
123
+ end
124
+ when 'resend'
125
+ params['resend_settings'] = {
126
+ test_type: resend_settings[:test_type],
127
+ select_winner_by: resend_settings[:select_winner_by],
128
+ b_value: {
129
+ subject: resend_settings[:b_value][:subject]
130
+ }
131
+ }
132
+
133
+ end
134
+
135
+ client.http.put("#{API_URL}/campaigns/#{campaign_id}", body: params.compact.to_json)
136
+ end
137
+
138
+ # Schedules the specified campaign.
139
+ #
140
+ # @param campaign_id [Integer] the ID of the campaign to schedule
141
+ # @param delivery [String] yes unless campaign type is rss Must be one of the following: instant, scheduled, timezone_based
142
+ # @param schedule[:date,:hours,:minutes,:timezone_id] [Array] campaign schedule
143
+ # @param resend[:delivery,:date,:hours,:minutes,:timezone_id] [String] resend settings
144
+ # https://developers.mailerlite.com/docs/campaigns.html#schedule-a-campaign
145
+ # @return [HTTP::Response] the response from the API
146
+ def schedule(campaign_id:, delivery:, schedule: nil, resend: nil)
147
+ params = {}
148
+ params['delivery'] = delivery if delivery
149
+ if %w[scheduled timezone_based].include?(delivery) && schedule
150
+ params['schedule'] = {}
151
+ params['schedule']['date'] = schedule[:date] if (delivery == 'scheduled') && schedule.key?(:date)
152
+ params['schedule']['hours'] = schedule[:hours] if schedule.key?(:hours)
153
+ params['schedule']['minutes'] = schedule[:minutes] if schedule.key?(:minutes)
154
+ params['schedule']['timezone_id'] = schedule[:timezone_id] if schedule.key?(:timezone_id)
155
+ end
156
+ params['resend'] = {} if resend
157
+ params['resend']['delivery'] = resend[:delivery] if resend&.key?(:delivery)
158
+ params['resend']['date'] = resend[:date] if resend&.key?(:date)
159
+ params['resend']['hours'] = resend[:hours] if resend&.key?(:hours)
160
+ params['resend']['minutes'] = resend[:minutes] if resend&.key?(:minutes)
161
+ params['resend']['timezone_id'] = resend[:timezone_id] if resend&.key?(:timezone_id)
162
+
163
+ client.http.post("#{API_URL}/campaigns/#{campaign_id}/schedule", body: params.compact.to_json)
164
+ end
165
+
166
+ # Returns the details of the specified Campaigns
167
+ #
168
+ # @param campaign_id [String] the ID of the campaign to fetch
169
+ # @return [HTTP::Response] the response from the API
170
+ def fetch(campaign_id)
171
+ client.http.get("#{API_URL}/campaigns/#{campaign_id}")
172
+ end
173
+
174
+ # Cancels the specified campaign.
175
+ #
176
+ # @param campaign_id [String] the ID of the campaign to delete
177
+ # @return [HTTP::Response] the response from the API
178
+ def cancel(campaign_id)
179
+ client.http.post("#{API_URL}/campaigns/#{campaign_id}/cancel")
180
+ end
181
+
182
+ # Deletes the specified campaign.
183
+ #
184
+ # @param campaign_id [String] the ID of the campaign to delete
185
+ # @return [HTTP::Response] the response from the API
186
+ def delete(campaign_id)
187
+ client.http.delete("#{API_URL}/campaigns/#{campaign_id}")
188
+ end
189
+
190
+ # activity the subscriber activity for specified campaign
191
+ #
192
+ # @param campaign_id [Integer] the ID of the campaign to activity
193
+ # @param filter[:type,:search] [String] specify filters
194
+ # @param limit [Integer] the maximum number of campaigns to return
195
+ # @param page [Integer] the page number of the results to return
196
+ # https://developers.mailerlite.com/docs/campaigns.html#get-subscribers-activity-of-a-sent-campaign
197
+ # @return [HTTP::Response] the response from the API
198
+ def activity(campaign_id:, filter: nil, page: nil, limit: nil, sort: nil)
199
+ params = {}
200
+ params['filter'] = {} if filter
201
+ params['filter']['type'] = filter[:type] if filter.key?(:type)
202
+ params['filter']['search'] = filter[:search] if filter.key?(:search)
203
+ params['page'] = page if page
204
+ params['limit'] = limit if limit
205
+ params['sort'] = sort if sort
206
+ client.http.post("#{API_URL}/campaigns/#{campaign_id}/reports/subscriber-activity", body: params.compact.to_json)
207
+ end
208
+
209
+ # Get a list of all campaign languages available
210
+ #
211
+ # @return [HTTP::Response] the response from the API
212
+ def languages
213
+ client.http.get("#{API_URL}/campaigns/languages")
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'http'
4
+ require 'dotenv/load'
5
+
6
+ API_URL = 'https://connect.mailerlite.com/api'
7
+
8
+ Dotenv.require_keys('MAILERLITE_API_TOKEN')
9
+
10
+ # mailerlite-ruby is a gem that integrates all endpoints from MailerLite API
11
+ module MailerLite
12
+ attr_reader :api_token
13
+
14
+ # Inits the client.
15
+ class Client
16
+ def initialize(api_token = ENV.fetch('MAILERLITE_API_TOKEN', nil))
17
+ @api_token = api_token
18
+ end
19
+
20
+ def headers
21
+ {
22
+ 'User-Agent' => "MailerLite-client-ruby/#{MailerLite::VERSION}",
23
+ 'Accept' => 'application/json',
24
+ 'Content-type' => 'application/json'
25
+ }
26
+ end
27
+
28
+ def http
29
+ HTTP
30
+ .timeout(connect: 15, read: 30)
31
+ .auth("Bearer #{@api_token}")
32
+ .headers(headers)
33
+ end
34
+ end
35
+ end