drip-ruby 3.1.1 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +9 -1
  3. data/.rubocop_todo.yml +17 -14
  4. data/.travis.yml +2 -4
  5. data/CHANGELOG.md +51 -1
  6. data/Gemfile +12 -0
  7. data/README.md +6 -0
  8. data/Rakefile +2 -0
  9. data/drip-ruby.gemspec +4 -11
  10. data/lib/drip.rb +2 -0
  11. data/lib/drip/client.rb +48 -66
  12. data/lib/drip/client/accounts.rb +4 -2
  13. data/lib/drip/client/broadcasts.rb +4 -2
  14. data/lib/drip/client/campaign_subscriptions.rb +3 -1
  15. data/lib/drip/client/campaigns.rb +7 -5
  16. data/lib/drip/client/configuration.rb +30 -0
  17. data/lib/drip/client/conversions.rb +4 -2
  18. data/lib/drip/client/custom_fields.rb +3 -1
  19. data/lib/drip/client/events.rb +6 -4
  20. data/lib/drip/client/forms.rb +4 -2
  21. data/lib/drip/client/http_client.rb +66 -0
  22. data/lib/drip/client/orders.rb +5 -3
  23. data/lib/drip/client/shopper_activity.rb +80 -0
  24. data/lib/drip/client/subscribers.rb +23 -16
  25. data/lib/drip/client/tags.rb +5 -3
  26. data/lib/drip/client/users.rb +17 -0
  27. data/lib/drip/client/webhooks.rb +7 -5
  28. data/lib/drip/client/workflow_triggers.rb +5 -3
  29. data/lib/drip/client/workflows.rb +8 -6
  30. data/lib/drip/collection.rb +3 -1
  31. data/lib/drip/collections.rb +2 -0
  32. data/lib/drip/collections/accounts.rb +2 -0
  33. data/lib/drip/collections/broadcasts.rb +2 -0
  34. data/lib/drip/collections/campaign_subscriptions.rb +2 -0
  35. data/lib/drip/collections/campaigns.rb +2 -0
  36. data/lib/drip/collections/errors.rb +2 -0
  37. data/lib/drip/collections/orders.rb +2 -0
  38. data/lib/drip/collections/purchases.rb +2 -0
  39. data/lib/drip/collections/subscribers.rb +2 -0
  40. data/lib/drip/collections/tags.rb +2 -0
  41. data/lib/drip/collections/webhooks.rb +2 -0
  42. data/lib/drip/collections/workflow_triggers.rb +2 -0
  43. data/lib/drip/collections/workflows.rb +2 -0
  44. data/lib/drip/errors.rb +2 -0
  45. data/lib/drip/request.rb +33 -0
  46. data/lib/drip/resource.rb +2 -0
  47. data/lib/drip/resources.rb +2 -0
  48. data/lib/drip/resources/account.rb +2 -0
  49. data/lib/drip/resources/broadcast.rb +2 -0
  50. data/lib/drip/resources/campaign.rb +2 -0
  51. data/lib/drip/resources/campaign_subscription.rb +2 -0
  52. data/lib/drip/resources/error.rb +2 -0
  53. data/lib/drip/resources/order.rb +2 -0
  54. data/lib/drip/resources/purchase.rb +2 -0
  55. data/lib/drip/resources/subscriber.rb +2 -0
  56. data/lib/drip/resources/tag.rb +2 -0
  57. data/lib/drip/resources/webhook.rb +2 -0
  58. data/lib/drip/resources/workflow.rb +2 -0
  59. data/lib/drip/resources/workflow_trigger.rb +2 -0
  60. data/lib/drip/response.rb +15 -13
  61. data/lib/drip/version.rb +3 -1
  62. data/test/drip/client/accounts_test.rb +2 -0
  63. data/test/drip/client/broadcasts_test.rb +2 -0
  64. data/test/drip/client/campaign_subscriptions_test.rb +2 -0
  65. data/test/drip/client/campaigns_test.rb +2 -0
  66. data/test/drip/client/configuration_test.rb +112 -0
  67. data/test/drip/client/conversions_test.rb +2 -0
  68. data/test/drip/client/custom_fields_test.rb +11 -1
  69. data/test/drip/client/events_test.rb +2 -0
  70. data/test/drip/client/forms_test.rb +2 -0
  71. data/test/drip/client/http_client_test.rb +98 -0
  72. data/test/drip/client/orders_test.rb +2 -0
  73. data/test/drip/client/shopper_activity_test.rb +189 -0
  74. data/test/drip/client/subscribers_test.rb +16 -1
  75. data/test/drip/client/tags_test.rb +2 -0
  76. data/test/drip/client/users_test.rb +24 -0
  77. data/test/drip/client/webhooks_test.rb +2 -0
  78. data/test/drip/client/workflow_triggers_test.rb +2 -0
  79. data/test/drip/client/workflows_test.rb +2 -0
  80. data/test/drip/client_test.rb +34 -97
  81. data/test/drip/collection_test.rb +16 -0
  82. data/test/drip/collections/account_test.rb +10 -0
  83. data/test/drip/collections/broadcasts_test.rb +10 -0
  84. data/test/drip/collections/campaign_subscriptions_test.rb +10 -0
  85. data/test/drip/collections/campaigns_test.rb +10 -0
  86. data/test/drip/collections/errors_test.rb +10 -0
  87. data/test/drip/collections/orders_test.rb +10 -0
  88. data/test/drip/collections/purchases_test.rb +10 -0
  89. data/test/drip/collections/tags_test.rb +10 -0
  90. data/test/drip/collections/webhooks_test.rb +10 -0
  91. data/test/drip/collections/workflow_triggers_test.rb +10 -0
  92. data/test/drip/collections/workflows_test.rb +10 -0
  93. data/test/drip/collections_test.rb +2 -0
  94. data/test/drip/request_test.rb +60 -0
  95. data/test/drip/resource_test.rb +14 -0
  96. data/test/drip/resources/account_test.rb +2 -0
  97. data/test/drip/resources/order_test.rb +2 -0
  98. data/test/drip/resources/subscriber_test.rb +2 -0
  99. data/test/drip/resources/tag_test.rb +15 -0
  100. data/test/drip/resources_test.rb +2 -0
  101. data/test/drip/response_test.rb +35 -0
  102. data/test/test_helper.rb +5 -0
  103. metadata +49 -109
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Broadcasts
@@ -12,7 +14,7 @@ module Drip
12
14
  # Returns a Drip::Response
13
15
  # See https://www.getdrip.com/docs/rest-api#broadcasts
14
16
  def broadcasts(options = {})
15
- get "#{account_id}/broadcasts", options
17
+ make_json_api_request :get, "v2/#{account_id}/broadcasts", options
16
18
  end
17
19
 
18
20
  # Public: Fetch a broadcast.
@@ -22,7 +24,7 @@ module Drip
22
24
  # Returns a Drip::Response.
23
25
  # See https://www.getdrip.com/docs/rest-api#broadcasts
24
26
  def broadcast(id)
25
- get "#{account_id}/broadcasts/#{id}"
27
+ make_json_api_request :get, "v2/#{account_id}/broadcasts/#{id}"
26
28
  end
27
29
  end
28
30
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module CampaignSubscriptions
@@ -8,7 +10,7 @@ module Drip
8
10
  # Returns a Drip::Response.
9
11
  # See https://www.getdrip.com/docs.rest-api#campaign_subscriptions
10
12
  def campaign_subscriptions(subscriber_id)
11
- get "#{account_id}/subscribers/#{subscriber_id}/campaign_subscriptions"
13
+ make_json_api_request :get, "v2/#{account_id}/subscribers/#{subscriber_id}/campaign_subscriptions"
12
14
  end
13
15
  end
14
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Campaigns
@@ -14,7 +16,7 @@ module Drip
14
16
  # Returns a Drip::Response.
15
17
  # See https://www.getdrip.com/docs.rest-api#campaigns
16
18
  def campaigns(options = {})
17
- get "#{account_id}/campaigns", options
19
+ make_json_api_request :get, "v2/#{account_id}/campaigns", options
18
20
  end
19
21
 
20
22
  # Public: Fetch a campaign.
@@ -24,7 +26,7 @@ module Drip
24
26
  # Returns a Drip::Response.
25
27
  # See https://www.getdrip.com/docs/rest-api#campaigns
26
28
  def campaign(id)
27
- get "#{account_id}/campaigns/#{id}"
29
+ make_json_api_request :get, "v2/#{account_id}/campaigns/#{id}"
28
30
  end
29
31
 
30
32
  # Public: Activate a campaign.
@@ -34,7 +36,7 @@ module Drip
34
36
  # Returns a Drip::Response.
35
37
  # See https://www.getdrip.com/docs/rest-api#campaigns
36
38
  def activate_campaign(id)
37
- post "#{account_id}/campaigns/#{id}/activate"
39
+ make_json_api_request :post, "v2/#{account_id}/campaigns/#{id}/activate"
38
40
  end
39
41
 
40
42
  # Public: Pause a campaign.
@@ -44,7 +46,7 @@ module Drip
44
46
  # Returns a Drip::Response.
45
47
  # See https://www.getdrip.com/docs/rest-api#campaigns
46
48
  def pause_campaign(id)
47
- post "#{account_id}/campaigns/#{id}/pause"
49
+ make_json_api_request :post, "v2/#{account_id}/campaigns/#{id}/pause"
48
50
  end
49
51
 
50
52
  # Public: List everyone subscribed to a campaign.
@@ -68,7 +70,7 @@ module Drip
68
70
  # Returns a Drip::Response.
69
71
  # See https://www.getdrip.com/docs/rest-api#campaigns
70
72
  def campaign_subscribers(id, options = {})
71
- get "#{account_id}/campaigns/#{id}/subscribers", options
73
+ make_json_api_request :get, "v2/#{account_id}/campaigns/#{id}/subscribers", options
72
74
  end
73
75
  end
74
76
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Drip
4
+ class Client
5
+ class Configuration
6
+ DEFAULT_URL_PREFIX = "https://api.getdrip.com/"
7
+ private_constant :DEFAULT_URL_PREFIX
8
+
9
+ CONFIGURATION_FIELDS = %i[access_token api_key account_id url_prefix http_open_timeout http_timeout].freeze
10
+
11
+ attr_accessor(*CONFIGURATION_FIELDS)
12
+
13
+ def initialize(**options)
14
+ remainder = options.keys - CONFIGURATION_FIELDS
15
+ raise ArgumentError, "unknown keyword#{'s' if remainder.size > 1}: #{remainder.join(', ')}" unless remainder.empty?
16
+
17
+ # Initialize this variable to suppress Ruby warning.
18
+ @url_prefix = nil
19
+
20
+ options.each do |k, v|
21
+ public_send("#{k}=", v)
22
+ end
23
+ end
24
+
25
+ def url_prefix
26
+ @url_prefix || DEFAULT_URL_PREFIX
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Conversions
@@ -10,7 +12,7 @@ module Drip
10
12
  # Returns a Drip::Response.
11
13
  # See https://www.getdrip.com/docs/rest-api#conversions
12
14
  def conversions(options = {})
13
- get "#{account_id}/goals", options
15
+ make_json_api_request :get, "v2/#{account_id}/goals", options
14
16
  end
15
17
 
16
18
  # Public: Fetch a conversion.
@@ -20,7 +22,7 @@ module Drip
20
22
  # Returns a Drip::Response.
21
23
  # See https://www.getdrip.com/docs/rest-api#conversions
22
24
  def conversion(id)
23
- get "#{account_id}/goals/#{id}"
25
+ make_json_api_request :get, "v2/#{account_id}/goals/#{id}"
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module CustomFields
@@ -6,7 +8,7 @@ module Drip
6
8
  # Returns a Drip::Response.
7
9
  # See https://www.getdrip.com/docs/rest-api#custom_fields
8
10
  def custom_fields
9
- get "#{account_id}/custom_field_identifiers"
11
+ make_json_api_request :get, "v2/#{account_id}/custom_field_identifiers"
10
12
  end
11
13
  end
12
14
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Events
@@ -14,7 +16,7 @@ module Drip
14
16
  # See https://www.getdrip.com/docs/rest-api#record_event
15
17
  def track_event(email, action, properties = {}, options = {})
16
18
  data = options.merge({ "email" => email, "action" => action, "properties" => properties })
17
- post "#{account_id}/events", generate_resource("events", data)
19
+ make_json_api_request :post, "v2/#{account_id}/events", private_generate_resource("events", data)
18
20
  end
19
21
 
20
22
  # Public: Track a collection of events all at once.
@@ -27,8 +29,8 @@ module Drip
27
29
  # Returns a Drip::Response.
28
30
  # See https://www.getdrip.com/docs/rest-api#event_batches
29
31
  def track_events(events)
30
- url = "#{account_id}/events/batches"
31
- post url, generate_resource("batches", { "events" => events })
32
+ url = "v2/#{account_id}/events/batches"
33
+ make_json_api_request :post, url, private_generate_resource("batches", { "events" => events })
32
34
  end
33
35
 
34
36
  # Public: Fetch all custom event actions.
@@ -41,7 +43,7 @@ module Drip
41
43
  # Returns a Drip::Response.
42
44
  # See https://www.getdrip.com/docs/rest-api#events
43
45
  def event_actions(options = {})
44
- get "#{account_id}/event_actions", options
46
+ make_json_api_request :get, "v2/#{account_id}/event_actions", options
45
47
  end
46
48
  end
47
49
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Forms
@@ -6,7 +8,7 @@ module Drip
6
8
  # Returns a Drip::Response.
7
9
  # See https://www.getdrip.com/docs/rest-api#forms
8
10
  def forms
9
- get "#{account_id}/forms"
11
+ make_json_api_request :get, "v2/#{account_id}/forms"
10
12
  end
11
13
 
12
14
  # Public: Fetch a form.
@@ -16,7 +18,7 @@ module Drip
16
18
  # Returns a Drip::Response.
17
19
  # See https://www.getdrip.com/docs/rest-api#forms
18
20
  def form(id)
19
- get "#{account_id}/forms/#{id}"
21
+ make_json_api_request :get, "v2/#{account_id}/forms/#{id}"
20
22
  end
21
23
  end
22
24
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Drip
4
+ class Client
5
+ class HTTPClient
6
+ REDIRECT_LIMIT = 10
7
+ private_constant :REDIRECT_LIMIT
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def make_request(drip_request, redirected_url: nil, step: 0)
14
+ raise TooManyRedirectsError, 'too many HTTP redirects' if step >= REDIRECT_LIMIT
15
+
16
+ uri = redirected_url || drip_request.url.tap do |orig_url|
17
+ next if drip_request.http_verb != :get
18
+
19
+ orig_url.query = URI.encode_www_form(drip_request.options)
20
+ end
21
+
22
+ response = Net::HTTP.start(uri.host, uri.port, connection_options(uri.scheme)) do |http|
23
+ request = drip_request.verb_klass.new uri
24
+ request.body = drip_request.body
25
+
26
+ add_standard_headers(request)
27
+ request['Content-Type'] = drip_request.content_type
28
+
29
+ http.request request
30
+ end
31
+
32
+ return make_request(drip_request, redirected_url: URI(response["Location"]), step: step + 1) if response.is_a?(Net::HTTPRedirection)
33
+
34
+ response
35
+ end
36
+
37
+ private
38
+
39
+ def add_standard_headers(request)
40
+ request['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
41
+ request['Accept'] = "*/*"
42
+
43
+ if @config.access_token
44
+ request['Authorization'] = "Bearer #{@config.access_token}"
45
+ else
46
+ request.basic_auth @config.api_key, ""
47
+ end
48
+
49
+ request
50
+ end
51
+
52
+ def connection_options(uri_scheme)
53
+ options = { use_ssl: uri_scheme == "https" }
54
+
55
+ if @config.http_open_timeout
56
+ options[:open_timeout] = @config.http_open_timeout
57
+ options[:ssl_timeout] = @config.http_open_timeout
58
+ end
59
+
60
+ options[:read_timeout] = @config.http_timeout if @config.http_timeout
61
+
62
+ options
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Drip
2
4
  class Client
3
5
  module Orders
@@ -11,7 +13,7 @@ module Drip
11
13
  # See https://developer.drip.com/#orders
12
14
  def create_or_update_order(email, options = {})
13
15
  data = options.merge(email: email)
14
- post "#{account_id}/orders", generate_resource("orders", data)
16
+ make_json_api_request :post, "v2/#{account_id}/orders", private_generate_resource("orders", data)
15
17
  end
16
18
 
17
19
  # Public: Create or update a batch of orders.
@@ -21,7 +23,7 @@ module Drip
21
23
  # Returns a Drip::Response.
22
24
  # See https://developer.drip.com/#create-or-update-a-batch-of-orders
23
25
  def create_or_update_orders(orders)
24
- post "#{account_id}/orders/batches", generate_resource("batches", { "orders" => orders })
26
+ make_json_api_request :post, "v2/#{account_id}/orders/batches", private_generate_resource("batches", { "orders" => orders })
25
27
  end
26
28
 
27
29
  # Public: Create or update a refund.
@@ -38,7 +40,7 @@ module Drip
38
40
  # Returns a Drip::Response.
39
41
  # See https://developer.drip.com/#create-or-update-a-refund
40
42
  def create_or_update_refund(options)
41
- post "#{account_id}/refunds", generate_resource("refunds", options)
43
+ make_json_api_request :post, "v2/#{account_id}/refunds", private_generate_resource("refunds", options)
42
44
  end
43
45
  end
44
46
  end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Drip
4
+ class Client
5
+ module ShopperActivity
6
+ # Public: Create a cart activity event.
7
+ #
8
+ # options - Required. A Hash of additional cart options. Refer to the
9
+ # Drip API docs for the required schema.
10
+ #
11
+ # Returns a Drip::Response.
12
+ # See https://developer.drip.com/#cart-activity
13
+ def create_cart_activity_event(data = {})
14
+ raise ArgumentError, 'email:, person_id:, or :visitor_uuid parameter required' if !data.key?(:email) && !data.key?(:person_id) && !data.key?(:visitor_uuid)
15
+
16
+ %i[provider action cart_id cart_url].each do |key|
17
+ raise ArgumentError, "#{key}: parameter required" unless data.key?(key)
18
+ end
19
+
20
+ data[:occurred_at] = Time.now.iso8601 unless data.key?(:occurred_at)
21
+ make_json_request :post, "v3/#{account_id}/shopper_activity/cart", data
22
+ end
23
+
24
+ # Public: Create an order activity event.
25
+ #
26
+ # options - Required. A Hash of additional order options. Refer to the
27
+ # Drip API docs for the required schema.
28
+ #
29
+ # Returns a Drip::Response.
30
+ # See https://developer.drip.com/#order-activity
31
+ def create_order_activity_event(data = {})
32
+ raise ArgumentError, 'email: or person_id: parameter required' if !data.key?(:email) && !data.key?(:person_id)
33
+
34
+ %i[provider action order_id].each do |key|
35
+ raise ArgumentError, "#{key}: parameter required" unless data.key?(key)
36
+ end
37
+
38
+ data[:occurred_at] = Time.now.iso8601 unless data.key?(:occurred_at)
39
+ make_json_request :post, "v3/#{account_id}/shopper_activity/order", data
40
+ end
41
+
42
+ # Public: Create a batch of order activity events.
43
+ #
44
+ # records - Required. An array of hashes containing orders attributes.
45
+ # Refer to the Drip API docs for the required schema.
46
+ #
47
+ # Returns a Drip::Response.
48
+ # See https://developer.drip.com/#create-or-update-a-batch-of-orders
49
+ def create_order_activity_events(records = [])
50
+ records.each_with_index do |record, i|
51
+ raise ArgumentError, "email: or person_id: parameter required in record #{i}" if !record.key?(:email) && !record.key?(:person_id)
52
+
53
+ %i[provider action order_id].each do |key|
54
+ raise ArgumentError, "#{key}: parameter required in record #{i}" unless record.key?(key)
55
+ end
56
+
57
+ record[:occurred_at] = Time.now.iso8601 unless record.key?(:occurred_at)
58
+ end
59
+
60
+ make_json_request :post, "v3/#{account_id}/shopper_activity/order/batch", { orders: records }
61
+ end
62
+
63
+ # Public: Create a product activity event.
64
+ #
65
+ # options - Required. A Hash of additional product options. Refer to the
66
+ # Drip API docs for the required schema.
67
+ #
68
+ # Returns a Drip::Response.
69
+ # See https://developer.drip.com/#product-activity
70
+ def create_product_activity_event(data = {})
71
+ %i[provider action product_id name price].each do |key|
72
+ raise ArgumentError, "#{key}: parameter required" unless data.key?(key)
73
+ end
74
+
75
+ data[:occurred_at] = Time.now.iso8601 unless data.key?(:occurred_at)
76
+ make_json_request :post, "v3/#{account_id}/shopper_activity/product", data
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "cgi"
2
4
 
3
5
  module Drip
@@ -16,7 +18,7 @@ module Drip
16
18
  # Returns a Drip::Response.
17
19
  # See https://www.getdrip.com/docs/rest-api#list_subscribers
18
20
  def subscribers(options = {})
19
- get "#{account_id}/subscribers", options
21
+ make_json_api_request :get, "v2/#{account_id}/subscribers", options
20
22
  end
21
23
 
22
24
  # Public: Fetch a subscriber.
@@ -26,13 +28,15 @@ module Drip
26
28
  # Returns a Drip::Response.
27
29
  # See https://www.getdrip.com/docs/rest-api#fetch_subscriber
28
30
  def subscriber(id_or_email)
29
- get "#{account_id}/subscribers/#{CGI.escape id_or_email}"
31
+ make_json_api_request :get, "v2/#{account_id}/subscribers/#{CGI.escape id_or_email}"
30
32
  end
31
33
 
32
34
  # Public: Create or update a subscriber.
33
35
  #
34
- # email - Required. The String subscriber email address.
36
+ # email - Optional. The String subscriber email address. (Deprecated in favor of a hash argument)
35
37
  # options - A Hash of options.
38
+ # - email - Required (or id). Lookup the subscriber by email.
39
+ # - id - Required (or email). Lookup the subscriber by Drip ID.
36
40
  # - new_email - Optional. A new email address for the subscriber.
37
41
  # If provided and a subscriber with the email above
38
42
  # does not exist, this address will be used to
@@ -44,9 +48,12 @@ module Drip
44
48
  #
45
49
  # Returns a Drip::Response.
46
50
  # See https://www.getdrip.com/docs/rest-api#create_or_update_subscriber
47
- def create_or_update_subscriber(email, options = {})
48
- data = options.merge(email: email)
49
- post "#{account_id}/subscribers", generate_resource("subscribers", data)
51
+ def create_or_update_subscriber(*args)
52
+ data = {}
53
+ data[:email] = args[0] if args[0].is_a? String
54
+ data.merge!(args.last) if args.last.is_a? Hash
55
+ raise ArgumentError, 'email: or id: parameter required' if !data.key?(:email) && !data.key?(:id)
56
+ make_json_api_request :post, "v2/#{account_id}/subscribers", private_generate_resource("subscribers", data)
50
57
  end
51
58
 
52
59
  # Public: Create or update a collection of subscribers.
@@ -65,8 +72,8 @@ module Drip
65
72
  # Returns a Drip::Response
66
73
  # See https://www.getdrip.com/docs/rest-api#subscriber_batches
67
74
  def create_or_update_subscribers(subscribers)
68
- url = "#{account_id}/subscribers/batches"
69
- post url, generate_resource("batches", { "subscribers" => subscribers })
75
+ url = "v2/#{account_id}/subscribers/batches"
76
+ make_json_api_request :post, url, private_generate_resource("batches", { "subscribers" => subscribers })
70
77
  end
71
78
 
72
79
  # Public: Unsubscribe a collection of subscribers.
@@ -77,8 +84,8 @@ module Drip
77
84
  # Returns a Drip::Response
78
85
  # See https://www.getdrip.com/docs/rest-api#subscriber_batches
79
86
  def unsubscribe_subscribers(subscribers)
80
- url = "#{account_id}/unsubscribes/batches"
81
- post url, generate_resource("batches", { "subscribers" => subscribers })
87
+ url = "v2/#{account_id}/unsubscribes/batches"
88
+ make_json_api_request :post, url, private_generate_resource("batches", { "subscribers" => subscribers })
82
89
  end
83
90
 
84
91
  # Public: Unsubscribe a subscriber globally or from a specific campaign.
@@ -91,9 +98,9 @@ module Drip
91
98
  # Returns a Drip::Response.
92
99
  # See https://www.getdrip.com/docs/rest-api#unsubscribe
93
100
  def unsubscribe(id_or_email, options = {})
94
- url = "#{account_id}/subscribers/#{CGI.escape id_or_email}/remove"
101
+ url = "v2/#{account_id}/subscribers/#{CGI.escape id_or_email}/remove"
95
102
  url += options[:campaign_id] ? "?campaign_id=#{options[:campaign_id]}" : ""
96
- post url
103
+ make_json_api_request :post, url
97
104
  end
98
105
 
99
106
  # Public: Subscribe to a campaign.
@@ -121,8 +128,8 @@ module Drip
121
128
  # See https://www.getdrip.com/docs/rest-api#subscribe
122
129
  def subscribe(email, campaign_id, options = {})
123
130
  data = options.merge("email" => email)
124
- url = "#{account_id}/campaigns/#{campaign_id}/subscribers"
125
- post url, generate_resource("subscribers", data)
131
+ url = "v2/#{account_id}/campaigns/#{campaign_id}/subscribers"
132
+ make_json_api_request :post, url, private_generate_resource("subscribers", data)
126
133
  end
127
134
 
128
135
  # Public: Delete a subscriber.
@@ -132,7 +139,7 @@ module Drip
132
139
  # Returns No Content.
133
140
  # See https://www.getdrip.com/docs/rest-api#fdelete_subscriber
134
141
  def delete_subscriber(id_or_email)
135
- delete "#{account_id}/subscribers/#{CGI.escape id_or_email}"
142
+ make_json_api_request :delete, "v2/#{account_id}/subscribers/#{CGI.escape id_or_email}"
136
143
  end
137
144
 
138
145
  # Public: Unsubscribe a subscriber from all mailings.
@@ -142,7 +149,7 @@ module Drip
142
149
  # Returns No Content.
143
150
  # See https://www.getdrip.com/docs/rest-api#fdelete_subscriber
144
151
  def unsubscribe_from_all(id_or_email)
145
- post "#{account_id}/subscribers/#{CGI.escape id_or_email}/unsubscribe_all"
152
+ make_json_api_request :post, "v2/#{account_id}/subscribers/#{CGI.escape id_or_email}/unsubscribe_all"
146
153
  end
147
154
  end
148
155
  end