Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added attr_at_xpath to cleanup the amazon parser

  • Loading branch information...
commit 10d9097c1313392eaad65f27d9a6828fe680127a 1 parent 77e7ed3
@jashmenn authored
View
28 app/models/sogi/order_parser.rb
@@ -4,11 +4,23 @@
# abstract class
class Sogi::OrderParser
+ module XmlParsingClasMethods
+ # attr_at_xpath :order_id, "/AmazonOrderID"
+ # then the custom data fields just specify what you want to go into custom data,
+ # by default calls the method of the same key name
+ def attr_at_xpath(symbol, xpath)
+ define_method symbol do
+ value_of xpath
+ end
+ end
+ end
+
# helper methods for easy parsing of the document
- module XmlParsingHelperMethods
+ module XmlParsingInstanceMethods
# a path to always search when using "value_of"
def value_of_prefix; ""; end
+ # returns the inner html of a given xpath
def value_of(xpath)
found_elem = @document.at(value_of_prefix + xpath)
return nil unless found_elem
@@ -17,12 +29,14 @@ def value_of(xpath)
alias_method :v, :value_of
end
+
# thin object to represent a particular order contained in the document
class Order
- include XmlParsingHelperMethods
+ include XmlParsingInstanceMethods
attr_accessor :document
class << self
+ include XmlParsingClasMethods
# def add_custom_data(key, xpath)
# @@custom_data ||= {}
# @@custom_data[key] = xpath
@@ -57,19 +71,23 @@ def line_items
ret
end
-
end
class LineItem
- include XmlParsingHelperMethods
+ class << self
+ include XmlParsingClasMethods
+ end
+
+ include XmlParsingInstanceMethods
attr_accessor :document
end
- include XmlParsingHelperMethods
+ include XmlParsingInstanceMethods
attr_accessor :body
attr_accessor :document
class << self
+ include XmlParsingClasMethods
# def custom_order_data(key, xpath)
# Order.add_custom_data(key, xpath)
View
51 app/models/sogi/parser/amazon.rb
@@ -12,54 +12,52 @@ def orders_found_in
"/AmazonEnvelope/Message/OrderReport"
end
+ attr_at_xpath :merchant_identifier, "/AmazonEnvelope/Header/MerchantIdentifier"
define_order_methods_as do
+ # the custom data fields just specify what you want to go into custom data,
+ # by default calls the method of the same key name
def line_items_found_in
"/Item"
end
- # attr_at_xpath :order_id, "/AmazonOrderID"
- # then the custom data fields just specify what you want to go into custom data,
- # by default calls the method of the same key name
-
- def order_id ; v "/AmazonOrderID" ; end
+ attr_at_xpath :order_id, "/AmazonOrderID"
- def ordered_at ; v "/OrderDate" ; end
- def posted_at ; v "/OrderPostedDate" ; end
+ attr_at_xpath :ordered_at, "/OrderDate"
+ attr_at_xpath :posted_at, "/OrderPostedDate"
- def billing_email ; v "/BillingData/BuyerEmailAddress" ; end
- def billing_name ; v "/BillingData/BuyerName" ; end
- def billing_phone_number ; v "/BillingData/BuyerPhoneNumber" ; end
+ attr_at_xpath :billing_email, "/BillingData/BuyerEmailAddress"
+ attr_at_xpath :billing_name, "/BillingData/BuyerName"
+ attr_at_xpath :billing_phone_number, "/BillingData/BuyerPhoneNumber"
- def fulfillment_method ; v "/FulfillmentData/FulfillmentMethod" ; end
- def fulfillment_level ; v "/FulfillmentData/FulfillmentServiceLevel" ; end
+ attr_at_xpath :fulfillment_method, "/FulfillmentData/FulfillmentMethod"
+ attr_at_xpath :fulfillment_level, "/FulfillmentData/FulfillmentServiceLevel"
- def shipping_name ; v "/FulfillmentData/Address/Name" ; end
- def shipping_address_one ; v "/FulfillmentData/Address/AddressFieldOne" ; end
- def shipping_city ; v "/FulfillmentData/Address/City" ; end
- def shipping_state ; v "/FulfillmentData/Address/StateOrRegion" ; end
- def shipping_zip ; v "/FulfillmentData/Address/PostalCode" ; end
- def shipping_country ; v "/FulfillmentData/Address/CountryCode" ; end
- def shipping_phone ; v "/FulfillmentData/Address/PhoneNumber" ; end
+ attr_at_xpath :shipping_name, "/FulfillmentData/Address/Name"
+ attr_at_xpath :shipping_address_one, "/FulfillmentData/Address/AddressFieldOne"
+ attr_at_xpath :shipping_city, "/FulfillmentData/Address/City"
+ attr_at_xpath :shipping_state, "/FulfillmentData/Address/StateOrRegion"
+ attr_at_xpath :shipping_zip, "/FulfillmentData/Address/PostalCode"
+ attr_at_xpath :shipping_country, "/FulfillmentData/Address/CountryCode"
+ attr_at_xpath :shipping_phone, "/FulfillmentData/Address/PhoneNumber"
end
define_line_item_methods_as do
- def order_code ; v "/AmazonOrderItemCode" ; end
- def sku ; v "/SKU" ; end
- def title ; v "/Title" ; end
- def quantity ; v("/Quantity").to_i ; end
- def tax_code ; v "/ProductTaxCode" ; end
+ attr_at_xpath :order_code, "/AmazonOrderItemCode"
+ attr_at_xpath :sku, "/SKU"
+ attr_at_xpath :title, "/Title"
+ attr_at_xpath :tax_code, "/ProductTaxCode"
+ attr_at_xpath :gift_message, "/GiftMessageText"
+ def quantity ; v("/Quantity").to_i ; end
def price ; price_method("Principal") ; end
def shipping_price ; price_method("Shipping") ; end
def tax ; price_method("Tax") ; end
def shipping_tax ; price_method("ShippingTax") ; end
- def gift_message ; v "/GiftMessageText" ; end
-
def price_method(looking_for)
price_elements = @document.search("/ItemPrice/Component")
price_elements.each do |elem|
@@ -69,7 +67,6 @@ def price_method(looking_for)
end
nil
end
-
end
end
View
7 spec/models/sogi/parser/amazon_spec.rb
@@ -21,12 +21,15 @@
@order.order_id.should eql("050-1234567-1234567")
end
- it "should have custom order data and know the merchant id" do
+ it "should have custom order data" do
# @order.custom_data.should have_at_least(1).items
- # @order.amazon_merchant_identifier.should eql("My Store")
pending "figuring out how custom data will work"
end
+ it "should know the merchant id" do
+ @parser.merchant_identifier.should eql("My Store")
+ end
+
it "should have information about dates" do
@order.ordered_at.should eql("2002-05-01T15:20:15-08:00") # todo make it a Time object
@order.posted_at.should eql("2002-05-01T15:21:49-08:00") # todo make it a Time object
Please sign in to comment.
Something went wrong with that request. Please try again.