Class: Scraprr::AttributeScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/scraprr/attribute_scraper.rb

Constant Summary collapse

DEFAULT_PATH =
'.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path = DEFAULT_PATH, opts = {}) ⇒ AttributeScraper

Returns a new instance of AttributeScraper.



7
8
9
10
11
# File 'lib/scraprr/attribute_scraper.rb', line 7

def initialize(name, path = DEFAULT_PATH, opts = {})
  @name = name
  @path = path
  @chain = build_chain(opts)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/scraprr/attribute_scraper.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/scraprr/attribute_scraper.rb', line 3

def path
  @path
end

#requiredObject (readonly)

Returns the value of attribute required.



3
4
5
# File 'lib/scraprr/attribute_scraper.rb', line 3

def required
  @required
end

Instance Method Details

#build_chain(opts) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/scraprr/attribute_scraper.rb', line 13

def build_chain(opts)
  chain = ValueExtractor.new(opts[:attr], opts[:html])
  chain = RegexpFilter.new(chain, opts[:regexp]) if opts[:regexp]
  chain = StripFilter.new(chain) if opts[:strip]
  chain = RequiredFilter.new(chain, opts[:name]) if opts[:required]
  chain
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/scraprr/attribute_scraper.rb', line 25

def eql?(other)
  name.eql?(other.name)
end

#extract(element) ⇒ Object



21
22
23
# File 'lib/scraprr/attribute_scraper.rb', line 21

def extract(element)
  @chain.run(element)
end

#hashObject



29
30
31
# File 'lib/scraprr/attribute_scraper.rb', line 29

def hash
  name.hash
end