diff --git a/.gitignore b/.gitignore index b642bc1..694cdb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# byebug generated +.byebug_history + # rcov generated coverage coverage.data diff --git a/lib/quaderno-ruby.rb b/lib/quaderno-ruby.rb index 0e0ae26..12d40db 100644 --- a/lib/quaderno-ruby.rb +++ b/lib/quaderno-ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Quaderno end @@ -10,4 +12,4 @@ class Quaderno require 'quaderno-ruby/collection' %w(block crud deliver payment retrieve).each { |filename| require "quaderno-ruby/behavior/#{filename}" } -%w(base contact item invoice receipt credit income estimate expense recurring document_item report report_request evidence payment webhook tax checkout_session).each { |filename| require "quaderno-ruby/#{ filename }" } +%w(base contact item transaction invoice receipt credit income estimate expense recurring document_item report report_request evidence payment webhook tax checkout_session).each { |filename| require "quaderno-ruby/#{filename}" } diff --git a/lib/quaderno-ruby/base.rb b/lib/quaderno-ruby/base.rb index ac62690..d6d534b 100644 --- a/lib/quaderno-ruby/base.rb +++ b/lib/quaderno-ruby/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'httparty' require 'json' diff --git a/lib/quaderno-ruby/behavior/crud.rb b/lib/quaderno-ruby/behavior/crud.rb index 23bc846..f11f54b 100644 --- a/lib/quaderno-ruby/behavior/crud.rb +++ b/lib/quaderno-ruby/behavior/crud.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Quaderno::Behavior module Crud @@ -69,7 +71,7 @@ def find(id, options = {}) def create(params = {}) authentication = get_authentication(params.merge(api_model: api_model)) - params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s } + params.dup.delete_if { |k, _| %w[auth_token access_token api_url mode api_model].include? k.to_s } response = post("#{authentication[:url]}#{api_model.api_path}.json", body: params.to_json, @@ -91,7 +93,7 @@ def create(params = {}) def update(id, params = {}) authentication = get_authentication(params.merge(api_model: api_model)) - params = params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s } + params = params.dup.delete_if { |k, _| %w[auth_token access_token api_url mode api_model].include? k.to_s } response = put("#{authentication[:url]}#{api_model.api_path}/#{id}.json", body: params.to_json, @@ -114,7 +116,7 @@ def update(id, params = {}) def delete(id, options = {}) authentication = get_authentication(options.merge(api_model: api_model)) - response = HTTParty.delete("#{authentication[:url]}#{ api_model.api_path }/#{ id }.json", + response = HTTParty.delete("#{authentication[:url]}#{api_model.api_path}/#{id}.json", basic_auth: authentication[:basic_auth], headers: default_headers.merge(authentication[:headers]) ) diff --git a/lib/quaderno-ruby/transaction.rb b/lib/quaderno-ruby/transaction.rb new file mode 100644 index 0000000..1a61d79 --- /dev/null +++ b/lib/quaderno-ruby/transaction.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Quaderno::Transaction < Quaderno::Base + api_model Quaderno::Transaction + api_path 'transactions' + is_a_document? true + + class << self + undef :all, :find, :update, :delete + end +end diff --git a/lib/quaderno-ruby/version.rb b/lib/quaderno-ruby/version.rb index 160187c..6c7ecc6 100644 --- a/lib/quaderno-ruby/version.rb +++ b/lib/quaderno-ruby/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Quaderno - VERSION = "2.1.3" + VERSION = "2.2.0" end diff --git a/quaderno.gemspec b/quaderno.gemspec index 71eda56..14c58bd 100644 --- a/quaderno.gemspec +++ b/quaderno.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |spec| spec.add_dependency('httparty', '~> 0.21.0') spec.add_development_dependency('rdoc', '>= 6.3.1') spec.add_development_dependency('activesupport', '~> 4.2.0') - spec.add_development_dependency('webmock', '~> 1.22.6') + spec.add_development_dependency('webmock', '~> 3.18') spec.add_development_dependency('vcr', '>= 0') spec.add_development_dependency('bundler', '~> 2.2') spec.add_development_dependency('rake', '>= 12.3.3') diff --git a/spec/helper.rb b/spec/helper.rb index d6802df..9ec4e57 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) $LOAD_PATH.unshift(File.dirname(__FILE__)) @@ -17,4 +19,3 @@ TEST_KEY = 'sk_test_bMz9mJJ5bZnWPwWGuV8y' TEST_OAUTH_ACCESS_TOKEN = 'afa16c7478f0ba3be222e627c2571d4dd5dca47924996b13a3af377feca47ff0' OLDEST_SUPPORTED_API_VERSION = 20160602 - diff --git a/spec/unit/test_quaderno_transaction.rb b/spec/unit/test_quaderno_transaction.rb new file mode 100644 index 0000000..8c23c99 --- /dev/null +++ b/spec/unit/test_quaderno_transaction.rb @@ -0,0 +1,97 @@ +# frozen_string_literal: true + +require 'helper' + +describe Quaderno::Transaction do + context 'A user with an authentication token' do + before(:each) do + Quaderno::Base.configure do |config| + config.auth_token = TEST_KEY + config.url = TEST_URL + config.api_version = nil + end + end + + it 'should create a sale transaction' do + sale_params = { + type: 'sale', customer: { first_name: 'John', last_name: 'Smith', email: 'an_email@example.com' }, + evidence: { billing_country: 'US', ip_address: '255.255.255.255', bank_country: 'US' }, + currency: 'USD', + items: [ + { + description: 'Awesome ebook', quantity: 1, amount: 19, + tax: { + city: nil, country: 'US', county: nil, currency: 'USD', name: nil, notes: nil, + notice: '', product_type: 'service', rate: 0.0, region: 'NY', tax_behavior: 'exclusive', + tax_code: 'ebook', taxable_part: nil, import: true, subtotal: nil, tax_amount: nil, total_amount: nil + } + }, + { + description: 'Awesome SaaS', quantity: 1, amount: 89, + tax: { + city: 'NEW YORK', country: 'US', county: 'NEW YORK', currency: 'USD', name: 'Sales tax', notes: nil, + notice: '', product_type: 'service', rate: 8.875, region: 'NY', tax_behavior: 'exclusive', + tax_code: 'saas', taxable_part: 100.0, import: true, subtotal: nil, tax_amount: nil, total_amount: nil + } + } + ], + payment: { method: 'credit_card', processor: 'processor12345', processor_id: 'txn_12345' }, + processor: 'processor12345', processor_id: 'txn_12345', po_number: 'PO_12345', + tags: 'tag-a,tag-b,tag-c', + custom_metadata: { anything_you_want: 'extra info' } + } + + VCR.use_cassette('new sale') do + @transaction = Quaderno::Transaction.create(sale_params) + end + + expect(@transaction.is_a?(Quaderno::Transaction)).to be true + expect(@transaction.type).to eq 'Invoice' + expect(@transaction.currency).to eq sale_params[:currency] + expect(@transaction.items.count).to eq sale_params[:items].count + expect(@transaction.total_cents).to eq(sale_params[:items].sum { |item| item[:amount] * 100 }) + expect(@transaction.contact.full_name).to eq "#{sale_params[:customer][:first_name]} #{sale_params[:customer][:last_name]}" + end + + it 'should create a refund transaction' do + refund_params = { + type: 'refund', customer: { first_name: 'John', last_name: 'Smith', email: 'an_email@example.com' }, + evidence: { billing_country: 'US', ip_address: '255.255.255.255', bank_country: 'US' }, + currency: 'USD', + items: [ + { + description: 'Awesome ebook', quantity: 1, amount: 19, + tax: { + city: nil, country: 'US', county: nil, currency: 'USD', name: nil, notes: nil, + notice: '', product_type: 'service', rate: 0.0, region: 'NY', tax_behavior: 'exclusive', + tax_code: 'ebook', taxable_part: nil, import: true, subtotal: nil, tax_amount: nil, total_amount: nil + } + }, + { + description: 'Awesome SaaS', quantity: 1, amount: 89, + tax: { + city: 'NEW YORK', country: 'US', county: 'NEW YORK', currency: 'USD', name: 'Sales tax', notes: nil, + notice: '', product_type: 'service', rate: 8.875, region: 'NY', tax_behavior: 'exclusive', + tax_code: 'saas', taxable_part: 100.0, import: true, subtotal: nil, tax_amount: nil, total_amount: nil + } + } + ], + payment: { method: 'credit_card', processor: 'processor12345', processor_id: 'txn_12345' }, + processor: 'processor12345', processor_id: 'txn_12345', po_number: 'PO_12345', + tags: 'tag-a,tag-b,tag-c', + custom_metadata: { anything_you_want: 'extra info' } + } + + VCR.use_cassette('new refund') do + @transaction = Quaderno::Transaction.create(refund_params) + end + + expect(@transaction.is_a?(Quaderno::Transaction)).to be true + expect(@transaction.type).to eq 'Credit' + expect(@transaction.currency).to eq refund_params[:currency] + expect(@transaction.items.count).to eq refund_params[:items].count + expect(@transaction.total_cents).to eq(refund_params[:items].sum { |item| item[:amount] * 100 }) + expect(@transaction.contact.full_name).to eq "#{refund_params[:customer][:first_name]} #{refund_params[:customer][:last_name]}" + end + end +end