Ищете советы по устранению этой ошибки и рефакторинг этого кода, чтобы улучшить его.Механизировать в модуле, агент «Имущество»
require 'mechanize'
require 'pry'
require 'pp'
module Mymodule
class WebBot
agent = Mechanize.new { |agent|
agent.user_agent_alias = 'Windows Chrome'
}
def form(response)
require "addressable/uri"
require "addressable/template"
template = Addressable::Template.new("http://www.domain.com/{?query*}")
url = template.expand({"query" => response}).to_s
page = agent.get(url)
end
def get_products
products = []
page.search("datatable").search('tr').each do |row|
begin
product = row.search('td')[1].text
rescue => e
p e.message
end
products << product
end
products
end
end
end
Вызов модуля:
response = {size: "SM", color: "BLUE"}
t = Mymodule::WebBot.new
t.form(response)
t.get_products
Ошибка:
NameError: undefined local variable or method `agent'
ошибки имен, хотя и не синтаксические ошибки. – pguardiario
Да. Полностью верно. Я исправлю это. –
Работает, отлично, спасибо. Любые советы по очистке кода? – user2012677