drip-ruby 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +12 -0
- data/drip-ruby.gemspec +29 -0
- data/lib/drip.rb +2 -0
- data/lib/drip/client.rb +82 -0
- data/lib/drip/client/events.rb +20 -0
- data/lib/drip/client/subscribers.rb +81 -0
- data/lib/drip/client/tags.rb +30 -0
- data/lib/drip/collection.rb +39 -0
- data/lib/drip/collections.rb +21 -0
- data/lib/drip/collections/errors.rb +13 -0
- data/lib/drip/collections/subscribers.rb +13 -0
- data/lib/drip/resource.rb +50 -0
- data/lib/drip/resources.rb +21 -0
- data/lib/drip/resources/error.rb +13 -0
- data/lib/drip/resources/subscriber.rb +15 -0
- data/lib/drip/response.rb +68 -0
- data/lib/drip/version.rb +3 -0
- data/test/drip/client/events_test.rb +42 -0
- data/test/drip/client/subscribers_test.rb +132 -0
- data/test/drip/client/tags_test.rb +54 -0
- data/test/drip/client_test.rb +33 -0
- data/test/drip/collection_test.rb +54 -0
- data/test/drip/collections_test.rb +12 -0
- data/test/drip/resource_test.rb +34 -0
- data/test/drip/resources/subscriber_test.rb +20 -0
- data/test/drip/resources_test.rb +12 -0
- data/test/drip/response_test.rb +124 -0
- data/test/fixtures/resources/not_found_error.json +4 -0
- data/test/fixtures/resources/subscriber.json +17 -0
- data/test/test_helper.rb +25 -0
- metadata +191 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
require "drip/collections/subscribers"
|
2
|
+
require "drip/collections/errors"
|
3
|
+
|
4
|
+
module Drip
|
5
|
+
module Collections
|
6
|
+
def self.classes
|
7
|
+
[
|
8
|
+
Drip::Subscribers,
|
9
|
+
Drip::Errors
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.find_class(name)
|
14
|
+
matches = self.classes.select do |c|
|
15
|
+
c.collection_name == name
|
16
|
+
end
|
17
|
+
|
18
|
+
matches.first || Drip::Collection
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Drip
|
2
|
+
class Resource
|
3
|
+
attr_reader :raw_attributes, :attributes
|
4
|
+
|
5
|
+
def initialize(raw_data = {})
|
6
|
+
@raw_attributes = raw_data.dup.freeze
|
7
|
+
@attributes = process(@raw_attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.resource_name
|
11
|
+
"resource"
|
12
|
+
end
|
13
|
+
|
14
|
+
def attribute_keys
|
15
|
+
[]
|
16
|
+
end
|
17
|
+
|
18
|
+
def singular?
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def respond_to?(method_name, include_private = false)
|
23
|
+
attribute_keys.include?(method_name) || super
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method_name, *args, &block)
|
27
|
+
attribute_keys.include?(method_name) ? attributes[method_name] : super
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def process(attributes)
|
33
|
+
{}.tap do |attrs|
|
34
|
+
attribute_keys.each do |key|
|
35
|
+
raw_value = attributes[key.to_s]
|
36
|
+
attrs[key] = process_attribute(key, raw_value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def process_attribute(key, raw_value)
|
42
|
+
case key
|
43
|
+
when :created_at, :updated_at
|
44
|
+
raw_value ? Time.parse(raw_value) : nil
|
45
|
+
else
|
46
|
+
raw_value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "drip/resources/subscriber"
|
2
|
+
require "drip/resources/error"
|
3
|
+
|
4
|
+
module Drip
|
5
|
+
module Resources
|
6
|
+
def self.classes
|
7
|
+
[
|
8
|
+
Drip::Subscriber,
|
9
|
+
Drip::Error
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.find_class(name)
|
14
|
+
matches = self.classes.select do |c|
|
15
|
+
c.resource_name == name
|
16
|
+
end
|
17
|
+
|
18
|
+
matches.first || Drip::Resource
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "drip/resource"
|
2
|
+
require "time"
|
3
|
+
|
4
|
+
module Drip
|
5
|
+
class Subscriber < Resource
|
6
|
+
def self.resource_name
|
7
|
+
"subscriber"
|
8
|
+
end
|
9
|
+
|
10
|
+
def attribute_keys
|
11
|
+
%i{id status email custom_fields tags time_zone
|
12
|
+
utc_offset visitor_uuid created_at href}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require "drip/resources"
|
2
|
+
require "drip/collections"
|
3
|
+
|
4
|
+
module Drip
|
5
|
+
class Response
|
6
|
+
attr_reader :status, :body, :links, :meta, :members
|
7
|
+
|
8
|
+
def initialize(status, body)
|
9
|
+
@status = status
|
10
|
+
@body = body
|
11
|
+
@links = parse_links
|
12
|
+
@meta = parse_meta
|
13
|
+
@members = parse_members
|
14
|
+
|
15
|
+
@body.freeze
|
16
|
+
end
|
17
|
+
|
18
|
+
def ==(other)
|
19
|
+
status == other.status &&
|
20
|
+
body == other.body
|
21
|
+
end
|
22
|
+
|
23
|
+
def success?
|
24
|
+
(200..299).include?(status)
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_to?(method_name, include_private = false)
|
28
|
+
member_map.keys.include?(method_name) || super
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(method_name, *args, &block)
|
32
|
+
return super unless member_map.keys.include?(method_name)
|
33
|
+
members[member_map[method_name]]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def parse_links
|
39
|
+
body.is_a?(Hash) ? body.delete("links") : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_meta
|
43
|
+
body.is_a?(Hash) ? body.delete("meta") : nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse_members
|
47
|
+
{}.tap do |members|
|
48
|
+
if body.is_a?(Hash)
|
49
|
+
body.each do |key, value|
|
50
|
+
klass = if value.is_a?(Array)
|
51
|
+
Drip::Collections.find_class(key)
|
52
|
+
else
|
53
|
+
Drip::Resources.find_class(key)
|
54
|
+
end
|
55
|
+
|
56
|
+
members[key] = klass.new(value)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def member_map
|
63
|
+
@member_map ||= {}.tap do |map|
|
64
|
+
members.each { |key, value| map[key.to_sym] = key }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/drip/version.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
require "faraday"
|
3
|
+
|
4
|
+
class Drip::Client::EventsTest < Drip::TestCase
|
5
|
+
def setup
|
6
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
|
+
|
8
|
+
@connection = Faraday.new do |builder|
|
9
|
+
builder.adapter :test, @stubs
|
10
|
+
end
|
11
|
+
|
12
|
+
@client = Drip::Client.new { |c| c.account_id = "12345" }
|
13
|
+
@client.expects(:connection).at_least_once.returns(@connection)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "#track_event" do
|
17
|
+
setup do
|
18
|
+
@email = "derrick@getdrip.com"
|
19
|
+
@action = "Signed up"
|
20
|
+
@properties = { "foo" => "bar" }
|
21
|
+
@payload = {
|
22
|
+
"events" => [{
|
23
|
+
"email" => @email,
|
24
|
+
"action" => @action,
|
25
|
+
"properties" => @properties
|
26
|
+
}]
|
27
|
+
}.to_json
|
28
|
+
|
29
|
+
@response_status = 201
|
30
|
+
@response_body = stub
|
31
|
+
|
32
|
+
@stubs.post "12345/events", @payload do
|
33
|
+
[@response_status, {}, @response_body]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
should "send the right request" do
|
38
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
39
|
+
assert_equal expected, @client.track_event(@email, @action, @properties)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
require "faraday"
|
3
|
+
|
4
|
+
class Drip::Client::SubscribersTest < Drip::TestCase
|
5
|
+
def setup
|
6
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
|
+
|
8
|
+
@connection = Faraday.new do |builder|
|
9
|
+
builder.adapter :test, @stubs
|
10
|
+
end
|
11
|
+
|
12
|
+
@client = Drip::Client.new { |c| c.account_id = "12345" }
|
13
|
+
@client.expects(:connection).at_least_once.returns(@connection)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "#subscriber" do
|
17
|
+
setup do
|
18
|
+
@id = "derrick@getdrip.com"
|
19
|
+
@response_status = 201
|
20
|
+
@response_body = stub
|
21
|
+
|
22
|
+
@stubs.get "12345/subscribers/#{CGI.escape @id}" do
|
23
|
+
[@response_status, {}, @response_body]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
should "send the right request" do
|
28
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
29
|
+
assert_equal expected, @client.subscriber(@id)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "#create_or_update_subscriber" do
|
34
|
+
setup do
|
35
|
+
@email = "derrick@getdrip.com"
|
36
|
+
@data = { "time_zone" => "America/Los_Angeles" }
|
37
|
+
@payload = { "subscribers" => [@data.merge(:email => @email)] }.to_json
|
38
|
+
|
39
|
+
@response_status = 201
|
40
|
+
@response_body = stub
|
41
|
+
|
42
|
+
@stubs.post "12345/subscribers", @payload do
|
43
|
+
[@response_status, {}, @response_body]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
should "send the right request" do
|
48
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
49
|
+
assert_equal expected, @client.create_or_update_subscriber(@email, @data)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "#subscribe" do
|
54
|
+
setup do
|
55
|
+
@email = "derrick@getdrip.com"
|
56
|
+
@campaign_id = "12345"
|
57
|
+
@data = { "time_zone" => "America/Los_Angeles" }
|
58
|
+
@payload = { "subscribers" => [@data.merge(:email => @email)] }.to_json
|
59
|
+
|
60
|
+
@response_status = 201
|
61
|
+
@response_body = stub
|
62
|
+
|
63
|
+
@stubs.post "12345/campaigns/#{@campaign_id}/subscribers", @payload do
|
64
|
+
[@response_status, {}, @response_body]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
should "send the right request" do
|
69
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
70
|
+
assert_equal expected, @client.subscribe(@email, @campaign_id, @data)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "#unsubscribe" do
|
75
|
+
context "if no campaign id is provided" do
|
76
|
+
setup do
|
77
|
+
@id = "derrick@getdrip.com"
|
78
|
+
|
79
|
+
@response_status = 201
|
80
|
+
@response_body = stub
|
81
|
+
|
82
|
+
@stubs.post "12345/subscribers/#{CGI.escape @id}/unsubscribe" do
|
83
|
+
[@response_status, {}, @response_body]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
should "send the right request" do
|
88
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
89
|
+
assert_equal expected, @client.unsubscribe(@id)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "if a campaign id is provided" do
|
94
|
+
setup do
|
95
|
+
@id = "derrick@getdrip.com"
|
96
|
+
@campaign = "12345"
|
97
|
+
|
98
|
+
@response_status = 201
|
99
|
+
@response_body = stub
|
100
|
+
|
101
|
+
@stubs.post "12345/subscribers/#{CGI.escape @id}/unsubscribe?campaign_id=#{@campaign}" do
|
102
|
+
[@response_status, {}, @response_body]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
should "send the right request" do
|
107
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
108
|
+
assert_equal expected, @client.unsubscribe(@id, campaign_id: @campaign)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context "#apply_tag" do
|
114
|
+
setup do
|
115
|
+
@email = "derrick@getdrip.com"
|
116
|
+
@tag = "Customer"
|
117
|
+
@payload = { "tags" => [{ "email" => @email, "tag" => @tag }] }.to_json
|
118
|
+
|
119
|
+
@response_status = 201
|
120
|
+
@response_body = stub
|
121
|
+
|
122
|
+
@stubs.post "12345/tags", @payload do
|
123
|
+
[@response_status, {}, @response_body]
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
should "send the right request" do
|
128
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
129
|
+
assert_equal expected, @client.apply_tag(@email, @tag)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper.rb'
|
2
|
+
require "faraday"
|
3
|
+
|
4
|
+
class Drip::Client::TagsTest < Drip::TestCase
|
5
|
+
def setup
|
6
|
+
@stubs = Faraday::Adapter::Test::Stubs.new
|
7
|
+
|
8
|
+
@connection = Faraday.new do |builder|
|
9
|
+
builder.adapter :test, @stubs
|
10
|
+
end
|
11
|
+
|
12
|
+
@client = Drip::Client.new { |c| c.account_id = "12345" }
|
13
|
+
@client.expects(:connection).at_least_once.returns(@connection)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "#apply_tag" do
|
17
|
+
setup do
|
18
|
+
@email = "derrick@getdrip.com"
|
19
|
+
@tag = "Customer"
|
20
|
+
@payload = { "tags" => [{ "email" => @email, "tag" => @tag }] }.to_json
|
21
|
+
|
22
|
+
@response_status = 201
|
23
|
+
@response_body = stub
|
24
|
+
|
25
|
+
@stubs.post "12345/tags", @payload do
|
26
|
+
[@response_status, {}, @response_body]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
should "send the right request" do
|
31
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
32
|
+
assert_equal expected, @client.apply_tag(@email, @tag)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "#remove_tag" do
|
37
|
+
setup do
|
38
|
+
@email = "derrick@getdrip.com"
|
39
|
+
@tag = "Customer"
|
40
|
+
|
41
|
+
@response_status = 204
|
42
|
+
@response_body = stub
|
43
|
+
|
44
|
+
@stubs.delete "12345/subscribers/#{CGI.escape @email}/tags/#{CGI.escape @tag}" do
|
45
|
+
[@response_status, {}, @response_body]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
should "send the right request" do
|
50
|
+
expected = Drip::Response.new(@response_status, @response_body)
|
51
|
+
assert_equal expected, @client.remove_tag(@email, @tag)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|