Created
August 18, 2017 07:30
-
-
Save serg2801/4bca107e93a76bb78c790be5fd9bf08d to your computer and use it in GitHub Desktop.
This file contains 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
# frozen_string_literal: true | |
require 'net/ftp' | |
require 'open-uri' | |
require 'openssl' | |
module ParserXml | |
class Parser | |
DEPTH = 'depth' # param in xml file | |
HEIGHT = 'height' # param in xml file | |
LENGTH = 'length' # param in xml file | |
WEIGHT = 'weight' # param in xml file | |
RETAIL_SALES_CATEGORY = 'retailSalesCategory' # param in xml file | |
UNIT_PRICE = 'unitPrice' # param in xml file | |
NET_PRICE = 'netPrice' # param in xml file | |
ITEM_CLASS = 'itemClass' # param in xml file | |
CATEGORY = 'retailSalesCategory' # param in xml file | |
def download_xml_status_items | |
ftp = Net::FTP.new('ftp.ashleyfurniture.com') | |
ftp.passive = true | |
ftp.login('****', '****') | |
ftp.chdir('********') | |
files = ftp.nlst('*846*') | |
most_recent = files.sort_by { |filename| ftp.mtime(filename) }.last | |
ftp.getbinaryfile(most_recent, "vendor/xml_files/xml_status_items/#{most_recent}", 1024) | |
ftp.close | |
rescue StandardError => e | |
puts e.message | |
end | |
def download_xml_info_rooms | |
ftp = Net::FTP.new('ftp.ashleyfurniture.com') | |
ftp.passive = true | |
ftp.login('****', '****') | |
ftp.chdir('*****') | |
files = ftp.nlst('*888*') | |
most_recent = files.sort_by { |filename| ftp.mtime(filename) }.last | |
ftp.getbinaryfile(most_recent, "vendor/xml_files/xml_info_rooms/#{most_recent}", 1024) | |
ftp.close | |
rescue StandardError => e | |
puts e.message | |
end | |
def item_update_attributes(item, item_info) | |
item_class = get_attr_item(item_info, ITEM_CLASS) | |
category = get_attr_item(item_info, CATEGORY) | |
description = get_description(item_info) | |
item_char_depth_in = get_size_in_item_char(item_info, DEPTH) | |
item_char_height_in = get_size_in_item_char(item_info, HEIGHT) | |
item_char_length_in = get_size_in_item_char(item_info, LENGTH) | |
package_char_weight_lb = get_size_in_item_package(item_info, WEIGHT) | |
unit_price = get_item_pricing(item_info, UNIT_PRICE) | |
net_price = get_item_pricing(item_info, NET_PRICE) | |
dimensions_inches_format = dimensions_inches_format(item_char_depth_in, item_char_height_in, package_char_weight_lb) | |
item.update_attributes(description: description, unit_price: unit_price, net_price: net_price, | |
dimensions_inches_format: dimensions_inches_format, item_char_depth_in: item_char_depth_in, | |
item_char_height_in: item_char_height_in, item_char_length_in: item_char_length_in, | |
package_char_weight_lb: package_char_weight_lb, item_class: item_class, retail_sale_category: category) | |
rescue StandardError => e | |
puts e.message | |
end | |
def item_update_status(item, item_info, xml_file_id) | |
status = get_status(item_info) | |
if status.to_i == 0 | |
future_status = get_future_status(item_info) == 0 ? {} : get_future_status(item_info) | |
item.update_attributes(future_status: future_status, xml_file_id: xml_file_id) | |
else | |
item.update_attributes(status: status, xml_file_id: xml_file_id, future_status: {}) | |
end | |
rescue StandardError => e | |
puts e.message | |
end | |
def get_size_in_item_char(item_info, param) | |
item_char = item_info.css('itemIdentification itemCharacteristics') | |
return nil if item_char[0].blank? || item_char[0].css("itemDimensions #{param}")[0].blank? | |
item_char[0].css("itemDimensions #{param}")[0]['value'] | |
end | |
def get_size_in_item_package(item_info, param) | |
p_char = item_info.css('itemIdentification packageCharacteristics') | |
return nil if p_char[0].blank? || p_char[0].css("packageDimensions #{param}")[0].blank? | |
p_char[0].css("packageDimensions #{param}")[0]['value'] | |
end | |
def get_item_pricing(item_info, param) | |
return nil if item_info.css("itemPricing #{param}")[0].blank? | |
item_info.css("itemPricing #{param}")[0].text | |
end | |
def get_description(item_info) | |
return nil if item_info.css('itemIdentification itemDescription').blank? | |
item_info.css('itemIdentification itemDescription')[0]['descriptionValue'].split('/')[0].strip | |
end | |
def get_attr_item(item_info, param) | |
return nil if item_info[param].blank? | |
item_info[param] | |
end | |
def get_status(item_info) | |
return nil if item_info.css('itemAvailability')[0].css('availQty')[0].blank? | |
item_info.css('itemAvailability')[0].css('availQty')[0]['value'] | |
end | |
def get_future_status(item_info) | |
item_info.css('itemAvailability').each do |i| | |
next if i.css('availQty')[0]['value'].to_i == 0 | |
if i.css('availQty')[0]['value'].to_i > 0 | |
return {date: i.css('availDate').text, status: i.css('availQty')[0]['value']} | |
end | |
end | |
end | |
def dimensions_inches_format(depth_inches, height_inches, p_char_weight) | |
# 63"W x 16"D x 37"H | |
return nil if depth_inches.blank? || height_inches.blank? | |
if p_char_weight.blank? | |
depth_inches + '"' + 'D' + ' x ' + height_inches + '"' + 'H' | |
else | |
p_char_weight + '"' + 'W' + ' x ' + depth_inches + '"' + 'D' + ' x ' + height_inches + '"' + 'H' | |
end | |
end | |
def create_dir_for_xml_files | |
dir_xml_files = "#{Rails.root}/vendor/xml_files" | |
dir_info_rooms = "#{Rails.root}/vendor/xml_files/xml_info_rooms" | |
dir_status_item = "#{Rails.root}/vendor/xml_files/xml_status_items" | |
FileUtils.mkdir(dir_xml_files) unless Dir.exist?(dir_xml_files) | |
FileUtils.mkdir(dir_info_rooms) unless Dir.exist?(dir_info_rooms) | |
FileUtils.mkdir(dir_status_item) unless Dir.exist?(dir_status_item) | |
end | |
def string_time_now | |
Time.now.strftime('%Y%m%d%H%M%S') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment