Class: Storexplore::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/storexplore/dsl.rb

Overview

Implementation of the DSL used to define new APIs on real web stores. See README for examples

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Initializes a new instance with no special categories, items or attributes definition. (Internal usage)



38
39
40
41
42
43
# File 'lib/storexplore/dsl.rb', line 38

def initialize
  @configure_agent_block = lambda do |_| {} end
  @scrap_attributes_block = lambda do |_| {} end
  @categories_digger = NullDigger.new
  @items_digger = NullDigger.new
end

Class Method Details

.walker_builder(&block) ⇒ Object

Starts the definition of the API. Evaluates the block with the context of an instance of Storexplore::Dsl



30
31
32
33
34
# File 'lib/storexplore/dsl.rb', line 30

def self.walker_builder(&block)
  new.tap do |dsl|
    dsl.instance_eval(&block)
  end
end

Instance Method Details

#agent(&block) ⇒ Object

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.



49
50
51
# File 'lib/storexplore/dsl.rb', line 49

def agent(&block)
  @configure_agent_block = block
end

#attributes(&block) ⇒ Object

Registers the block to be used to extract attributes from a store page. Block will be evaluated within the context of a Storexplore::WalkerPage



55
56
57
# File 'lib/storexplore/dsl.rb', line 55

def attributes(&block)
  @scrap_attributes_block = block
end

#categories(selector, &block) ⇒ Object

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



63
64
65
# File 'lib/storexplore/dsl.rb', line 63

def categories(selector, &block)
  @categories_digger = Digger.new(selector, Dsl.walker_builder(&block))
end

#configure_agent(agent) ⇒ Object

Initializes the mechanize agent with the given setup

  • agent : the mechanize agent that will be used throughout the walking

(Internal usage)



75
76
77
# File 'lib/storexplore/dsl.rb', line 75

def configure_agent(agent)
  @configure_agent_block.call(agent)
end

#items(selector, &block) ⇒ Object

Same as #categories, but for child items



68
69
70
# File 'lib/storexplore/dsl.rb', line 68

def items(selector, &block)
  @items_digger = Digger.new(selector, Dsl.walker_builder(&block))
end

#new_walker(page_getter, father = nil, index = nil) ⇒ Object

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)



85
86
87
88
89
90
91
92
93
# File 'lib/storexplore/dsl.rb', line 85

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