drip-ruby 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -1
  3. data/.rubocop_todo.yml +17 -9
  4. data/.travis.yml +1 -1
  5. data/CHANGELOG.md +20 -1
  6. data/Gemfile +10 -0
  7. data/drip-ruby.gemspec +3 -11
  8. data/lib/drip/client.rb +44 -66
  9. data/lib/drip/client/accounts.rb +2 -2
  10. data/lib/drip/client/broadcasts.rb +2 -2
  11. data/lib/drip/client/campaign_subscriptions.rb +1 -1
  12. data/lib/drip/client/campaigns.rb +5 -5
  13. data/lib/drip/client/configuration.rb +28 -0
  14. data/lib/drip/client/conversions.rb +2 -2
  15. data/lib/drip/client/custom_fields.rb +1 -1
  16. data/lib/drip/client/events.rb +4 -4
  17. data/lib/drip/client/forms.rb +2 -2
  18. data/lib/drip/client/http_client.rb +64 -0
  19. data/lib/drip/client/orders.rb +3 -3
  20. data/lib/drip/client/shopper_activity.rb +78 -0
  21. data/lib/drip/client/subscribers.rb +13 -13
  22. data/lib/drip/client/tags.rb +3 -3
  23. data/lib/drip/client/webhooks.rb +5 -5
  24. data/lib/drip/client/workflow_triggers.rb +3 -3
  25. data/lib/drip/client/workflows.rb +6 -6
  26. data/lib/drip/request.rb +31 -0
  27. data/lib/drip/response.rb +13 -13
  28. data/lib/drip/version.rb +1 -1
  29. data/test/drip/client/configuration_test.rb +122 -0
  30. data/test/drip/client/http_client_test.rb +96 -0
  31. data/test/drip/client/shopper_activity_test.rb +175 -0
  32. data/test/drip/client_test.rb +32 -97
  33. data/test/drip/collection_test.rb +14 -0
  34. data/test/drip/collections/account_test.rb +8 -0
  35. data/test/drip/collections/broadcasts_test.rb +8 -0
  36. data/test/drip/collections/campaign_subscriptions_test.rb +8 -0
  37. data/test/drip/collections/campaigns_test.rb +8 -0
  38. data/test/drip/collections/errors_test.rb +8 -0
  39. data/test/drip/collections/orders_test.rb +8 -0
  40. data/test/drip/collections/purchases_test.rb +8 -0
  41. data/test/drip/collections/tags_test.rb +8 -0
  42. data/test/drip/collections/webhooks_test.rb +8 -0
  43. data/test/drip/collections/workflow_triggers_test.rb +8 -0
  44. data/test/drip/collections/workflows_test.rb +8 -0
  45. data/test/drip/request_test.rb +58 -0
  46. data/test/drip/resource_test.rb +12 -0
  47. data/test/drip/resources/tag_test.rb +13 -0
  48. data/test/drip/response_test.rb +33 -0
  49. data/test/test_helper.rb +3 -0
  50. metadata +42 -104
@@ -19,11 +19,6 @@ class Drip::ClientTest < Drip::TestCase
19
19
  assert_equal "aaaa", client.url_prefix
20
20
  end
21
21
 
22
- should "have default url prefix" do
23
- client = Drip::Client.new
24
- assert_equal "https://api.getdrip.com/v2/", client.url_prefix
25
- end
26
-
27
22
  should "accept access token" do
28
23
  client = Drip::Client.new do |config|
29
24
  config.access_token = "aaaa"
@@ -55,36 +50,12 @@ class Drip::ClientTest < Drip::TestCase
55
50
  assert_equal 20, client.http_open_timeout
56
51
  assert_equal 25, client.http_timeout
57
52
  end
58
- end
59
53
 
60
- context "#generate_resource" do
61
- setup do
62
- @key = "subscribers"
63
- @data = { "email" => "foo" }
64
- @client = Drip::Client.new
65
- end
66
-
67
- should "return a JSON API payload" do
68
- assert_equal({ @key => [@data] }, @client.generate_resource(@key, @data))
69
- end
70
- end
71
-
72
- context "given a personal api key" do
73
- setup do
74
- @key = "aaaa"
75
- @client = Drip::Client.new do |config|
76
- config.api_key = @key
77
- end
78
- end
79
-
80
- should "use Basic authentication" do
81
- stub_request(:get, "https://api.getdrip.com/v2/testpath").
82
- to_return(status: 200, body: "", headers: {})
83
-
84
- @client.get("testpath")
85
-
86
- header = "Basic #{Base64.encode64(@key + ':')}".strip
87
- assert_requested :get, "https://api.getdrip.com/v2/testpath", headers: { 'Authorization' => header }
54
+ should "accept options after initialization" do
55
+ # Deprecated
56
+ client = Drip::Client.new
57
+ assert_output(nil, /^\[DEPRECATED\] Setting configuration/) { client.account_id = "12345" }
58
+ assert_equal "12345", client.account_id
88
59
  end
89
60
  end
90
61
 
@@ -98,6 +69,7 @@ class Drip::ClientTest < Drip::TestCase
98
69
 
99
70
  should "return objects" do
100
71
  stub_request(:get, "https://api.getdrip.com/v2/12345/subscribers/jdoe%40example.com").
72
+ with(headers: { "Content-Type" => "application/vnd.api+json" }).
101
73
  to_return(status: 200, body: "{\"links\":{\"subscribers.account\":\"https://api.getdrip.com/v2/accounts/{subscribers.account}\"},\"subscribers\":[{\"id\":\"randomid\",\"href\":\"https://api.getdrip.com/v2/1234/subscribers/randomid\",\"status\":\"active\",\"email\":\"jdoe@example.com\",\"time_zone\":null,\"utc_offset\":0,\"visitor_uuid\":null,\"custom_fields\":{\"first_name\":\"John\"},\"tags\":[\"customer\"],\"created_at\":\"2018-06-04T21:29:49Z\",\"ip_address\":null,\"user_agent\":null,\"lifetime_value\":null,\"original_referrer\":null,\"landing_url\":null,\"prospect\":null,\"base_lead_score\":null,\"eu_consent\":\"unknown\",\"lead_score\":null,\"user_id\":\"123\",\"links\":{\"account\":\"1234\"}}]}")
102
74
 
103
75
  response = @client.subscriber('jdoe@example.com')
@@ -120,85 +92,48 @@ class Drip::ClientTest < Drip::TestCase
120
92
  @client = Drip::Client.new do |config|
121
93
  config.api_key = @key
122
94
  config.url_prefix = @url_prefix
95
+ config.account_id = "12345"
123
96
  end
124
97
  end
125
98
 
126
- should "connect to alternate prefix" do
127
- stub_request(:get, "https://api.example.com/v9001/testpath").
99
+ should "connect to alternate prefix with prepended v2" do
100
+ stub_request(:get, "https://api.example.com/v9001/v2/12345/subscribers/blah").
128
101
  to_return(status: 200, body: "", headers: {})
129
- @client.get("testpath")
102
+ @client.subscriber("blah")
130
103
 
131
- assert_requested :get, "https://api.example.com/v9001/testpath"
104
+ assert_requested :get, "https://api.example.com/v9001/v2/12345/subscribers/blah"
132
105
  end
133
106
  end
134
107
 
135
- context "given a OAuth access token" do
136
- setup do
137
- @key = "aaaa"
138
- @client = Drip::Client.new do |config|
139
- config.access_token = @key
140
- end
141
- end
142
-
143
- should "use Bearer token authentication" do
144
- stub_request(:get, "https://api.getdrip.com/v2/testpath").
145
- to_return(status: 200, body: "", headers: {})
146
- @client.get("testpath")
147
- header = "Bearer #{@key}"
148
- assert_requested :get, "https://api.getdrip.com/v2/testpath", headers: { 'Authorization' => header }
108
+ context "#generate_resource" do
109
+ # Deprecated
110
+ should "return a resource and note deprecation" do
111
+ client = Drip::Client.new
112
+ resource = nil
113
+ assert_output(nil, /^\[DEPRECATED\] Drip\:\:Client\#generate_resource is deprecated/) { resource = client.generate_resource("hello", {}) }
114
+ assert_equal({ "hello" => [{}] }, resource)
149
115
  end
150
116
  end
151
117
 
152
- context "given a redirecting url" do
153
- setup do
154
- @client = Drip::Client.new
118
+ context "#content_type" do
119
+ # Deprecated
120
+ should "return default content type and print warning" do
121
+ client = Drip::Client.new
122
+ content_type = nil
123
+ assert_output(nil, /^\[DEPRECATED\] Drip\:\:Client\#content_type is deprecated/) { content_type = client.content_type }
124
+ assert_equal "application/vnd.api+json", content_type
155
125
  end
126
+ end
156
127
 
157
- should "follow redirect" do
128
+ context "#get et all" do
129
+ # Deprecated
130
+ should "delegate with v2 and print deprecation warning" do
158
131
  stub_request(:get, "https://api.getdrip.com/v2/testpath").
159
- to_return(status: 301, body: "", headers: { "Location" => "https://api.example.com/mytestpath" })
160
- stub_request(:get, "https://api.example.com/mytestpath").
161
132
  to_return(status: 200, body: "{}")
162
- response = @client.get("testpath")
163
- assert_requested :get, "https://api.getdrip.com/v2/testpath"
164
- assert_requested :get, "https://api.example.com/mytestpath"
165
- assert_equal({}, response.body)
166
- end
167
133
 
168
- should "not follow too many redirects" do
169
- stub_request(:get, "https://api.getdrip.com/v2/testpath").
170
- to_return(status: 301, body: "", headers: { "Location" => "https://api.example.com/mytestpath" })
171
- stub_request(:get, "https://api.example.com/mytestpath").
172
- to_return(status: 302, body: "", headers: { "Location" => "https://api.getdrip.com/v2/testpath" })
173
- assert_raises(Drip::TooManyRedirectsError) { @client.get("testpath") }
174
- assert_requested :get, "https://api.getdrip.com/v2/testpath", times: 5
175
- assert_requested :get, "https://api.example.com/mytestpath", times: 5
176
- end
177
- end
178
-
179
- context "given a get request" do
180
- setup do
181
- @client = Drip::Client.new
182
- @response = mock
183
- @response.stubs(:code).returns('200')
184
- @response.stubs(:body).returns('{}')
185
-
186
- @http = mock
187
- @http.expects(:request).returns(@response)
188
-
189
- @request = mock
190
- @request.stubs(:[]=)
191
- @request.stubs(:basic_auth)
192
- end
193
-
194
- should "encode query and not set body" do
195
- Net::HTTP::Get.expects(:new).returns(@request)
196
- Net::HTTP.expects(:start).yields(@http).returns(@response)
197
-
198
- @request.expects(:body=).never
199
- URI.expects(:encode_www_form).once
200
-
201
- response = @client.get("testpath")
134
+ client = Drip::Client.new
135
+ response = nil
136
+ assert_output(nil, /^\[DEPRECATED\] Drip\:\:Client\#get please use the API endpoint specific methods/) { response = client.get("testpath") }
202
137
  assert_equal({}, response.body)
203
138
  end
204
139
  end
@@ -42,6 +42,19 @@ class Drip::CollectionTest < Drip::TestCase
42
42
  end
43
43
  end
44
44
 
45
+ context ".collection_name" do
46
+ should "return default value" do
47
+ assert_equal "resources", Drip::Collection.collection_name
48
+ end
49
+ end
50
+
51
+ context ".resource_name" do
52
+ should "return default value" do
53
+ assert_equal "resource", Drip::Collection.resource_name
54
+ end
55
+ end
56
+
57
+ # :nocov:
45
58
  class TestCollection < Drip::Collection
46
59
  def self.collection_name
47
60
  "subscribers"
@@ -51,4 +64,5 @@ class Drip::CollectionTest < Drip::TestCase
51
64
  "subscriber"
52
65
  end
53
66
  end
67
+ # :nocov:
54
68
  end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/accounts"
3
+
4
+ class Drip::AccountsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "account", Drip::Accounts.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/broadcasts"
3
+
4
+ class Drip::BroadcastsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "broadcast", Drip::Broadcasts.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/campaign_subscriptions"
3
+
4
+ class Drip::CampaignSubscriptionsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "campaign_subscription", Drip::CampaignSubscriptions.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/campaigns"
3
+
4
+ class Drip::CampaignsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "campaign", Drip::Campaigns.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/errors"
3
+
4
+ class Drip::ErrorsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "error", Drip::Errors.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/orders"
3
+
4
+ class Drip::OrdersTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "order", Drip::Orders.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/purchases"
3
+
4
+ class Drip::PurchasesTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "purchase", Drip::Purchases.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/tags"
3
+
4
+ class Drip::TagsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "tag", Drip::Tags.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/webhooks"
3
+
4
+ class Drip::WebhooksTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "webhook", Drip::Webhooks.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/workflow_triggers"
3
+
4
+ class Drip::WorkflowTriggersTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "workflow_trigger", Drip::WorkflowTriggers.resource_name
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/collections/workflows"
3
+
4
+ class Drip::WorkflowsTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "workflow", Drip::Workflows.resource_name
7
+ end
8
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+ require "drip/request"
3
+
4
+ class Drip::RequestTest < Drip::TestCase
5
+ context "basic data" do
6
+ should "pass through data" do
7
+ request = Drip::Request.new(:get, "https://www.example.com/blah", { hello: "world" }, "application/vnd.visio")
8
+ assert_equal :get, request.http_verb
9
+ assert_equal "https://www.example.com/blah", request.url
10
+ assert_equal({ hello: "world" }, request.options)
11
+ assert_equal "application/vnd.visio", request.content_type
12
+ end
13
+ end
14
+
15
+ context "#verb_klass" do
16
+ context "when a supported verb" do
17
+ setup do
18
+ @subject = Drip::Request.new(:get, "https://www.example.com/blah", { hello: "world" }, "application/vnd.visio")
19
+ end
20
+
21
+ should "return a useful http class" do
22
+ assert_equal Net::HTTP::Get, @subject.verb_klass
23
+ end
24
+ end
25
+
26
+ context "when an unsupported verb" do
27
+ setup do
28
+ @subject = Drip::Request.new(:garbage, "https://www.example.com/blah", { hello: "world" }, "application/vnd.visio")
29
+ end
30
+
31
+ should "return nil" do
32
+ assert_nil @subject.verb_klass
33
+ end
34
+ end
35
+ end
36
+
37
+ context "#body" do
38
+ context "when HTTP GET" do
39
+ setup do
40
+ @subject = Drip::Request.new(:get, "https://www.example.com/blah", { hello: "world" }, "application/vnd.visio")
41
+ end
42
+
43
+ should "return nil" do
44
+ assert_nil @subject.body
45
+ end
46
+ end
47
+
48
+ context "when HTTP POST" do
49
+ setup do
50
+ @subject = Drip::Request.new(:post, "https://www.example.com/blah", { hello: "world" }, "application/vnd.visio")
51
+ end
52
+
53
+ should "return JSON" do
54
+ assert_equal '{"hello":"world"}', @subject.body
55
+ end
56
+ end
57
+ end
58
+ end
@@ -19,4 +19,16 @@ class Drip::ResourceTest < Drip::TestCase
19
19
  resource = TestResource.new("created_at" => "2015-06-15T10:00:00Z")
20
20
  assert_equal Time.utc(2015, 6, 15, 10, 0, 0), resource.created_at
21
21
  end
22
+
23
+ context ".resource_name" do
24
+ should "return default value" do
25
+ assert_equal "resource", Drip::Resource.resource_name
26
+ end
27
+ end
28
+
29
+ context "#singular?" do
30
+ should "return default value" do
31
+ assert_equal true, Drip::Resource.new({}).singular?
32
+ end
33
+ end
22
34
  end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper.rb'
2
+ require "drip/resources/tag"
3
+
4
+ class Drip::TagTest < Drip::TestCase
5
+ should "have a resource name" do
6
+ assert_equal "tag", Drip::Tag.resource_name
7
+ end
8
+
9
+ should "accept data" do
10
+ tag = Drip::Tag.new({ attr1: "hello" })
11
+ assert_equal "hello", tag.attributes[:attr1]
12
+ end
13
+ end
@@ -120,6 +120,23 @@ class Drip::ResponseTest < Drip::TestCase
120
120
  assert @subject.subscribers.is_a?(Drip::Subscribers)
121
121
  assert_equal 1, @subject.subscribers.count
122
122
  end
123
+
124
+ should "parse resource" do
125
+ body = { "subscriber" => @members[0] }
126
+ subject = Drip::Response.new(200, body)
127
+ assert_equal "john@acme.com", subject.subscriber.email
128
+ end
129
+
130
+ context "with v3 response" do
131
+ setup do
132
+ @body = { "request_id" => "9f119d4b-893a-4279-adb2-c920b6c2034b" }
133
+ @subject = Drip::Response.new(200, @body)
134
+ end
135
+
136
+ should "be accessible via method call" do
137
+ assert_equal "9f119d4b-893a-4279-adb2-c920b6c2034b", @subject.request_id
138
+ end
139
+ end
123
140
  end
124
141
 
125
142
  context "rate limit response" do
@@ -133,4 +150,20 @@ class Drip::ResponseTest < Drip::TestCase
133
150
  assert_equal @body["message"], @subject.message
134
151
  end
135
152
  end
153
+
154
+ context "#respond_to?" do
155
+ setup do
156
+ @members = [load_json_fixture("resources/subscriber.json")]
157
+ @body = { "subscribers" => @members }
158
+ @subject = Drip::Response.new(200, @body)
159
+ end
160
+
161
+ should "respond to fixture members" do
162
+ assert @subject.respond_to?(:subscribers)
163
+ end
164
+
165
+ should "not respond to randomness" do
166
+ refute @subject.respond_to?(:blahdeblah)
167
+ end
168
+ end
136
169
  end
@@ -1,3 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
 
3
6
  require "drip"