Class: Botz::Scraper::DirectHtml
- Inherits:
-
Object
- Object
- Botz::Scraper::DirectHtml
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- lib/botz/scraper/direct_html.rb
Overview
direct resource to html scraping
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#scraper_class ⇒ Object
readonly
Returns the value of attribute scraper_class.
Class Method Summary collapse
-
.field(name, path = nil, persist: true, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- .field_names ⇒ Object
Instance Method Summary collapse
- #call {|to_h| ... } ⇒ Object
-
#initialize(scraper_class, resource) ⇒ DirectHtml
constructor
A new instance of DirectHtml.
- #to_h ⇒ Object
Constructor Details
#initialize(scraper_class, resource) ⇒ DirectHtml
Returns a new instance of DirectHtml.
22 23 24 25 |
# File 'lib/botz/scraper/direct_html.rb', line 22 def initialize(scraper_class, resource) @scraper_class = scraper_class @html = resource end |
Instance Attribute Details
#html ⇒ Object (readonly)
Returns the value of attribute html.
20 21 22 |
# File 'lib/botz/scraper/direct_html.rb', line 20 def html @html end |
#scraper_class ⇒ Object (readonly)
Returns the value of attribute scraper_class.
19 20 21 |
# File 'lib/botz/scraper/direct_html.rb', line 19 def scraper_class @scraper_class end |
Class Method Details
.field(name, path = nil, persist: true, &block) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/botz/scraper/direct_html.rb', line 47 def self.field(name, path = nil, persist: true, &block) if persist field_names << name case name when /.*\?/ validates name, inclusion: { in: [true, false] } else validates name, presence: true, allow_blank: true end end return define_method(name) { instance_exec(html, &block) } if path.nil? return define_method(name) { html.search(path).text.strip } if block.nil? define_method(name) { html.search(path).first.try { |e| instance_exec(e, &block) } } end |
.field_names ⇒ Object
28 29 30 |
# File 'lib/botz/scraper/direct_html.rb', line 28 def field_names @field_names ||= [] end |
Instance Method Details
#call {|to_h| ... } ⇒ Object
40 41 42 43 44 |
# File 'lib/botz/scraper/direct_html.rb', line 40 def call fail Error.new(scraper_class, errors) if invalid? yield(to_h) end |
#to_h ⇒ Object
33 34 35 36 37 38 |
# File 'lib/botz/scraper/direct_html.rb', line 33 def to_h fetched_at = Time.current fetched_on = fetched_at.beginning_of_day = { fetched_on: fetched_on, fetched_at: fetched_at } self.class.field_names.map { |field| [field, send(field)] }.to_h.merge() end |