Skip to content

Instantly share code, notes, and snippets.

@serg2801
Last active August 1, 2017 10:57
Show Gist options
  • Save serg2801/92402c76c85aa92bdb0380ca5e047f11 to your computer and use it in GitHub Desktop.
Save serg2801/92402c76c85aa92bdb0380ca5e047f11 to your computer and use it in GitHub Desktop.
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
VOLUME = 'volume' # param in xml file
WEIGHT = 'weight' # param in xml file
ITEM_IS_AVAILABLE = 'itemIsAvailable' # param in xml file
RETAIL_SALES_CATEGORY = 'retailSalesCategory' # param in xml file
ITEM_STATUS = 'itemStatus' # param in xml file
IMAGE = 'image' # param in xml file
HARMONIZATION_CODE = 'harmonizationCode' # param in xml file
UNIT_PRICE = 'unitPrice' # param in xml file
NET_PRICE = 'netPrice' # param in xml file
FREIGHT_AMOUNT = 'freightAmount' # param in xml file
DISCOUNT_AMOUNT = 'discountAmount' # param in xml file
def download_xml_status_items
ftp = Net::FTP.new('******')
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.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_is_available = get_attr_item(item_info, ITEM_IS_AVAILABLE)
retail_sales_category = get_attr_item(item_info, RETAIL_SALES_CATEGORY)
item_status = get_attr_item(item_info, ITEM_STATUS)
image = get_attr_item(item_info, IMAGE)
harmonization_code = get_attr_item(item_info, HARMONIZATION_CODE)
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)
item_char_depth_cm = get_size_cm_item_char(item_info, DEPTH)
item_char_height_cm = get_size_cm_item_char(item_info, HEIGHT)
item_char_length_cm = get_size_cm_item_char(item_info, LENGTH)
package_char_depth_in = get_size_in_item_package(item_info, DEPTH)
package_char_height_in = get_size_in_item_package(item_info, HEIGHT)
package_char_length_in = get_size_in_item_package(item_info, LENGTH)
package_char_volume_cubic_feet = get_size_in_item_package(item_info, VOLUME)
package_char_weight_lb = get_size_in_item_package(item_info, WEIGHT)
package_char_depth_cm = get_size_cm_item_package(item_info, DEPTH)
package_char_height_cm = get_size_cm_item_package(item_info, HEIGHT)
package_char_length_cm = get_size_cm_item_package(item_info, LENGTH)
unit_price = get_item_pricing(item_info, UNIT_PRICE)
net_price = get_item_pricing(item_info, NET_PRICE)
freight_amount = get_item_pricing(item_info, FREIGHT_AMOUNT)
discount_amount = get_item_pricing(item_info, DISCOUNT_AMOUNT)
dimensions_inches_format = dimensions_inches_format(item_char_depth_in, item_char_height_in, package_char_weight_lb)
item.update
rescue StandardError => e
puts e.message
end
def item_update_status(item, item_info, xml_file_id)
status = get_status(item_info)
item.update
rescue StandardError => e
puts e.message
end
def item_save_image(item, item_info)
img_name = get_attr_item(item_info, IMAGE)
puts "#{item.id.to_s + ' ' + item.item_number.to_s}"
img_name.slice! '_BIG' unless img_name.blank?
url = "path#{img_name}"
open(url) do |f|
item.image = f
end
item.save!
rescue StandardError => e
if e.message == '404 Not Found'
puts "#{item.id.to_s + ' ' + item.item_number.to_s} not found full size"
url = "https://path#{get_attr_item(item_info, IMAGE)}"
open(url) do |f|
item.image = f
end
item.save!
else
puts e.message
end
rescue StandardError => er
puts er.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_cm_item_char(item_info, param)
item_char = item_info.css('itemIdentification itemCharacteristics')
return nil if item_char[1].blank? || item_char[1].css("itemDimensions #{param}")[0].blank?
item_char[1].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_size_cm_item_package(item_info, param)
p_char = item_info.css('itemIdentification packageCharacteristics')
return nil if p_char[1].blank? || p_char[1].css("packageDimensions #{param}")[0].blank?
p_char[1].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 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
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