Skip to content

Instantly share code, notes, and snippets.

@jaturken
Last active August 29, 2015 14:18
Show Gist options
  • Save jaturken/780f2652cfcef3032647 to your computer and use it in GitHub Desktop.
Save jaturken/780f2652cfcef3032647 to your computer and use it in GitHub Desktop.
groups_categories.rb
def create_category(category_node)
url = category_node.xpath("./a[1]/@href").text
name = url.sub("http://catalog.onliner.by/","").delete('/')
name_ru = category_node.xpath("./a[last()]").text
is_new = category_node.xpath("./a[2]/img[@class='img_new']").any?
Category.create({ name: name, name_ru: name_ru, url: url, is_new: is_new })
end
def create_group(group_node)
name = group_node.text.delete("0-9")
Group.create({ name_ru: name })
end
groups = html.xpath("//h1[@class='cm__h1']")
categories_blocks = html.xpath("//ul[@class='b-catalogitems']")
groups.zip(categories_blocks).map do |group_node, categories_block|
group = create_group(group_node)
categories_block.xpath("./li").map do |category_node|
category = create_category(category_node)
group.add_category(category)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment