class Storexplore::Dsl
Implementation of the DSL used to define new APIs on real web stores. See README for examples
Public Class Methods
Initializes a new instance with no special categories, items or attributes definition. (Internal usage)
# File lib/storexplore/dsl.rb, line 37 def initialize @configure_agent_block = lambda do |_| {} end @scrap_attributes_block = lambda do |_| {} end @categories_digger = NullDigger.new @items_digger = NullDigger.new end
Starts the definition of the API. Evaluates the block with the context of an instance of Storexplore::Dsl
# File lib/storexplore/dsl.rb, line 29 def self.walker_builder(&block) new.tap do |dsl| dsl.instance_eval(&block) end end
Public Instance Methods
Registers a block that can customize the Mechanize Agent that will be used throughout the the store digging. This can be useful to setup custom cookies for example. Ignored anywhere except on the top level store definition.
# File lib/storexplore/dsl.rb, line 48 def agent(&block) @configure_agent_block = block end
Registers the block to be used to extract attributes from a store page. Block will be evaluated within the context of a Storexplore::WalkerPage
# File lib/storexplore/dsl.rb, line 54 def attributes(&block) @scrap_attributes_block = block end
Defines how to find child categories.
-
selector is the nokogiri selector to match links to these categories
-
the block defines how to scrap these children categories, will be evaluated within the context of the child
Storexplore::Dsl
instance
# File lib/storexplore/dsl.rb, line 62 def categories(selector, &block) @categories_digger = Digger.new(selector, Dsl.walker_builder(&block)) end
Initializes the mechanize agent with the given setup
-
agent : the mechanize agent that will be used throughout the walking
(Internal usage)
# File lib/storexplore/dsl.rb, line 74 def configure_agent(agent) @configure_agent_block.call(agent) end
Same as categories
, but for child items
# File lib/storexplore/dsl.rb, line 67 def items(selector, &block) @items_digger = Digger.new(selector, Dsl.walker_builder(&block)) end
Initializes a new Storexplore::Walker
instance based on specified custom definitions from the instance.
-
page_getter : proxy to the page that we want to explore
-
father : parent
Storexplore::Walker
instance (troubleshouting) -
index : index of the page within its brothers
(Internal usage)
# File lib/storexplore/dsl.rb, line 84 def new_walker(page_getter, father = nil, index = nil) Walker.new(page_getter).tap do |walker| walker.categories_digger = @categories_digger walker.items_digger = @items_digger walker.scrap_attributes_block = @scrap_attributes_block walker.father = father walker.index = index end end