Method: Grubby::Scraper.scrapes
- Defined in:
- lib/grubby/scraper.rb
.scrapes(field, **options, &block) ⇒ void
This method returns an undefined value.
Defines an attribute reader method named by field
. During #initialize, the given block is called, and the attribute is set to the block’s return value.
By default, raises an exception if the block’s return value is nil. To prevent this behavior, set the :optional
option to true. Alternatively, the block can be conditionally evaluated, based on another method’s return value, using the :if
or :unless
options.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/grubby/scraper.rb', line 68 def self.scrapes(field, **, &block) field = field.to_sym (self.fields << field).uniq! define_method(field) do raise "#{self.class}#initialize does not invoke `super`" unless defined?(@scraped) if !@scraped.key?(field) && !@errors.key?(field) begin skip = ([:if] && !self.send([:if])) || ([:unless] && self.send([:unless])) if skip @scraped[field] = nil else @scraped[field] = instance_eval(&block) if @scraped[field].nil? raise FieldValueRequiredError.new(field) unless [:optional] $log.debug("#{self.class}##{field} is nil") end end rescue RuntimeError, IndexError => e @errors[field] = e end end if @errors.key?(field) raise FieldScrapeFailedError.new(field, @errors[field]) else @scraped[field] end end end |