Skip to content

Commit

Permalink
Catch up with API capabilities by adding new objects (coupons, letter…
Browse files Browse the repository at this point in the history
…s, notes, tasks, tax rates, text messages) and endpoints (voids, subscription previews, invoice consolidation, performing charges) (#14)
  • Loading branch information
adam-invoiced authored and Jared King committed Nov 11, 2019
1 parent 8c2fe4c commit 60b1cb1
Show file tree
Hide file tree
Showing 24 changed files with 707 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Gemfile.lock
example.rb
/invoiced-*.gem
/coverage
/coverage
.DS_Store
12 changes: 11 additions & 1 deletion lib/invoiced.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@
require 'invoiced/attachment'
require 'invoiced/catalog_item'
require 'invoiced/contact'
require 'invoiced/coupon'
require 'invoiced/credit_note'
require 'invoiced/customer'
require 'invoiced/email'
require 'invoiced/estimate'
require 'invoiced/event'
require 'invoiced/file'
require 'invoiced/invoice'
require 'invoiced/letter'
require 'invoiced/line_item'
require 'invoiced/note'
require 'invoiced/payment_plan'
require 'invoiced/plan'
require 'invoiced/subscription'
require 'invoiced/task'
require 'invoiced/tax_rate'
require 'invoiced/text_message'
require 'invoiced/transaction'

module Invoiced
Expand All @@ -43,7 +49,7 @@ class Client
ReadTimeout = 80

attr_reader :api_key, :api_url, :sandbox, :sso_key
attr_reader :CatalogItem, :CreditNote, :Customer, :Estimate, :Event, :File, :Invoice, :Plan, :Subscription, :Transaction
attr_reader :CatalogItem, :Coupon, :CreditNote, :Customer, :Estimate, :Event, :File, :Invoice, :Note, :Plan, :Subscription, :Task, :TaxRate, :Transaction

def initialize(api_key, sandbox=false, sso_key=false)
@api_key = api_key
Expand All @@ -53,14 +59,18 @@ def initialize(api_key, sandbox=false, sso_key=false)

# Object endpoints
@CatalogItem = Invoiced::CatalogItem.new(self)
@Coupon = Invoiced::Coupon.new(self)
@CreditNote = Invoiced::CreditNote.new(self)
@Customer = Invoiced::Customer.new(self)
@Estimate = Invoiced::Estimate.new(self)
@Event = Invoiced::Event.new(self)
@File = Invoiced::File.new(self)
@Invoice = Invoiced::Invoice.new(self)
@Note = Invoiced::Note.new(self)
@Plan = Invoiced::Plan.new(self)
@Subscription = Invoiced::Subscription.new(self)
@Task = Invoiced::Task.new(self)
@TaxRate = Invoiced::TaxRate.new(self)
@Transaction = Invoiced::Transaction.new(self)
end

Expand Down
10 changes: 10 additions & 0 deletions lib/invoiced/coupon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Invoiced
class Coupon < Object
include Invoiced::Operations::List
include Invoiced::Operations::Create
include Invoiced::Operations::Update
include Invoiced::Operations::Delete

OBJECT_NAME = 'coupon'
end
end
8 changes: 8 additions & 0 deletions lib/invoiced/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,13 @@ def attachments(params={})

return attachments, metadata
end

def void()
response = @client.request(:post, "#{self.endpoint()}/void", {})

refresh_from(response[:body].dup.merge({:id => self.id}))

return response[:code] == 200
end
end
end
29 changes: 29 additions & 0 deletions lib/invoiced/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ def send_statement(params={}, opts={})
Util.build_objects(email, response[:body])
end

def send_statement_sms(params={}, opts={})
response = @client.request(:post, "#{self.endpoint()}/text_messages", params, opts)

# build text message objects
text_message = TextMessage.new(@client)
Util.build_objects(text_message, response[:body])
end

def send_statement_letter(params={}, opts={})
response = @client.request(:post, "#{self.endpoint()}/letters", params, opts)

# build letter objects
letter = Letter.new(@client)
Util.build_objects(letter, response[:body])
end

def balance
response = @client.request(:get, "#{self.endpoint()}/balance")

Expand All @@ -31,12 +47,25 @@ def line_items()
line.set_endpoint_base(self.endpoint())
end

def list_notes()
note = Note.new(@client)
note.set_endpoint_base(self.endpoint())
end

def invoice(params={}, opts={})
response = @client.request(:post, "#{self.endpoint()}/invoices", params, opts)

# build invoice object
invoice = Invoice.new(@client)
Util.convert_to_object(invoice, response[:body])
end

def consolidate_invoices(params={})
response = @client.request(:post, "#{self.endpoint()}/consolidate_invoices", params)

# build invoice object
invoice = Invoice.new(@client)
Util.convert_to_object(invoice, response[:body])
end
end
end
8 changes: 8 additions & 0 deletions lib/invoiced/estimate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,13 @@ def attachments(params={})

return attachments, metadata
end

def void()
response = @client.request(:post, "#{self.endpoint()}/void", {})

refresh_from(response[:body].dup.merge({:id => self.id}))

return response[:code] == 200
end
end
end
32 changes: 31 additions & 1 deletion lib/invoiced/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ def send(params={}, opts={})
Util.build_objects(email, response[:body])
end

def send_sms(params={}, opts={})
response = @client.request(:post, "#{self.endpoint()}/text_messages", params, opts)

# build text message objects
text_message = TextMessage.new(@client)
Util.build_objects(text_message, response[:body])
end

def send_letter(params={}, opts={})
response = @client.request(:post, "#{self.endpoint()}/letters", params, opts)

# build letter objects

letter = Letter.new(@client)
Util.build_objects(letter, response[:body])
end

def pay(opts={})
response = @client.request(:post, "#{self.endpoint()}/pay", {}, opts)

Expand All @@ -33,7 +50,7 @@ def attachments(params={})
if !attachment.has_key?(:id)
attachment[:id] = attachment[:file][:id]
end
end
end

# build objects
attachment = Attachment.new(@client)
Expand All @@ -49,5 +66,18 @@ def payment_plan()
paymentPlan = PaymentPlan.new(@client)
paymentPlan.set_endpoint_base(self.endpoint())
end

def notes()
note = Note.new(@client)
note.set_endpoint_base(self.endpoint())
end

def void()
response = @client.request(:post, "#{self.endpoint()}/void", {})

refresh_from(response[:body].dup.merge({:id => self.id}))

return response[:code] == 200
end
end
end
5 changes: 5 additions & 0 deletions lib/invoiced/letter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Invoiced
class Letter < Object
OBJECT_NAME = 'letter'
end
end
10 changes: 10 additions & 0 deletions lib/invoiced/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Invoiced
class Note < Object
include Invoiced::Operations::List
include Invoiced::Operations::Create
include Invoiced::Operations::Update
include Invoiced::Operations::Delete

OBJECT_NAME = 'note'
end
end
6 changes: 6 additions & 0 deletions lib/invoiced/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ class Subscription < Object
def cancel
delete
end

def preview(params={}, opts={})
response = @client.request(:post, "/subscriptions/preview", params, opts)

response[:body]
end
end
end
10 changes: 10 additions & 0 deletions lib/invoiced/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Invoiced
class Task < Object
include Invoiced::Operations::List
include Invoiced::Operations::Create
include Invoiced::Operations::Update
include Invoiced::Operations::Delete

OBJECT_NAME = 'task'
end
end
10 changes: 10 additions & 0 deletions lib/invoiced/tax_rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Invoiced
class TaxRate < Object
include Invoiced::Operations::List
include Invoiced::Operations::Create
include Invoiced::Operations::Update
include Invoiced::Operations::Delete

OBJECT_NAME = 'tax_rate'
end
end
5 changes: 5 additions & 0 deletions lib/invoiced/text_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Invoiced
class TextMessage < Object
OBJECT_NAME = 'text_message'
end
end
6 changes: 6 additions & 0 deletions lib/invoiced/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@ def refund(params={}, opts={})

Util.convert_to_object(self, response[:body])
end

def initiate_charge(params={}, opts={})
response = @client.request(:post, "/charges", params, opts)

Util.convert_to_object(self, response[:body])
end
end
end
90 changes: 90 additions & 0 deletions test/invoiced/coupon_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require File.expand_path('../../test_helper', __FILE__)

module Invoiced
class CouponTest < Test::Unit::TestCase
should "return the api endpoint" do
coupon = Coupon.new(@client, "test")
assert_equal('/coupons/test', coupon.endpoint())
end

should "create a coupon" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(201)
mockResponse.stubs(:body).returns('{"id":"test","name":"Test Coupon"}')
mockResponse.stubs(:headers).returns({})

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

coupon = @client.Coupon.create({:name => "Test Coupon"})

assert_instance_of(Invoiced::Coupon, coupon)
assert_equal("test", coupon.id)
assert_equal('Test Coupon', coupon.name)
end

should "retrieve a coupon" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(200)
mockResponse.stubs(:body).returns('{"id":"test","name":"Test Coupon"}')
mockResponse.stubs(:headers).returns({})

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

coupon = @client.Coupon.retrieve("test")

assert_instance_of(Invoiced::Coupon, coupon)
assert_equal("test", coupon.id)
assert_equal('Test Coupon', coupon.name)
end

should "not update a coupon when no params" do
coupon = Coupon.new(@client, "test")
assert_false(coupon.save)
end

should "update a coupon" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(200)
mockResponse.stubs(:body).returns('{"id":"test","name":"Testing Coupon"}')
mockResponse.stubs(:headers).returns({})

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

coupon = Coupon.new(@client, "test")
coupon.name = "Testing Coupon"
assert_true(coupon.save)

assert_equal(coupon.name, "Testing Coupon")
end

should "list all coupons" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(200)
mockResponse.stubs(:body).returns('[{"id":"test","name":"Test Coupon"}]')
mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '<https://api.invoiced.com/catalog_items?per_page=25&page=1>; rel="self", <https://api.invoiced.com/catalog_items?per_page=25&page=1>; rel="first", <https://api.invoiced.com/catalog_items?per_page=25&page=1>; rel="last"')

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

coupons, metadata = @client.Coupon.list

assert_instance_of(Array, coupons)
assert_equal(1, coupons.length)
assert_equal("test", coupons[0].id)

assert_instance_of(Invoiced::List, metadata)
assert_equal(15, metadata.total_count)
end

should "delete a coupon" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(204)
mockResponse.stubs(:body).returns('')
mockResponse.stubs(:headers).returns({})

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

coupon = Coupon.new(@client, "test")
assert_true(coupon.delete)
end
end
end
14 changes: 14 additions & 0 deletions test/invoiced/credit_note_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,19 @@ class CreditNoteTest < Test::Unit::TestCase
assert_instance_of(Invoiced::List, metadata)
assert_equal(10, metadata.total_count)
end

should "void a credit note" do
mockResponse = mock('RestClient::Response')
mockResponse.stubs(:code).returns(200)
mockResponse.stubs(:body).returns('{"id":123,"status":"voided"}')
mockResponse.stubs(:headers).returns({})

RestClient::Request.any_instance.expects(:execute).returns(mockResponse)

credit_note = CreditNote.new(@client, 123)
assert_true(credit_note.void)

assert_equal(credit_note.status, 'voided')
end
end
end
Loading

0 comments on commit 60b1cb1

Please sign in to comment.