Skip to content

Commit

Permalink
Merge branch 'jimmytang-api_update'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Tang authored and Jimmy Tang committed Feb 23, 2011
2 parents 436ad3a + bb68736 commit b0041e4
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 74 deletions.
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in rshoeboxed.gemspec
gemspec
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -9,7 +9,7 @@ $hoe = Hoe.new('rshoeboxed', RShoeboxed::VERSION) do |p|
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n") p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
p.rubyforge_name = p.name # TODO this is default value p.rubyforge_name = p.name # TODO this is default value
p.extra_deps = [ p.extra_deps = [
['builder','>= 2.1.2'], ['builder','~> 2.1.2'],
] ]
p.extra_dev_deps = [ p.extra_dev_deps = [
['newgem', ">= #{::Newgem::VERSION}"], ['newgem', ">= #{::Newgem::VERSION}"],
Expand Down
4 changes: 1 addition & 3 deletions lib/rshoeboxed/connection.rb
@@ -1,5 +1,3 @@
gem "builder"
require "builder"
require "cgi" require "cgi"
require "net/https" require "net/https"
require 'rexml/document' require 'rexml/document'
Expand Down Expand Up @@ -221,4 +219,4 @@ def append_credentials(xml)
end end
end end
end end
end end
31 changes: 20 additions & 11 deletions lib/rshoeboxed/receipt.rb
Expand Up @@ -4,9 +4,8 @@


module RShoeboxed module RShoeboxed
class Receipt class Receipt
attr_accessor :id, :store, :image_url, :categories attr_accessor :id, :store, :image_url, :categories, :account_currency
attr_reader :sell_date, :created_date, :modified_date, :total attr_reader :sell_date, :created_date, :modified_date, :converted_total

def self.parse(xml) def self.parse(xml)
document = REXML::Document.new(xml) document = REXML::Document.new(xml)
document.elements.collect("//Receipt") do |receipt_element| document.elements.collect("//Receipt") do |receipt_element|
Expand All @@ -17,9 +16,10 @@ def self.parse(xml)
receipt.sell_date = receipt_element.attributes["selldate"] receipt.sell_date = receipt_element.attributes["selldate"]
receipt.created_date = receipt_element.attributes["createdDate"] receipt.created_date = receipt_element.attributes["createdDate"]
receipt.modified_date = receipt_element.attributes["modifiedDate"] receipt.modified_date = receipt_element.attributes["modifiedDate"]
receipt.total = receipt_element.attributes["total"] receipt.converted_total = receipt_element.attributes["convertedTotal"]
receipt.account_currency = receipt_element.attributes["accountCurrency"]
receipt.image_url = receipt_element.attributes["imgurl"] receipt.image_url = receipt_element.attributes["imgurl"]

# Get the categories elements and have Category parse them # Get the categories elements and have Category parse them
category_element = receipt_element.elements["Categories"] category_element = receipt_element.elements["Categories"]
receipt.categories = category_element ? Category.parse(category_element.to_s) : [] receipt.categories = category_element ? Category.parse(category_element.to_s) : []
Expand All @@ -29,12 +29,21 @@ def self.parse(xml)
receipt receipt
end end
end end

def converted_total=(unprocessed_converted_total)
unprocessed_converted_total.gsub!(/[^\d|.]/, "") if unprocessed_converted_total.is_a?(String)
unprocessed_converted_total = BigDecimal.new(unprocessed_converted_total) unless unprocessed_converted_total.is_a?(BigDecimal)
@converted_total = unprocessed_converted_total
end

def total
warn "DEPRECATION WARNING: `total` is deprecated. Please use `converted_total`"
converted_total
end


def total=(total) def total=(unprocessed_converted_total)
total.gsub!(/[^\d|.]/, "") if total.is_a?(String) warn "DEPRECATION WARNING: `total=` is deprecated. Please use `converted_total=`"
total = BigDecimal.new(total) unless total.is_a?(BigDecimal) self.converted_total = unprocessed_converted_total

@total = total
end end


def sell_date=(date) def sell_date=(date)
Expand All @@ -56,7 +65,7 @@ def ==(receipt)
self.id == receipt.id && self.store == receipt.store && self.image_url == receipt.image_url && self.id == receipt.id && self.store == receipt.store && self.image_url == receipt.image_url &&
self.categories == receipt.categories && self.sell_date == receipt.sell_date && self.categories == receipt.categories && self.sell_date == receipt.sell_date &&
self.modified_date == receipt.modified_date && self.created_date == receipt.created_date && self.modified_date == receipt.modified_date && self.created_date == receipt.created_date &&
self.total == receipt.total self.converted_total == receipt.converted_total && self.account_currency == receipt.account_currency
end end
end end
end end
3 changes: 3 additions & 0 deletions lib/rshoeboxed/version.rb
@@ -0,0 +1,3 @@
module RShoeboxed
VERSION = "0.0.8"
end
31 changes: 18 additions & 13 deletions rshoeboxed.gemspec
@@ -1,42 +1,47 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "rshoeboxed/version"


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{rshoeboxed} s.name = %q{rshoeboxed}
s.version = "0.0.7" s.version = RShoeboxed::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Ben Curren"]
s.email = ["ben@outright.com"]
s.homepage = %q{http://github.com/jimmytang/rshoeboxed}
s.summary = %q{Ruby wrapper for the Shoeboxed API.}
s.description = %q{Ruby wrapper for the Shoeboxed API.}

s.rubyforge_project = %q{rshoeboxed}

s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/rshoeboxed.rb", "lib/rshoeboxed/category.rb", "lib/rshoeboxed/connection.rb", "lib/rshoeboxed/list_proxy.rb", "lib/rshoeboxed/parse_error.rb", "lib/rshoeboxed/receipt.rb", "script/console", "script/destroy", "script/generate", "test/fixtures/category_request.xml", "test/fixtures/category_response.xml", "test/fixtures/receipt_info_request.xml", "test/fixtures/receipt_info_response.xml", "test/fixtures/receipt_request.xml", "test/fixtures/receipt_response.xml", "test/fixtures/receipt_response_bad_credentials.xml", "test/fixtures/receipt_response_internal_error.xml", "test/fixtures/receipt_response_restricted_ip.xml", "test/fixtures/receipt_response_unknown_api_call.xml", "test/fixtures/receipt_response_xml_validation.xml", "test/fixtures/receipt_with_category_id_request.xml", "test/test_category.rb", "test/test_connection.rb", "test/test_helper.rb", "test/test_list_proxy.rb", "test/test_receipt.rb"]
s.test_files = ["test/test_category.rb", "test/test_connection.rb", "test/test_helper.rb", "test/test_list_proxy.rb", "test/test_receipt.rb"]
s.require_paths = ["lib"]


s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Ben Curren"]
s.date = %q{2010-02-15} s.date = %q{2010-02-15}
s.description = %q{Ruby wrapper for the Shoeboxed API.}
s.email = ["ben@gobootstrap.com"]
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"] s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/rshoeboxed.rb", "lib/rshoeboxed/category.rb", "lib/rshoeboxed/connection.rb", "lib/rshoeboxed/list_proxy.rb", "lib/rshoeboxed/parse_error.rb", "lib/rshoeboxed/receipt.rb", "script/console", "script/destroy", "script/generate", "test/fixtures/category_request.xml", "test/fixtures/category_response.xml", "test/fixtures/receipt_info_request.xml", "test/fixtures/receipt_info_response.xml", "test/fixtures/receipt_request.xml", "test/fixtures/receipt_response.xml", "test/fixtures/receipt_response_bad_credentials.xml", "test/fixtures/receipt_response_internal_error.xml", "test/fixtures/receipt_response_restricted_ip.xml", "test/fixtures/receipt_response_unknown_api_call.xml", "test/fixtures/receipt_response_xml_validation.xml", "test/fixtures/receipt_with_category_id_request.xml", "test/test_category.rb", "test/test_connection.rb", "test/test_helper.rb", "test/test_list_proxy.rb", "test/test_receipt.rb"]
s.has_rdoc = true s.has_rdoc = true
s.homepage = %q{http://github.com/bcurren/rshoeboxed}
s.rdoc_options = ["--main", "README.rdoc"] s.rdoc_options = ["--main", "README.rdoc"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{rshoeboxed}
s.rubygems_version = %q{1.3.1} s.rubygems_version = %q{1.3.1}
s.summary = %q{Ruby wrapper for the Shoeboxed API.}
s.test_files = ["test/test_category.rb", "test/test_connection.rb", "test/test_helper.rb", "test/test_list_proxy.rb", "test/test_receipt.rb"]


if s.respond_to? :specification_version then if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2 s.specification_version = 2


if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<builder>, [">= 2.1.2"]) s.add_runtime_dependency(%q<builder>, ["~> 2.1.2"])
s.add_development_dependency(%q<newgem>, [">= 1.1.0"]) s.add_development_dependency(%q<newgem>, [">= 1.1.0"])
s.add_development_dependency(%q<mocha>, [">= 0.9.4"]) s.add_development_dependency(%q<mocha>, [">= 0.9.4"])
s.add_development_dependency(%q<hoe>, [">= 1.8.0"]) s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
else else
s.add_dependency(%q<builder>, [">= 2.1.2"]) s.add_dependency(%q<builder>, ["~> 2.1.2"])
s.add_dependency(%q<newgem>, [">= 1.1.0"]) s.add_dependency(%q<newgem>, [">= 1.1.0"])
s.add_dependency(%q<mocha>, [">= 0.9.4"]) s.add_dependency(%q<mocha>, [">= 0.9.4"])
s.add_dependency(%q<hoe>, [">= 1.8.0"]) s.add_dependency(%q<hoe>, [">= 1.8.0"])
end end
else else
s.add_dependency(%q<builder>, [">= 2.1.2"]) s.add_dependency(%q<builder>, ["~> 2.1.2"])
s.add_dependency(%q<newgem>, [">= 1.1.0"]) s.add_dependency(%q<newgem>, [">= 1.1.0"])
s.add_dependency(%q<mocha>, [">= 0.9.4"]) s.add_dependency(%q<mocha>, [">= 0.9.4"])
s.add_dependency(%q<hoe>, [">= 1.8.0"]) s.add_dependency(%q<hoe>, [">= 1.8.0"])
Expand Down
14 changes: 9 additions & 5 deletions test/fixtures/receipt_info_response.xml
@@ -1,5 +1,9 @@
<Receipt store="Morgan Imports" id="1" total="$1,929.00" selldate="5/12/2008" createdDate="4/12/2008" modifiedDate="4/20/2008" imgurl="http://www.shoeboxed.com/receipt.jpeg"> <GetReceiptInfoCallResponse>
<Categories> <Receipt store="Morgan Imports" id="139595947" total="$1,929.00" documentCurrency="USD" accountCurrency="USD" conversionRate="1" documentTotal="1929.00" convertedTotal="1929.00" formattedDocumentTotal="$1,929.00" formattedConvertedTotal="$1,929.00" documentTax="" convertedTax="" formattedDocumentTax="" formattedConvertedTax="" date="7/12/2008" modifiedDate="7/12/2008" createdDate="7/10/2008" selldate="5/12/2008" imgurl="http://www.shoeboxed.com/receipt.jpeg?rid=139595947&code=1b106d61cbfa5078f53050e2f3bc315f">
<Category name="Category 1" id="1"/> <Categories>
</Categories> <Category name="Meals / Entertainment" id="23423342"/>
</Receipt> <Category name="General Retail" id="9121023"/>
<Category name="Fuel" id="18222392"/>
</Categories>
</Receipt>
</GetReceiptInfoCallResponse>
23 changes: 13 additions & 10 deletions test/fixtures/receipt_response.xml
@@ -1,14 +1,17 @@
<GetReceiptCallResponse> <GetReceiptCallResponse>
<Receipts count="2"> <Receipts count="2">
<Receipt store="Great Plains Trust Company" id="23984923842" total="$3,378.30" selldate="5/12/2008" createdDate="4/12/2008" modifiedDate="4/20/2008" imgurl="http://www.shoeboxed.com/receipt1.jpeg"> <Receipt store="Great Plains Trust Company" id="23984923842" total="$3,378.30" documentCurrency="USD" accountCurrency="USD" conversionRate="1" documentTotal="3378.30" convertedTotal="3378.30" formattedDocumentTotal="$3,378.30" formattedConvertedTotal="$3,378.30" documentTax="" convertedTax="" formattedDocumentTax="" formattedConvertedTax="" date="7/12/2008" modifiedDate="7/12/2008" createdDate="7/10/2008" selldate="5/12/2008" imgurl="http://www.shoeboxed.com/receipt.jpeg?rid= 23984923842&code=1b106d61cbfa5078f53050e2f3bc315f">
<Categories> <Categories>
<Category name="Category 1" id="1"/> <Category name="Meals / Entertainment" id="23423342"/>
</Categories> <Category name="General Retail" id="9121023"/>
</Receipt> <Category name="Fuel" id="18222392"/>
<Receipt store="RadioShack" id="39239293" total="$3.51" selldate="5/12/2008" createdDate="4/12/2008" modifiedDate="4/20/2008" imgurl="http://www.shoeboxed.com/receipt2.jpeg"> </Categories>
<Categories> </Receipt>
<Category name="Category 2" id="2"/> <Receipt store="RadioShack" id="39239293" total="$3.51" documentCurrency="USD" accountCurrency="USD" conversionRate="1" documentTotal="3.51" convertedTotal="3.51" formattedDocumentTotal="$3.51" formattedConvertedTotal="$3.51" documentTax="" convertedTax="" formattedDocumentTax="" formattedConvertedTax="" date="7/12/2008" modifiedDate="7/12/2008" createdDate="7/10/2008" selldate="5/12/2008" imgurl="http://www.shoeboxed.com/receipt.jpeg?rid= 39239293&code=1b106d61cbfa5078f53050e2f3bc315f">
</Categories> <Categories>
</Receipt> <Category name="Electronics" id="892732"/>
</Categories>
</Receipt>
</Receipts> </Receipts>
</GetReceiptCallResponse> </GetReceiptCallResponse>

51 changes: 31 additions & 20 deletions test/test_connection.rb
Expand Up @@ -7,12 +7,20 @@ def setup
@conn = Connection.new("api_key", "user_token") @conn = Connection.new("api_key", "user_token")


@category1 = Category.new @category1 = Category.new
@category1.id = "1" @category1.id = "23423342"
@category1.name = "Category 1" @category1.name = "Meals / Entertainment"


@category2 = Category.new @category2 = Category.new
@category2.id = "2" @category2.id = "9121023"
@category2.name = "Category 2" @category2.name = "General Retail"

@category3 = Category.new
@category3.id = "18222392"
@category3.name = "Fuel"

@category4 = Category.new
@category4.id = "892732"
@category4.name = "Electronics"
end end


def test_authenticate__connection_failures def test_authenticate__connection_failures
Expand Down Expand Up @@ -45,14 +53,15 @@ def test_get_receipt_info_call__success_getting_one_receipt


receipt = conn.get_receipt_info_call('1') receipt = conn.get_receipt_info_call('1')
assert_not_nil receipt assert_not_nil receipt
assert_equal "1", receipt.id assert_equal "139595947", receipt.id
assert_equal "Morgan Imports", receipt.store assert_equal "Morgan Imports", receipt.store
assert_equal Date.new(2008, 5, 12), receipt.sell_date assert_equal Date.new(2008, 5, 12), receipt.sell_date
assert_equal Date.new(2008, 4, 12), receipt.created_date assert_equal Date.new(2008, 7, 10), receipt.created_date
assert_equal Date.new(2008, 4, 20), receipt.modified_date assert_equal Date.new(2008, 7, 12), receipt.modified_date
assert_equal BigDecimal.new("1929.00"), receipt.total assert_equal BigDecimal.new("1929.00"), receipt.converted_total
assert_equal "http://www.shoeboxed.com/receipt.jpeg", receipt.image_url assert_equal "USD", receipt.account_currency
assert_equal [@category1], receipt.categories assert_equal "http://www.shoeboxed.com/receipt.jpeg?rid=139595947&code=1b106d61cbfa5078f53050e2f3bc315f", receipt.image_url
assert_equal [@category1, @category2, @category3], receipt.categories
end end


def test_build_receipt_info_call_request def test_build_receipt_info_call_request
Expand All @@ -77,21 +86,23 @@ def test_get_receipt_call__success_getting_two_receipt
assert_equal "23984923842", receipt.id assert_equal "23984923842", receipt.id
assert_equal "Great Plains Trust Company", receipt.store assert_equal "Great Plains Trust Company", receipt.store
assert_equal Date.new(2008, 5, 12), receipt.sell_date assert_equal Date.new(2008, 5, 12), receipt.sell_date
assert_equal Date.new(2008, 4, 12), receipt.created_date assert_equal Date.new(2008, 7, 10), receipt.created_date
assert_equal Date.new(2008, 4, 20), receipt.modified_date assert_equal Date.new(2008, 7, 12), receipt.modified_date
assert_equal BigDecimal.new("3378.30"), receipt.total assert_equal BigDecimal.new("3378.30"), receipt.converted_total
assert_equal "http://www.shoeboxed.com/receipt1.jpeg", receipt.image_url assert_equal "USD", receipt.account_currency
assert_equal [@category1], receipt.categories assert_equal "http://www.shoeboxed.com/receipt.jpeg?rid= 23984923842&code=1b106d61cbfa5078f53050e2f3bc315f", receipt.image_url
assert_equal [@category1, @category2, @category3], receipt.categories


receipt = receipts[1] receipt = receipts[1]
assert_equal "39239293", receipt.id assert_equal "39239293", receipt.id
assert_equal "RadioShack", receipt.store assert_equal "RadioShack", receipt.store
assert_equal Date.new(2008, 5, 12), receipt.sell_date assert_equal Date.new(2008, 5, 12), receipt.sell_date
assert_equal Date.new(2008, 4, 12), receipt.created_date assert_equal Date.new(2008, 7, 10), receipt.created_date
assert_equal Date.new(2008, 4, 20), receipt.modified_date assert_equal Date.new(2008, 7, 12), receipt.modified_date
assert_equal BigDecimal.new("3.51"), receipt.total assert_equal BigDecimal.new("3.51"), receipt.converted_total
assert_equal "http://www.shoeboxed.com/receipt2.jpeg", receipt.image_url assert_equal "USD", receipt.account_currency
assert_equal [@category2], receipt.categories assert_equal "http://www.shoeboxed.com/receipt.jpeg?rid= 39239293&code=1b106d61cbfa5078f53050e2f3bc315f", receipt.image_url
assert_equal [@category4], receipt.categories
end end


def test_get_receipt_call__uses_gmt def test_get_receipt_call__uses_gmt
Expand Down
34 changes: 23 additions & 11 deletions test/test_receipt.rb
Expand Up @@ -5,8 +5,16 @@ class TestReceipt < Test::Unit::TestCase


def setup def setup
@category1 = Category.new @category1 = Category.new
@category1.id = "1" @category1.id = "23423342"
@category1.name = "Category 1" @category1.name = "Meals / Entertainment"

@category2 = Category.new
@category2.id = "9121023"
@category2.name = "General Retail"

@category3 = Category.new
@category3.id = "18222392"
@category3.name = "Fuel"
end end


def test_initialize_parse_xml def test_initialize_parse_xml
Expand All @@ -15,14 +23,15 @@ def test_initialize_parse_xml
assert_equal 1, receipts.size assert_equal 1, receipts.size


receipt = receipts.first receipt = receipts.first
assert_equal "1", receipt.id assert_equal "139595947", receipt.id
assert_equal "Morgan Imports", receipt.store assert_equal "Morgan Imports", receipt.store
assert_equal Date.new(2008, 5, 12), receipt.sell_date assert_equal Date.new(2008, 5, 12), receipt.sell_date
assert_equal Date.new(2008, 4, 12), receipt.created_date assert_equal Date.new(2008, 7, 10), receipt.created_date
assert_equal Date.new(2008, 4, 20), receipt.modified_date assert_equal Date.new(2008, 7, 12), receipt.modified_date
assert_equal BigDecimal.new("1929.00"), receipt.total assert_equal BigDecimal.new("1929.00"), receipt.converted_total
assert_equal "http://www.shoeboxed.com/receipt.jpeg", receipt.image_url assert_equal "USD", receipt.account_currency
assert_equal [@category1], receipt.categories assert_equal "http://www.shoeboxed.com/receipt.jpeg?rid=139595947&code=1b106d61cbfa5078f53050e2f3bc315f", receipt.image_url
assert_equal [@category1, @category2, @category3], receipt.categories
end end


def test_receipt__accessors def test_receipt__accessors
Expand All @@ -34,9 +43,12 @@ def test_receipt__accessors


receipt.id = "1" receipt.id = "1"
assert_equal "1", receipt.id assert_equal "1", receipt.id

receipt.converted_total = '$1,000.19'
assert_equal BigDecimal.new('1000.19'), receipt.converted_total


receipt.total = '$1,000.19' receipt.account_currency = "USD"
assert_equal BigDecimal.new('1000.19'), receipt.total assert_equal "USD", receipt.account_currency


receipt.created_date = '1/2/2001' receipt.created_date = '1/2/2001'
assert_equal Date.parse('1/2/2001'), receipt.created_date assert_equal Date.parse('1/2/2001'), receipt.created_date
Expand All @@ -50,7 +62,7 @@ def test_receipt__accessors
receipt.categories = [@category1] receipt.categories = [@category1]
assert_equal [@category1], receipt.categories assert_equal [@category1], receipt.categories
end end

def test_equal def test_equal
# lame test but at least execute the code # lame test but at least execute the code
assert_equal Receipt.new, Receipt.new assert_equal Receipt.new, Receipt.new
Expand Down

0 comments on commit b0041e4

Please sign in to comment.