Module: LCBO::CrawlKit::Page

Included in:
InventoryPage, ProductListPage, ProductPage, StoreListPage, StorePage
Defined in:
lib/lcbo/crawlkit/page.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/lcbo/crawlkit/page.rb', line 5

def self.included(mod)
  mod.module_eval do
    include Eventable
    attr_reader :html, :query_params, :body_params, :response
    instance_variable_set :@request_prototype, RequestPrototype.new
    instance_variable_set :@fields, []
  end
  mod.extend(ClassMethods)
end

Instance Method Details

#[](field) ⇒ Object



78
79
80
# File 'lib/lcbo/crawlkit/page.rb', line 78

def [](field)
  as_hash[field.to_sym]
end

#as_hashObject



122
123
124
# File 'lib/lcbo/crawlkit/page.rb', line 122

def as_hash
  @as_hash ||= Hash[fields.map { |field| [field, send(field)] }]
end

#fieldsObject



86
87
88
# File 'lib/lcbo/crawlkit/page.rb', line 86

def fields
  self.class.fields
end

#http_methodObject



90
91
92
# File 'lib/lcbo/crawlkit/page.rb', line 90

def http_method
  self.class.http_method
end

#initialize(query_params = {}, body_params = {}, response = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lcbo/crawlkit/page.rb', line 66

def initialize(query_params = {}, body_params = {}, response = nil)
  if response
    @response     = response.is_a?(Hash) ? Response.new(response) : response
    @query_params = @response.query_params
    @body_params  = @response.body_params
    @html         = @response.body
  else
    @query_params = query_params
    @body_params  = body_params
  end
end

#is_parsed?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/lcbo/crawlkit/page.rb', line 118

def is_parsed?
  doc ? true : false
end

#parseObject



109
110
111
112
113
114
115
116
# File 'lib/lcbo/crawlkit/page.rb', line 109

def parse
  return if is_parsed?
  return unless @html
  fire :before_parse
  @doc = Nokogiri::HTML(@html)
  fire :after_parse
  self
end

#processObject



94
95
96
97
98
# File 'lib/lcbo/crawlkit/page.rb', line 94

def process
  request
  parse
  self
end

#requestObject



100
101
102
103
104
105
106
107
# File 'lib/lcbo/crawlkit/page.rb', line 100

def request
  return if @html
  fire :before_request
  @response = request_prototype.request(query_params, body_params)
  @html     = @response.body
  fire :after_request
  self
end

#request_prototypeObject



82
83
84
# File 'lib/lcbo/crawlkit/page.rb', line 82

def request_prototype
  self.class.request_prototype
end