Giter Club home page Giter Club logo

yandex_market-parser's Introduction

YandexMarket::Parser Build Status

YandexMarket::Parser is a parsers generator. Generated parsers are SAX-based XML parsers for YandexML files.

Installation

Add this line to your application's Gemfile:

gem 'yandex_market-parser', :git => "git://github.com/take-five/yandex_market-parser.git"

And then execute:

$ bundle

Usage

The way to customize processing of Yandex.Market XML files is to define Parser and Controller.

Parser is responsible for reading file, recognizing its structure and map XML-nodes to Ruby objects. Controller is responsible for the rest part of work - it processes objects received from Parser.

To create a parser you should create a new class - successor of YandexMarket::Parser::Base, and configure YML-specific sections. In each section you should define of which attributes you are interested. Some attributes are already mapped to standard XML-nodes. Anyway, you can map some specific attributes by yourself.

class MyCoolParser < YandexMarket::Parser::Base
  configure.catalog do |c|
    c.collect :date
  end

  configure.offers do |c|
    c.xpath 'oferta/@id' => :oferta_id # custom mapping
    c.collect :id, :price, :oferta_id
    # you can specify a base class for generated classes (it should be successor of YandexMarket::Model)
    c.base_class MyCoolOffer
    # alternatively you can specify a concrete class for section
    c.instantiate MyCoolOffer # no additional classes shall be generated
  end
end

To create a controller you should create a new class - successor of YandexMarket::Controller::Base. You should define dispatch rules for main YML-objects: catalog, shop, currency, category, offer. Optionally you can add hooks for every handler. E.g. if you set up your dispatch rules to route offer nodes to handle_offer, you can declare before_handle_offer and after_handle_offer hooks.

class MyCoolController < YandexMarket::Controller::Base
  dispatch do |d|
    d.route 'yml_catalog' => :catalog,
            'shop' => :shop,
            'currency' => :currency,
            'category' => :category,
            'offer' => :offer
  end

  before_offer :before_offer_hook

  def catalog(o)
  end

  def shop(o)
  end

  def currency(o)
  end

  def category(o)
  end

  def offer(o)
  end

  private
  def before_offer_hook(offer)
  end
end

There is already few predefined controllers, they are designed mainly for testing purposes:

  1. YandexMarket::Controller::Naive - it just stores all objects to array, and it is accessible by method objects
  2. YandexMarket::Controller::Stats - counts nodes by node type, statistics is accessible by method stats

And now you can parse YML-files:

controller = MyCoolController.new
parser  = MyCoolParser.new(controller)
parser.parse_stream(File.open('/tmp/yandex.xml'))

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

yandex_market-parser's People

Contributors

take-five avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.