Skip to content

Instantly share code, notes, and snippets.

@shamritskiy3468
Created March 5, 2019 08:04
Show Gist options
  • Save shamritskiy3468/c6a1fd3d2ca6a3feed027df216af2dde to your computer and use it in GitHub Desktop.
Save shamritskiy3468/c6a1fd3d2ca6a3feed027df216af2dde to your computer and use it in GitHub Desktop.
class Product
attr_accessor :name, :sku, :price, :regular_price, :promoname
def initialize(name, sku, price, regular_price, promoname)
@name = name
@sku = sku
@price = price
@regular_price = regular_price
@promoname = promoname
@stock = stock
end
end
class ProductBuilder
def self.build
builder = new
yield(builder)
builder.product
end
def initialize
@product = Product.new
end
def set_name(name)
@product.name = name
end
def set_stock(stock)
@product.stock = stock
end
def set_price(price)
@product.price = price
end
def set_regular_price(regular_price)
@product.regular_price = regular_price
end
def set_sku(sku)
@product.sku = sku
end
def set_promoname(promoname)
@product.promoname = promoname
end
def product
@product
end
end
class EducationGarfieldParser < Scripting::CustomParser
@document
@base_prod
@ctx
@list_products
def parse(ctx)
@ctx = ctx
@base_prod = ctx.base_product
@document = ctx.doc
if page_valid?
if @base_prod
products = products_json
create_prices_from_json(products["OFFERS"])
if products["OFFERS"]
@list_products = extract_products_from_list
products["OFFERS"].each_with_index do |single_product, index|
ctx.add_product(create_single_product(single_product, index))
end
end
end
else
log "******* PAGE NOT VALID *********"
end
ctx.products.empty?
end
def page_valid?
prod_variations = @document.xpath('//div[@class="row item_detailed_header"]//ul[@class="product-item-scu-item-list item_prop_list"]//li')
@document.xpath('//div[contains(@class, "bx-catalog-element bx-blue")]/@itemtype').text.strip.include?('//schema.org/Product') && (prod_variations.size > 1)
end
def create_prices_from_json(offers)
result = offers.each_with_object([]) { |offer, arr| arr << offer["ITEM_PRICES"].first["BASE_PRICE"] }
end
def get_prices_for_comparison
#@list_products.each_with_object {|list_element, res| res << list_element.xpath('')
end
def create_single_product(product, index)
p = @base_prod.dup
p[NAME] = product["NAME"]
p[PRICE] = product["ITEM_PRICES"].last["BASE_PRICE"]
p[SKU] = p[KEY] = product["ID"]
# @list_products.each do |item|
# p[PROMONAME] = "Акция" if item.xpath('//@data-discountstatus').text.strip.include?("item_discounted")
# end
if product["NAME"].include?(@list_products[index].xpath('.//@title').text.strip) && @list_products[index].xpath('.//@data-discountstatus').text.strip.include?('item_discounted')
p[PROMONAME] = "URAAAAAAAAAAAAA"
end
# not valid confirmation on page
# weight = separate_prod.xpath('.//@title').text.strip
# compare_hash = get_prices_with_weights
# p[PROMO_NAME] = separate_prod.xpath('.//@data-discountstatus').text.strip.include?("item_discounted") ? "Акция" : nil
# p[REGULAR_PRICE] = separate_prod.xpath('.//@data-old-price').text.strip.chomp('руб.') if !separate_prod.xpath('.//@data-old-price').empty?
# p[STOCK] = separate_prod.xpath(".//@data-availstatus").equal?("") ? "out_of stock" : "in stock"
# hash work attemps here
# end
p
end
def product_from_page
products = []
@document.xpath('//div[@class="product-item-scu-item-list item_prop_list"]//li').each do |product|
end
end
def extract_products_from_list
@document.xpath('//div[@class="row item_detailed_header"]//ul[@class="product-item-scu-item-list item_prop_list"]//li')
end
def prices_from_list
@document.xpath('//span[@itemprop="offers"]//meta[@itemprop="price"]//@content').map { |node| node.text.strip }
end
def products_json
script = find_description_script
json = prepare_json(script).tr("'", "\"")
JSON.parse(json)
end
def multi_product_count
products_json["OFFERS"].size
end
def find_description_script
@ctx.todo[:javascript].find { |scr| scr.include?("JCCatalogElement") }
end
def prepare_json(script)
script.scan(/(?<=JCCatalogElement\().+?(?=\);)/).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment