Permalink
Browse files

Return native ruby Date type when appropriate

  • Loading branch information...
1 parent 368a4ed commit 1e8aedd734ae4ced3c9026de0ac7742d2174b279 @ffmike committed Dec 11, 2011
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- shoehorn (0.5.0)
+ shoehorn (0.5.1)
builder
GEM
View
@@ -1,15 +1,26 @@
require 'rexml/document'
require 'builder'
+class String
+ def to_date_from_shoeboxed_string
+ return nil if self.nil?
+ begin
+ Date.parse(self, false)
+ rescue Exception => ex
+ nil
+ end
+ end
+end
+
require 'shoehorn/errors'
-require 'shoehorn/connection'
+require 'shoehorn/connection'
require 'shoehorn/documents_base'
require 'shoehorn/bills'
require 'shoehorn/bill'
require 'shoehorn/business_cards'
require 'shoehorn/business_card'
require 'shoehorn/categories'
-require 'shoehorn/category'
+require 'shoehorn/category'
require 'shoehorn/expense_reports'
require 'shoehorn/expense_report'
require 'shoehorn/images'
View
@@ -7,7 +7,7 @@ def initialize(connection)
bills, self.matched_count = get_page(1)
@array = bills || []
end
-
+
def self.parse(xml)
bills = Array.new
document = REXML::Document.new(xml)
@@ -20,8 +20,8 @@ def self.parse(xml)
bill.id = bill_element.attributes["id"]
bill.envelope_code = bill_element.attributes["envelopeCode"]
bill.note = bill_element.attributes["note"]
- bill.create_date = bill_element.attributes["createDate"]
- bill.modify_date = bill_element.attributes["modifyDate"]
+ bill.create_date = bill_element.attributes["createDate"].to_date_from_shoeboxed_string
+ bill.modify_date = bill_element.attributes["modifyDate"].to_date_from_shoeboxed_string
bill.name = bill_element.attributes["name"]
bill.conversion_rate = bill_element.attributes["conversionRate"]
bill.document_currency = bill_element.attributes["documentCurrency"]
@@ -48,16 +48,16 @@ def find_by_id(id)
bills, matched_count = Bills.parse(response)
bills.empty? ? nil : bills[0]
end
-
- def get_page(i)
+
+ def get_page(i)
current_page = i
request = build_bill_request
- response = connection.post_xml(request)
+ response = connection.post_xml(request)
pages_retrieved << current_page
Bills.parse(response)
end
-
+
private
def build_bill_request(options={})
process_options(options)
@@ -18,7 +18,7 @@ def self.parse(xml)
business_card.id = business_card_element.attributes["id"]
business_card.first_name = business_card_element.attributes["firstName"]
business_card.last_name = business_card_element.attributes["lastName"]
- business_card.create_date = business_card_element.attributes["createDate"]
+ business_card.create_date = business_card_element.attributes["createDate"].to_date_from_shoeboxed_string
business_card.address = business_card_element.attributes["address"]
business_card.address2 = business_card_element.attributes["address2"]
business_card.city = business_card_element.attributes["city"]
@@ -24,7 +24,7 @@ def self.parse(xml)
expense_report = ExpenseReport.new
expense_report.id = report_element.elements["Id"].text
expense_report.name = report_element.elements["Name"].text
- expense_report.date = report_element.elements["Date"].text
+ expense_report.date = report_element.elements["Date"].text.to_date_from_shoeboxed_string
expense_report.num_pages = report_element.elements["NumPages"].text.to_i
expense_report.url = report_element.elements["URL"].text
rescue => e
@@ -20,8 +20,8 @@ def self.parse(xml)
other_document.id = other_document_element.attributes["id"]
other_document.envelope_code = other_document_element.attributes["envelopeCode"]
other_document.note = other_document_element.attributes["note"]
- other_document.create_date = other_document_element.attributes["createDate"]
- other_document.modify_date = other_document_element.attributes["modifyDate"]
+ other_document.create_date = other_document_element.attributes["createDate"].to_date_from_shoeboxed_string
+ other_document.modify_date = other_document_element.attributes["modifyDate"].to_date_from_shoeboxed_string
other_document.name = other_document_element.attributes["name"]
image_element = other_document_element.elements["Images"]
@@ -29,9 +29,9 @@ def self.parse(xml)
receipt.converted_tax = receipt_element.attributes["convertedTax"]
receipt.formatted_document_tax = receipt_element.attributes["formattedDocumentTax"]
receipt.formatted_converted_tax = receipt_element.attributes["formattedConvertedTax"]
- receipt.modified_date = receipt_element.attributes["modifiedDate"]
- receipt.created_date = receipt_element.attributes["createdDate"]
- receipt.selldate = receipt_element.attributes["selldate"]
+ receipt.modified_date = receipt_element.attributes["modifiedDate"].to_date_from_shoeboxed_string
+ receipt.created_date = receipt_element.attributes["createdDate"].to_date_from_shoeboxed_string
+ receipt.selldate = receipt_element.attributes["selldate"].to_date_from_shoeboxed_string
category_element = receipt_element.elements["Categories"]
receipt.categories = category_element ? Categories.parse(category_element.to_s) : []
View
@@ -24,8 +24,8 @@ class BillsTest < ShoehornTest
assert_equal 2, bills.size
assert_equal "123884", bills[0].id
assert_equal "", bills[0].envelope_code
- assert_equal "02/04/2011", bills[0].create_date
- assert_equal "02/04/2011", bills[0].modify_date
+ assert_equal Date.new(2011, 2, 4), bills[0].create_date
+ assert_equal Date.new(2011, 2, 4), bills[0].modify_date
assert_equal "Power Bill", bills[0].name
assert_equal "USD", bills[0].document_currency
assert_equal "USD", bills[0].account_currency
@@ -59,8 +59,8 @@ class BillsTest < ShoehornTest
bill = connection.bills.find_by_id("123884")
assert_equal "123884", bill.id
assert_equal "", bill.envelope_code
- assert_equal "02/04/2011", bill.create_date
- assert_equal "02/04/2011", bill.modify_date
+ assert_equal Date.new(2011, 2, 4), bill.create_date
+ assert_equal Date.new(2011, 2, 4), bill.modify_date
assert_equal "Power Bill", bill.name
assert_equal "USD", bill.document_currency
assert_equal "USD", bill.account_currency
@@ -25,7 +25,7 @@ class BusinessCardsTest < ShoehornTest
assert_equal "331378049", business_cards[0].id
assert_equal "Richard", business_cards[0].first_name
assert_equal "Davies", business_cards[0].last_name
- assert_equal "2/5/2009", business_cards[0].create_date
+ assert_equal Date.new(2009, 2, 5), business_cards[0].create_date
assert_equal "", business_cards[0].address
assert_equal "", business_cards[0].address2
assert_equal "RTP", business_cards[0].city
@@ -59,7 +59,7 @@ class BusinessCardsTest < ShoehornTest
assert_equal "331378049", business_card.id
assert_equal "Richard", business_card.first_name
assert_equal "Davies", business_card.last_name
- assert_equal "2/5/2009", business_card.create_date
+ assert_equal Date.new(2009, 2, 5), business_card.create_date
assert_equal "", business_card.address
assert_equal "", business_card.address2
assert_equal "RTP", business_card.city
@@ -24,7 +24,7 @@ class ExpenseReportsTest < ShoehornTest
assert_equal 2, expense_reports.size
assert_equal "1902938", expense_reports[0].id
assert_equal "Vacation Expenses", expense_reports[0].name
- assert_equal "07/11/2011", expense_reports[0].date
+ assert_equal Date.new(2011, 7, 11), expense_reports[0].date
assert_equal 3, expense_reports[0].num_pages
assert_equal "https://app.shoeboxed.com/api/export/pdf-expenses/1902938/851d1b6e82b1d28b1cef6f9f6eebbb19", expense_reports[0].url
end
@@ -24,8 +24,8 @@ class OtherDocumentsTest < ShoehornTest
assert_equal 2, other_documents.size
assert_equal "124532", other_documents[0].id
assert_equal "BYWFW2Y0", other_documents[0].envelope_code
- assert_equal "06/13/2011", other_documents[0].create_date
- assert_equal "06/13/2011", other_documents[0].modify_date
+ assert_equal Date.new(2011, 6, 13), other_documents[0].create_date
+ assert_equal Date.new(2011, 6, 13), other_documents[0].modify_date
assert_equal "Bank Statement 05/2011", other_documents[0].name
end
@@ -52,8 +52,8 @@ class OtherDocumentsTest < ShoehornTest
other_document = connection.other_documents.find_by_id("123884")
assert_equal "124532", other_document.id
assert_equal "BYWFW2Y0", other_document.envelope_code
- assert_equal "06/13/2011", other_document.create_date
- assert_equal "06/13/2011", other_document.modify_date
+ assert_equal Date.new(2011, 6, 13), other_document.create_date
+ assert_equal Date.new(2011, 6, 13), other_document.modify_date
assert_equal "Bank Statement 05/2011", other_document.name
assert_equal 4, other_document.images.size
View
@@ -36,9 +36,9 @@ class ReceiptsTest < ShoehornTest
assert_equal "", receipts[0].converted_tax
assert_equal "", receipts[0].formatted_document_tax
assert_equal "", receipts[0].formatted_converted_tax
- assert_equal "7/12/2008", receipts[0].modified_date
- assert_equal "7/10/2008", receipts[0].created_date
- assert_equal "5/12/2008", receipts[0].selldate
+ assert_equal Date.new(2008, 7, 12), receipts[0].modified_date
+ assert_equal Date.new(2008, 7, 10), receipts[0].created_date
+ assert_equal Date.new(2008, 5, 12), receipts[0].selldate
end
should "retrieve the total number of available receipts" do
@@ -83,9 +83,9 @@ class ReceiptsTest < ShoehornTest
assert_equal "", receipt.converted_tax
assert_equal "", receipt.formatted_document_tax
assert_equal "", receipt.formatted_converted_tax
- assert_equal "7/12/2008", receipt.modified_date
- assert_equal "7/10/2008", receipt.created_date
- assert_equal "5/12/2008", receipt.selldate
+ assert_equal Date.new(2008, 7, 12), receipt.modified_date
+ assert_equal Date.new(2008, 7, 10), receipt.created_date
+ assert_equal Date.new(2008, 5, 12), receipt.selldate
assert_equal 3, receipt.categories.size
assert_equal "23423342", receipt.categories[0].id

0 comments on commit 1e8aedd

Please sign in to comment.