forked from DripEmail/drip-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'ssbean/purchases'
- Loading branch information
Showing
9 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters