Class: RockautoApi::Parsers::OrderParser
- Inherits:
-
Object
- Object
- RockautoApi::Parsers::OrderParser
- Defined in:
- lib/rockauto_api/parsers/order_parser.rb
Class Method Summary collapse
- .extract_field(doc, pattern) ⇒ Object
- .parse(html) ⇒ Object
- .parse_billing(doc) ⇒ Object
- .parse_items(doc) ⇒ Object
- .parse_shipping(doc) ⇒ Object
Class Method Details
.extract_field(doc, pattern) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/rockauto_api/parsers/order_parser.rb', line 19 def self.extract_field(doc, pattern) doc.css("td, th, div, span").each do |el| if el.text.strip.match?(pattern) next_el = el.next_element || el.parent&.next_element return next_el.text.strip if next_el end end nil end |
.parse(html) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rockauto_api/parsers/order_parser.rb', line 6 def self.parse(html) doc = Nokogiri::HTML(html) { order_number: extract_field(doc, /Order\s*#?/i), order_date: extract_field(doc, /Date/i), status: extract_field(doc, /Status/i), items: parse_items(doc), billing: parse_billing(doc), shipping: parse_shipping(doc) } end |
.parse_billing(doc) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/rockauto_api/parsers/order_parser.rb', line 48 def self.parse_billing(doc) Models::BillingInfo.new( subtotal: extract_field(doc, /Subtotal/i), shipping_cost: extract_field(doc, /Shipping/i), tax: extract_field(doc, /Tax/i), total: extract_field(doc, /Total/i) ) end |
.parse_items(doc) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rockauto_api/parsers/order_parser.rb', line 29 def self.parse_items(doc) doc.css("table tr").map { |row| cells = row.css("td").map { |c| c.text.strip } next nil if cells.size < 3 Models::OrderItem.new( part_number: cells[0] || "", description: cells[1] || "", brand: cells[2], quantity: cells[3], unit_price: cells[4], total_price: cells[5], status: cells[6], tracking_number: cells[7] ) }.compact rescue StandardError [] end |
.parse_shipping(doc) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rockauto_api/parsers/order_parser.rb', line 57 def self.parse_shipping(doc) Models::ShippingInfo.new( method: extract_field(doc, /Method/i), carrier: extract_field(doc, /Carrier/i), tracking_number: extract_field(doc, /Tracking/i) ) end |