Class: Botz::Scraper::DirectHtml

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#htmlObject (readonly)

Returns the value of attribute html.



20
21
22
# File 'lib/botz/scraper/direct_html.rb', line 20

def html
  @html
end

#scraper_classObject (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_namesObject



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

Yields:



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_hObject



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
  timestamps = { fetched_on: fetched_on, fetched_at: fetched_at }
  self.class.field_names.map { |field| [field, send(field)] }.to_h.merge(timestamps)
end