Skip to content

Commit

Permalink
Merge remote-tracking branch 'ssbean/purchases'
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Dec 27, 2016
2 parents 9f79f57 + 66403e8 commit f22e404
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/drip/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "drip/client/tags"
require "drip/client/events"
require "drip/client/campaigns"
require "drip/client/purchases"
require "faraday"
require "faraday_middleware"
require "json"
Expand All @@ -15,6 +16,7 @@ class Client
include Tags
include Events
include Campaigns
include Purchases

attr_accessor :access_token, :api_key, :account_id

Expand Down
67 changes: 67 additions & 0 deletions lib/drip/client/purchases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "cgi"

module Drip
class Client
module Purchases
# Public: Create a purchase.
#
# options - A Hash of options.
# - amount - Required. The total amount of the purchase in
# cents.
# - properties - Optional. An Object containing properties about
# the order.
# - items - Optional. An Array of objects containing information
# about specific order items.
# - name - Required. The product name.
# - amount - Required. The line total (in
# cents).
# - quantity - Optional. The quantity of the
# item purchased (if omitted,
# defaults to 1).
# - product_id - Optional. A unique identifier
# for the specific product.
# - sku - Optional. The product SKU number.
# - properties - Optional. An Object containing
# properties about the line item.
# - options - Optional. A Hash of additional options:
# - provider - The identifier for the provider from
# which the purchase data was received
# - order_id - A unique identifier for the order
# (generally the primary key generated by the
# order management system).
# - permalink - A URL for the human-readable
# interface to view the order details.
# - occurred_at - The String time at which the event
# occurred in ISO-8601 format. Defaults to the
# current time.
#
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#create_purchase
def create_purchase(email, amount, properties = [], items = [], options = {})
data = options.merge(:amount => amount, :properties => properties, :items => items)
post "#{account_id}/subscribers/#{CGI.escape email}/purchases", generate_resource("purchases", data)
end

# Public: Fetch a list of purchases for a subscriber.
#
# email - The String email address of the subscriber.
#
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#list_purchases
def purchases(email)
get "#{account_id}/subscribers/#{CGI.escape email}/purchases"
end

# Public: Fetch a purchase.
#
# email - The String email address of the subscriber.
# id - The String ID of the purchase
#
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#list_purchases
def purchase(email, id)
get "#{account_id}/subscribers/#{CGI.escape email}/purchases/#{id}"
end
end
end
end
4 changes: 3 additions & 1 deletion lib/drip/collections.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "drip/collections/accounts"
require "drip/collections/subscribers"
require "drip/collections/errors"
require "drip/collections/purchases"

module Drip
module Collections
def self.classes
[
Drip::Accounts,
Drip::Subscribers,
Drip::Errors
Drip::Errors,
Drip::Purchases
]
end

Expand Down
13 changes: 13 additions & 0 deletions lib/drip/collections/purchases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "drip/collection"

module Drip
class Purchases < Collection
def self.collection_name
"purchases"
end

def self.resource_name
"purchase"
end
end
end
4 changes: 3 additions & 1 deletion lib/drip/resources.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require "drip/resources/account"
require "drip/resources/subscriber"
require "drip/resources/error"
require "drip/resources/purchase"

module Drip
module Resources
def self.classes
[
Drip::Account,
Drip::Subscriber,
Drip::Error
Drip::Error,
Drip::Purchase
]
end

Expand Down
9 changes: 9 additions & 0 deletions lib/drip/resources/purchase.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "drip/resource"

module Drip
class Purchase < Resource
def self.resource_name
"purchase"
end
end
end
91 changes: 91 additions & 0 deletions test/drip/client/purchases_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require File.dirname(__FILE__) + '/../../test_helper.rb'

class Drip::Client::PurchasesTest < Drip::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new

@connection = Faraday.new do |builder|
builder.adapter :test, @stubs
end

@client = Drip::Client.new { |c| c.account_id = "12345" }
@client.expects(:connection).at_least_once.returns(@connection)
end

context "#create_purchase" do
setup do
@email = "[email protected]"
@amount = 3900
@properties = [
{
address: '123 Anywhere St'
}
]
@items = [
{
name: 'foo',
amount: 100
},
{
name: 'bar',
amount: 200
}
]
@payload = {
purchases: [{
amount: @amount,
properties: @properties,
items: @items
}]
}.to_json

@response_status = 201
@response_body = stub

@stubs.post "12345/subscribers/#{CGI.escape @email}/purchases", @payload do
[@response_status, {}, @response_body]
end
end

should "send the right request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.create_purchase(@email, @amount, @properties, @items)
end
end

context "#purchases" do
setup do
@email = "[email protected]"
@response_status = 201
@response_body = stub

@stubs.get "12345/subscribers/#{CGI.escape @email}/purchases" do
[@response_status, {}, @response_body]
end
end

should "send the right request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.purchases(@email)
end
end

context "#purchase" do
setup do
@email = "[email protected]"
@id = '23456'
@response_status = 201
@response_body = stub

@stubs.get "12345/subscribers/#{CGI.escape @email}/purchases/#{@id}" do
[@response_status, {}, @response_body]
end
end

should "send the right request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.purchase(@email, @id)
end
end

end
1 change: 1 addition & 0 deletions test/drip/collections_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Drip::CollectionsTest < Drip::TestCase
assert_equal Drip::Subscribers, Drip::Collections.find_class("subscribers")
assert_equal Drip::Accounts, Drip::Collections.find_class("accounts")
assert_equal Drip::Errors, Drip::Collections.find_class("errors")
assert_equal Drip::Purchases, Drip::Collections.find_class("purchases")
end

should "return base collection by default" do
Expand Down
1 change: 1 addition & 0 deletions test/drip/resources_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Drip::ResourcesTest < Drip::TestCase
should "find resources" do
assert_equal Drip::Subscriber, Drip::Resources.find_class("subscriber")
assert_equal Drip::Error, Drip::Resources.find_class("error")
assert_equal Drip::Purchase, Drip::Resources.find_class("purchase")
end

should "return base resource by default" do
Expand Down

0 comments on commit f22e404

Please sign in to comment.