Created
July 3, 2013 19:47
-
-
Save ambethia/5922113 to your computer and use it in GitHub Desktop.
POW Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'nokogiri' | |
| require 'cgi' | |
| configure do | |
| set :public_folder, Proc.new { File.join(root, "static") } | |
| enable :sessions | |
| end | |
| get '/' do | |
| erb :upload | |
| end | |
| post '/' do | |
| content_type 'text/xml' | |
| attachment "OR10010.xml" | |
| Converter.new(params['input'][:tempfile], | |
| params['store_id'], params['cf_1'], | |
| params['cf_2'], params['cf_3']).xml | |
| end | |
| class Converter | |
| attr :opts, :doc, :root, :books | |
| def initialize(input, store_id, cf_1, cf_2, cf_3) | |
| @opts = { | |
| :input => input.read, | |
| :store_id => store_id, | |
| :cf_1 => cf_1, | |
| :cf_2 => cf_2, | |
| :cf_3 => cf_3 | |
| } | |
| @doc = Nokogiri.XML(opts[:input]) { |c| c.default_xml.noblanks } | |
| @root = doc.root.name | |
| @books = [] | |
| doc.css("#{root}_row").each do |node| | |
| book = { courses: [] } | |
| book[:isbn] = node.at_css("inventory_table_in_isbn").content | |
| book[:author] = node.at_css("inventory_table_in_author").content.rstrip | |
| book[:title] = node.at_css("inventory_table_in_title").content.rstrip | |
| book[:edition] = node.at_css("inventory_table_in_edition").content | |
| book[:copyright] = node.at_css("inventory_table_in_copyright").content | |
| book[:publisher] = node.at_css("inventory_table_in_publisher").content | |
| book[:actual_sales] = node.at_css("currentsales").content | |
| book[:new_on_hand] = node.at_css("inventory_site_tb_in_qty_onhand_new").content | |
| book[:usd_on_hand] = node.at_css("inventory_site_tb_in_qty_onhand_used").content | |
| book[:book_class] = node.at_css("book_class_tb_book_class_code").content | |
| book[:order_from_vendor] = node.at_css("inventory_table_in_vendor1").content | |
| book[:edition_note] = node.at_css("item_tb_itm_edition_status").content | |
| book[:book_note] = node.at_css("item_tb_itm_book_note").content | |
| book[:on_hand_quantity] = node.at_css("totalqoh").content | |
| book[:estimated_buyback] = node.at_css("estbb").content | |
| book[:estimated_buyback] = node.at_css("estbb").content | |
| book[:on_order_quantity] = node.at_css("onorder").content | |
| book[:available] = node.at_css("pre_estavil").content | |
| book[:open_to_buy] = node.at_css("otb").content | |
| book[:estimated_sales] = node.at_css("estsales").content | |
| book[:stgp_id] = opts[:store_id] | |
| book[:process_stor_id] = opts[:store_id] | |
| book[:tide_code] = "" # UNKNOWN | |
| book[:cf_vendor] = "#{book[:publisher]}/ #{book[:order_from_vendor]}" | |
| book[:cf_store_id] = "" # BLANK | |
| book[:cf_total] = "" # BLANK | |
| node.css("#{root}_group1").each do |node| | |
| course = {} | |
| course[:required] = node.at_css("item_adoption_tb_required_code").content | |
| course[:actual_enrollment] = node.at_css("section_tb_section_actual_enrollment").content | |
| course[:course_note] = node.at_css("college_course_tb_course_note").content | |
| course[:expected_sales] = node.at_css("estsales").content | |
| course[:term_type] = node.at_css("termyear_tb_term_desc").content | |
| course[:term_year] = node.at_css("termyear_tb_term_start_date").content.split("-").first | |
| course[:course_code] = [ | |
| node.at_css("college_department_tb_college_depart_code").content, | |
| node.at_css("college_course_tb_course_name").content, | |
| node.at_css("section_tb_section_number").content].join("-") | |
| course[:instructor] = node.at_css("customer_tb_cu_lname").content | |
| course[:expected_enrollment] = node.at_css("section_tb_section_estimated_enrollment").content | |
| course[:section_note] = node.at_css("section_tb_section_note").content | |
| course[:stgp_id] = opts[:store_id] | |
| course[:process_stor_id] = opts[:store_id] | |
| course[:tide_code] = "" # UNKNOWN | |
| course[:term_id] = node.at_css("termyear_tb_term_id").content | |
| course[:pers_id] = "" # UNKNOWN | |
| course[:div_dept_course_code] = node.at_css("book_class_tb_book_class_code").content | |
| course[:enrollment] = "#{node.at_css("section_tb_section_estimated_enrollment").content} / #{node.at_css("section_tb_section_actual_enrollment").content}" | |
| book[:courses] << course | |
| end | |
| books << book | |
| end | |
| end | |
| def xml | |
| out = Nokogiri::XML::Builder.new do |xml| | |
| xml.OR10010 do | |
| xml.LIST_G_STGP do | |
| books.each do |book| | |
| xml.G_STGP do | |
| xml.OTBW_ACTUAL_SALES book[:actual_sales] | |
| xml.OTBW_NEW_ON_HAND book[:new_on_hand] | |
| xml.OTBW_USD_ON_HAND book[:usd_on_hand] | |
| xml.OTBW_AUTHOR book[:author] | |
| xml.OTBW_TITLE book[:title] | |
| xml.OTBW_EDITION book[:edition] | |
| xml.OTBW_COPYRIGHT book[:copyright] | |
| xml.OTBW_ISBN book[:isbn] | |
| xml.OTBW_BOOK_CLASS book[:book_class] | |
| xml.OTBW_PUBLISHER book[:publisher] | |
| xml.OTBW_ORDER_FROM_VENDOR book[:order_from_vendor] | |
| xml.OTBW_EDITION_NOTE book[:edition_note] | |
| xml.OTBW_BOOK_NOTE book[:book_note] | |
| xml.OTBW_ON_HAND_QUANTITY book[:on_hand_quantity] | |
| xml.OTBW_ESTIMATED_BUYBACK book[:estimated_buyback] | |
| xml.OTBW_ON_ORDER_QUANTITY book[:on_order_quantity] | |
| xml.OTBW_AVAILABLE book[:available] | |
| xml.OTBW_OPEN_TO_BUY book[:open_to_buy] | |
| xml.OTBW_ESTIMATED_SALES book[:estimated_sales] | |
| xml.OTBW_STGP_ID book[:stgp_id] | |
| xml.OTBW_PROCESS_STOR_ID book[:process_stor_id] | |
| xml.OTBW_TIDE_CODE book[:tide_code] | |
| xml.LIST_G_STOR "\n " | |
| xml.LIST_G_COURSE do | |
| book[:courses].each do |course| | |
| xml.G_COURSE do | |
| xml.OTCW_REQUIRED course[:required] | |
| xml.OTCW_ACTUAL_ENROLLMENT course[:actual_enrollment] | |
| xml.OTCW_COURSE_NOTE course[:course_note] | |
| xml.OTCW_EXPECTED_SALES course[:expected_sales] | |
| xml.OTCW_TERM_TYPE course[:term_type] | |
| xml.OTCW_TERM_YEAR course[:term_year] | |
| xml.OTCW_COURSE_CODE course[:course_code] | |
| xml.OTCW_INSTRUCTOR course[:instructor] | |
| xml.OTCW_EXPECTED_ENROLLMENT course[:expected_enrollment] | |
| xml.OTCW_SECTION_NOTE course[:section_note] | |
| xml.OTCW_STGP_ID course[:stgp_id] | |
| xml.OTCW_PROCESS_STOR_ID course[:process_stor_id] | |
| xml.OTCW_TIDE_CODE course[:tide_code] | |
| xml.OTCW_TERM_ID course[:term_id] | |
| xml.OTCW_PERS_ID course[:pers_id] | |
| xml.OTCW_DIV_DEPT_COURSE_CODE course[:div_dept_course_code] | |
| xml.CF_ENROLLMENT course[:enrollment] | |
| end | |
| end | |
| end | |
| xml.CF_VENDOR book[:cf_vendor] | |
| xml.CF_ISBN book[:isbn] | |
| xml.CF_STORE_ID book[:cf_store_id] | |
| xml.CF_TOTAL book[:cf_total] | |
| end | |
| end | |
| end | |
| xml.CF_1 opts[:cf_1] | |
| xml.CF_2 opts[:cf_2] | |
| xml.CF_3 opts[:cf_3] | |
| end | |
| end | |
| out.to_xml | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment