drip-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- 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
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 399ac5f6deb63991f5d809be4cfb1ff91814122c
|
4
|
+
data.tar.gz: 2e0edbc7c6f951004b3ab07b3b7549518e8eb2ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4d5835097d80f4988c1134090077b06e6b15193733455d4fcef4faeb1898d50a0d78b845535d29eb687f5ab135a0819e13ed26025de1f03acfcd306d850065a8
|
7
|
+
data.tar.gz: f08e9b1009bed397d34a7cf88629639d76ba344ca260857baded67fa09920afc1a95aaa20bc6b722f040c8f9cdf8cba985d6790c9bf70a4d4e159e8e42a87e1b
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.DS_Store
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Derrick Reimer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Drip Ruby Bindings
|
2
|
+
|
3
|
+
A Ruby toolkit for the [Drip](https://www.getdrip.com/) API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'drip-ruby'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install drip-ruby
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To begin making requests, spin up a new Drip client:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
client = Drip::Client.new do |c|
|
25
|
+
c.api_key = "YOUR_API_TOKEN"
|
26
|
+
c.account_id = "YOUR_ACCOUNT_ID"
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
You can find your API key [here](https://www.getdrip.com/settings/general)
|
31
|
+
and your account ID [here](https://www.getdrip.com/settings/site).
|
32
|
+
|
33
|
+
Since the Drip client is a flat API client, most API actions are available
|
34
|
+
as methods on the client object. The following methods are currently available:
|
35
|
+
|
36
|
+
| Action | Method |
|
37
|
+
| :------------------------- | :--------------------------------------------------- |
|
38
|
+
| Create/update a subscriber | `#create_or_update_subscriber(email, options = {})` |
|
39
|
+
| Fetch a subscriber | `#subscriber(id_or_email)` |
|
40
|
+
| Subscribe to a campaign | `#subscribe(email, campaign_id, options = {})` |
|
41
|
+
| Unsubscribe | `#unsubscribe(id_or_email, options = {})` |
|
42
|
+
| Apply a tag | `#apply_tag(email, tag)` |
|
43
|
+
| Remove a tag | `#remove_tag(email, tag)` |
|
44
|
+
| Track an event | `#track_event(email, action, properties = {})` |
|
45
|
+
|
46
|
+
|
47
|
+
**Note:** We do not have complete API coverage yet. If we are missing an API method
|
48
|
+
that you need to use in your application, please file an issue and/or open a
|
49
|
+
pull request. [See the official REST API docs](https://www.getdrip.com/docs/rest-api)
|
50
|
+
for a complete API reference.
|
51
|
+
|
52
|
+
## Examples
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
client = Drip::Client.new do |c|
|
56
|
+
c.api_key = "YOUR_API_TOKEN"
|
57
|
+
c.account_id = "YOUR_ACCOUNT_ID"
|
58
|
+
end
|
59
|
+
|
60
|
+
# Fetch a subscriber
|
61
|
+
resp = client.subscriber("foo@example.com")
|
62
|
+
# => <Drip::Response ...>
|
63
|
+
|
64
|
+
resp.success?
|
65
|
+
# => true
|
66
|
+
|
67
|
+
subscriber = resp.subscribers.first
|
68
|
+
subscriber.email
|
69
|
+
# => "foo@example.com"
|
70
|
+
```
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it ( https://github.com/DripEmail/drip-ruby/fork )
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/drip-ruby.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'drip/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "drip-ruby"
|
8
|
+
spec.version = Drip::VERSION
|
9
|
+
spec.authors = ["Derrick Reimer"]
|
10
|
+
spec.email = ["derrickreimer@gmail.com"]
|
11
|
+
spec.summary = %q{A Ruby gem for interacting with the Drip API}
|
12
|
+
spec.description = %q{A simple wrapper for the Drip API}
|
13
|
+
spec.homepage = "http://github.com/DripEmail/drip-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "shoulda-context", "~> 1.0"
|
24
|
+
spec.add_development_dependency "mocha"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "faraday"
|
27
|
+
spec.add_runtime_dependency "faraday_middleware"
|
28
|
+
spec.add_runtime_dependency "json"
|
29
|
+
end
|
data/lib/drip.rb
ADDED
data/lib/drip/client.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require "drip/response"
|
2
|
+
require "drip/client/subscribers"
|
3
|
+
require "drip/client/tags"
|
4
|
+
require "drip/client/events"
|
5
|
+
require "faraday"
|
6
|
+
require "faraday_middleware"
|
7
|
+
require "json"
|
8
|
+
|
9
|
+
module Drip
|
10
|
+
class Client
|
11
|
+
include Subscribers
|
12
|
+
include Tags
|
13
|
+
include Events
|
14
|
+
|
15
|
+
attr_accessor :api_key, :account_id
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
yield(self) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def generate_resource(key, *args)
|
22
|
+
{ key => args }
|
23
|
+
end
|
24
|
+
|
25
|
+
def content_type
|
26
|
+
'application/vnd.api+json'
|
27
|
+
end
|
28
|
+
|
29
|
+
def get(url, options = {})
|
30
|
+
build_response do
|
31
|
+
connection.get do |req|
|
32
|
+
req.url url
|
33
|
+
req.params = options
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def post(url, options = {})
|
39
|
+
build_response do
|
40
|
+
connection.post do |req|
|
41
|
+
req.url url
|
42
|
+
req.body = options.to_json
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def put(url, options = {})
|
48
|
+
build_response do
|
49
|
+
connection.put do |req|
|
50
|
+
req.url url
|
51
|
+
req.body = options.to_json
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete(url, options = {})
|
57
|
+
build_response do
|
58
|
+
connection.delete do |req|
|
59
|
+
req.url url
|
60
|
+
req.body = options.to_json
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_response(&block)
|
66
|
+
response = yield
|
67
|
+
Drip::Response.new(response.status, response.body)
|
68
|
+
end
|
69
|
+
|
70
|
+
def connection
|
71
|
+
@connection ||= Faraday.new do |f|
|
72
|
+
f.adapter :net_http
|
73
|
+
f.url_prefix = "https://api.getdrip.com/v2/"
|
74
|
+
f.headers['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
|
75
|
+
f.headers['Content-Type'] = content_type
|
76
|
+
f.headers['Accept'] = "*/*"
|
77
|
+
f.basic_auth api_key, ""
|
78
|
+
f.response :json, :content_type => /\bjson$/
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "cgi"
|
2
|
+
|
3
|
+
module Drip
|
4
|
+
class Client
|
5
|
+
module Events
|
6
|
+
# Public: Track an event.
|
7
|
+
#
|
8
|
+
# email - Required. The String email address of the subscriber.
|
9
|
+
# action - Required. The String event action.
|
10
|
+
# properties - Optional. A Hash of event properties.
|
11
|
+
#
|
12
|
+
# Returns a Drip::Response.
|
13
|
+
# See https://www.getdrip.com/docs/rest-api#record_event
|
14
|
+
def track_event(email, action, properties = {})
|
15
|
+
data = { "email" => email, "action" => action, "properties" => properties }
|
16
|
+
post "#{account_id}/events", generate_resource("events", data)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "cgi"
|
2
|
+
|
3
|
+
module Drip
|
4
|
+
class Client
|
5
|
+
module Subscribers
|
6
|
+
# Public: Fetch a subscriber.
|
7
|
+
#
|
8
|
+
# id_or_email - Required. The String id or email address of the subscriber.
|
9
|
+
#
|
10
|
+
# Returns a Drip::Response.
|
11
|
+
# See https://www.getdrip.com/docs/rest-api#fetch_subscriber
|
12
|
+
def subscriber(id_or_email)
|
13
|
+
get "#{account_id}/subscribers/#{CGI.escape id_or_email}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# Public: Create or update a subscriber.
|
17
|
+
#
|
18
|
+
# options - A Hash of options.
|
19
|
+
# - email - Required. The String subscriber email address.
|
20
|
+
# - new_email - Optional. A new email address for the subscriber.
|
21
|
+
# If provided and a subscriber with the email above
|
22
|
+
# does not exist, this address will be used to
|
23
|
+
# create a new subscriber.
|
24
|
+
# - time_zone - Optional. The subscriber's time zone (in Olsen
|
25
|
+
# format). Defaults to Etc/UTC.
|
26
|
+
# - custom_fields - Optional. A Hash of custom field data.
|
27
|
+
# - tags - Optional. An Array of tags.
|
28
|
+
#
|
29
|
+
# Returns a Drip::Response.
|
30
|
+
# See https://www.getdrip.com/docs/rest-api#create_or_update_subscriber
|
31
|
+
def create_or_update_subscriber(email, options = {})
|
32
|
+
data = options.merge(:email => email)
|
33
|
+
post "#{account_id}/subscribers", generate_resource("subscribers", data)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Public: Unsubscribe a subscriber globally or from a specific campaign.
|
37
|
+
#
|
38
|
+
# id_or_email - Required. The String id or email address of the subscriber.
|
39
|
+
# options - A Hash of options.
|
40
|
+
# - campaign_id - Optional. The campaign from which to
|
41
|
+
# unsubscribe the subscriber. Defaults to all.
|
42
|
+
#
|
43
|
+
# Returns a Drip::Response.
|
44
|
+
# See https://www.getdrip.com/docs/rest-api#unsubscribe
|
45
|
+
def unsubscribe(id_or_email, options = {})
|
46
|
+
url = "#{account_id}/subscribers/#{CGI.escape id_or_email}/unsubscribe"
|
47
|
+
url += options[:campaign_id] ? "?campaign_id=#{options[:campaign_id]}" : ""
|
48
|
+
post url
|
49
|
+
end
|
50
|
+
|
51
|
+
# Public: Subscribe to a campaign.
|
52
|
+
#
|
53
|
+
# email - Required. The String email address of the subscriber.
|
54
|
+
# campaign_id - Required. The String campaign id.
|
55
|
+
# options - Optional. A Hash of options.
|
56
|
+
# - double_optin - Optional. If true, the double opt-in confirmation
|
57
|
+
# email is sent; if false, the confirmation
|
58
|
+
# email is skipped. Defaults to the value set
|
59
|
+
# on the campaign.
|
60
|
+
# - starting_email_index - Optional. The index (zero-based) of
|
61
|
+
# the email to send first. Defaults to 0.
|
62
|
+
# - time_zone - Optional. The subscriber's time zone (in Olsen
|
63
|
+
# format). Defaults to Etc/UTC.
|
64
|
+
# - custom_fields - Optional. A Hash of custom field data.
|
65
|
+
# - tags - Optional. An Array of tags.
|
66
|
+
# - reactivate_if_removed - Optional. If true, re-subscribe
|
67
|
+
# the subscriber to the campaign if there
|
68
|
+
# is a removed subscriber in Drip with the same
|
69
|
+
# email address; otherwise, respond with
|
70
|
+
# 422 Unprocessable Entity. Defaults to true.
|
71
|
+
#
|
72
|
+
# Returns a Drip::Response.
|
73
|
+
# See https://www.getdrip.com/docs/rest-api#subscribe
|
74
|
+
def subscribe(email, campaign_id, options = {})
|
75
|
+
data = options.merge("email" => email)
|
76
|
+
url = "#{account_id}/campaigns/#{campaign_id}/subscribers"
|
77
|
+
post url, generate_resource("subscribers", data)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "cgi"
|
2
|
+
|
3
|
+
module Drip
|
4
|
+
class Client
|
5
|
+
module Tags
|
6
|
+
# Public: Apply a tag to a subscriber.
|
7
|
+
#
|
8
|
+
# email - The String email address of the subscriber.
|
9
|
+
# tag - The String tag to apply.
|
10
|
+
#
|
11
|
+
# Returns a Drip::Response.
|
12
|
+
# See https://www.getdrip.com/docs/rest-api#apply_tag
|
13
|
+
def apply_tag(email, tag)
|
14
|
+
data = { "email" => email, "tag" => tag }
|
15
|
+
post "#{account_id}/tags", generate_resource("tags", data)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Public: Remove a tag from a subscriber.
|
19
|
+
#
|
20
|
+
# email - The String email address of the subscriber.
|
21
|
+
# tag - The String tag to remove.
|
22
|
+
#
|
23
|
+
# Returns a Drip::Response.
|
24
|
+
# See https://www.getdrip.com/docs/rest-api#remove_tag
|
25
|
+
def remove_tag(email, tag)
|
26
|
+
delete "#{account_id}/subscribers/#{CGI.escape email}/tags/#{CGI.escape tag}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Drip
|
2
|
+
class Collection
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
attr_reader :raw_items, :items
|
6
|
+
|
7
|
+
def initialize(raw_items)
|
8
|
+
@raw_items = raw_items.dup.freeze
|
9
|
+
@items = parse_items
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.collection_name
|
13
|
+
"resources"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.resource_name
|
17
|
+
"resource"
|
18
|
+
end
|
19
|
+
|
20
|
+
def item_class
|
21
|
+
@item_class ||= Drip::Resources.
|
22
|
+
find_class(self.class.resource_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
def parse_items
|
26
|
+
raw_items.map do |raw_item|
|
27
|
+
item_class.new(raw_item)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def singular?
|
32
|
+
items.length < 2
|
33
|
+
end
|
34
|
+
|
35
|
+
def each(&block)
|
36
|
+
items.each { |item| yield(item) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|