Permalink
Browse files

Initial working version

  • Loading branch information...
adamfortuna committed Feb 3, 2012
1 parent 661f270 commit c3e91f4b5c9ea845415114b953710a55808a7bdf
View
@@ -1,6 +1,8 @@
source 'http://rubygems.org'
gemspec
+gem "hashie", :git => "https://github.com/intridea/hashie.git"
+
group :development, :test do
gem 'rake'
end
View
@@ -1,10 +1,16 @@
+GIT
+ remote: https://github.com/intridea/hashie.git
+ revision: 6728ed259b4e8e679901b1d64e1d791f28ec186a
+ specs:
+ hashie (2.0.0.beta)
+
PATH
remote: .
specs:
- Avalara (0.0.1)
+ avalara (0.0.1)
hashie
httparty
- multi_json
+ multi_json (~> 1.0.4)
GEM
remote: http://rubygems.org/
@@ -13,15 +19,18 @@ GEM
i18n (~> 0.6)
multi_json (~> 1.0)
addressable (2.2.6)
+ archive-tar-minitar (0.5.2)
+ columnize (0.3.6)
crack (0.3.1)
diff-lcs (1.1.3)
factory_girl (2.5.0)
activesupport
- hashie (1.2.0)
httparty (0.8.1)
multi_json
multi_xml
i18n (0.6.0)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
multi_json (1.0.4)
multi_xml (0.4.1)
rake (0.9.2.2)
@@ -33,6 +42,16 @@ GEM
rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.8.0)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
vcr (1.11.3)
webmock (1.7.10)
addressable (~> 2.2, > 2.2.5)
@@ -42,9 +61,11 @@ PLATFORMS
ruby
DEPENDENCIES
- Avalara!
+ avalara!
factory_girl
+ hashie!
rake
rspec
+ ruby-debug19
vcr
webmock
View
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
require 'avalara/version'
Gem::Specification.new do |s|
- s.name = 'Avalara'
+ s.name = 'avalara'
s.version = Avalara::VERSION
s.authors = ['Adam Fortuna']
s.email = ['adam@envylabs.com']
@@ -18,14 +18,11 @@ Gem::Specification.new do |s|
s.add_dependency 'hashie'
s.add_dependency 'httparty'
- s.add_dependency 'multi_json'
+ s.add_dependency 'multi_json', '~> 1.0.4'
s.add_development_dependency 'vcr'
s.add_development_dependency 'webmock'
s.add_development_dependency 'rspec'
s.add_development_dependency 'factory_girl'
- # s.add_development_dependency 'infinity_test'
- # s.add_development_dependency 'factory_girl', '~> 2.0'
- # s.add_development_dependency 'rack', '~> 1.2'
- # s.add_development_dependency 'watchr'
+ s.add_development_dependency 'ruby-debug19'
end
View
@@ -4,9 +4,9 @@
require 'avalara/errors'
require 'avalara/configuration'
-require 'avalara/parser'
require 'avalara/api'
+require 'avalara/types'
require 'avalara/request'
require 'avalara/response'
@@ -69,7 +69,7 @@ def self.geographical_tax(latitude, longitude, sales_amount)
end
def self.get_tax(invoice)
- uri = [configuration.endpoint, configuration.version, 'tax', 'get'].join('/')
+ uri = [endpoint, version, 'tax', 'get'].join('/')
response = API.post(uri,
:body => invoice.to_json,
@@ -80,15 +80,17 @@ def self.get_tax(invoice)
return case response.code
when 200..299
Response::Invoice.new(response)
- when 400..499
- Error.new(response)
- when 500.599
- Error.new(response)
+ when 400..599
+ raise ApiError.new(Response::Invoice.new(response))
else
- Error.new(response)
+ raise ApiError.new(response)
end
- rescue Timeout::Error
- Error.new("Timeout")
+ rescue Timeout::Error => e
+ raise TimeoutError.new(e)
+ rescue ApiError => e
+ raise e
+ rescue Exception => e
+ raise Error.new(e)
end
private
View
@@ -1,5 +1,6 @@
# encoding: UTF-8
require 'httparty'
+require 'avalara/parser'
module Avalara
class API
View
@@ -2,7 +2,7 @@
module Avalara
Error = Class.new(StandardError)
- InvalidObjectError = Class.new(Error)
TimeoutError = Class.new(Error)
NotImplementedError = Class.new(Error)
+ ApiError = Class.new(Error)
end
View
@@ -6,5 +6,6 @@ module Request
autoload :DetailLevel, 'avalara/request/detail_level'
autoload :Invoice, 'avalara/request/invoice'
autoload :Line, 'avalara/request/line'
+ autoload :Message, 'avalara/request/message'
end
end
@@ -1,10 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Request
- class Address < ::Hashie::Trash
-
+ class Address < Avalara::Types::Stash
property :AddressCode, :from => :address_code, :required => true
property :Line1, :from => :line_1
property :Line2, :from => :line_2
@@ -1,10 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Request
- class DetailLevel < ::Hashie::Trash
-
+ class DetailLevel < Avalara::Types::Stash
property :Line, :from => :line
property :Summary, :from => :summary
property :Document, :from => :document
@@ -1,12 +1,12 @@
# encoding: UTF-8
-require 'hashie/trash'
require 'multi_json'
module Avalara
module Request
# Same as GetTaxRequest
- class Invoice < ::Hashie::Trash
-
+ class Invoice < Avalara::Types::Stash
+ coerce_key :DocDate, Avalara::Types::Date
+
# Set outgoing
property :CustomerCode, :from => :customer_code
property :DocDate, :from => :doc_date
@@ -24,6 +24,20 @@ class Invoice < ::Hashie::Trash
property :Addresses, :from => :addresses
property :ReferenceCode, :from => :reference_code
+ def addresses=(addresses)
+ self.Addresses = []
+ addresses.each do |address|
+ self.Addresses << Address.new(address)
+ end
+ end
+
+ def lines=(lines)
+ self.Lines = []
+ lines.each do |line|
+ self.Lines << Line.new(line)
+ end
+ end
+
def to_json
MultiJson.encode(self.to_hash, :pretty => true)
end
@@ -1,10 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Request
- class Line < ::Hashie::Trash
-
+ class Line < Avalara::Types::Stash
property :LineNo, :from => :line_no, :required => true
property :DestinationCode, :from => :destination_code, :required => true
property :OriginCode, :from => :origin_code, :required => true
View
@@ -3,6 +3,7 @@
module Avalara
module Response
autoload :Invoice, 'avalara/response/invoice'
+ autoload :Message, 'avalara/response/message'
autoload :TaxLine, 'avalara/response/tax_line'
autoload :TaxDetail, 'avalara/response/tax_detail'
autoload :TaxAddress, 'avalara/response/tax_address'
@@ -1,11 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Response
- class Invoice < ::Hashie::Trash
-
- # Set outgoing
+ class Invoice < Avalara::Types::Stash
property :doc_code, :from => :DocCode
property :doc_date, :from => :DocDate
property :timestamp, :from => :Timestamp
@@ -19,6 +16,18 @@ class Invoice < ::Hashie::Trash
property :tax_lines, :from => :TaxLines
property :tax_addresses, :from => :TaxAddresses
property :result_code, :from => :ResultCode
+ property :messages, :from => :Messages
+
+ def success?
+ result_code == 'Success'
+ end
+
+ def Messages=(new_messages)
+ self.messages = []
+ new_messages.each do |message|
+ self.messages << Message.new(message)
+ end
+ end
def TaxLines=(lines)
self.tax_lines = []
@@ -0,0 +1,13 @@
+# encoding: UTF-8
+
+module Avalara
+ module Response
+ class Message < Avalara::Types::Stash
+ property :summary, :from => :Summary
+ property :details, :from => :Details
+ property :refers_to, :from => :RefersTo
+ property :severity, :from => :Severity
+ property :source, :from => :Source
+ end
+ end
+end
@@ -1,9 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Response
- class TaxAddress < ::Hashie::Trash
+ class TaxAddress < Avalara::Types::Stash
property :address, :from => :Address
property :address_code, :from => :AddressCode
property :city, :from => :City
@@ -1,9 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Response
- class TaxDetail < ::Hashie::Trash
+ class TaxDetail < Avalara::Types::Stash
property :taxable, :from => :Taxable
property :rate, :from => :Rate
property :tax, :from => :Tax
@@ -1,9 +1,8 @@
# encoding: UTF-8
-require 'hashie/trash'
module Avalara
module Response
- class TaxLine < ::Hashie::Trash
+ class TaxLine < Avalara::Types::Stash
property :line_no, :from => :LineNo
property :tax_code, :from => :TaxCode
property :taxability, :from => :Taxability
View
@@ -0,0 +1,8 @@
+# encoding: UTF-8
+
+module Avalara
+ module Types
+ autoload :Date, 'avalara/types/date'
+ autoload :Stash, 'avalara/types/stash'
+ end
+end
View
@@ -0,0 +1,10 @@
+module Avalara
+ module Types
+ class Date
+ def self.coerce(object)
+ return object unless object.respond_to?(:strftime)
+ return object.strftime("%Y-%m-%d")
+ end
+ end
+ end
+end
View
@@ -0,0 +1,28 @@
+# encoding: UTF-8
+
+require 'hashie/trash'
+require 'hashie/extensions/coercion'
+
+module Avalara
+ module Types
+ ##
+ # A Stash is a 'suppressing' Hashie::Trash where keys that are not defined
+ # are simply ignored and unavailable to the local object.
+ #
+ # A Stash is useful when you need to read data from another application,
+ # but you only want a predefined subset of the returned data to become
+ # available, locally.
+ #
+ class Stash < ::Hashie::Trash
+ include Hashie::Extensions::Coercion
+
+
+ private
+
+
+ def property_exists?(property)
+ self.class.property?(property.to_sym)
+ end
+ end
+ end
+end
@@ -1,5 +1,5 @@
Factory.define :address, :class => Avalara::Request::Address do |c|
- c.address_code "1"
+ c.address_code 1
c.line_1 "435 Ericksen Avenue Northeast"
c.line_2 "#250"
# c.line_3 "line_3"
Oops, something went wrong.

0 comments on commit c3e91f4

Please sign in to comment.