[英]Rails4: NameError: uninitialized constant Net - Heroku
Building a rails web scraper application that will return the current water supplier based on a UK postcode (eg: EC2A 4BX). 构建一个rails web scraper应用程序,该应用程序将根据英国邮政编码(例如:EC2A 4BX)返回当前的水供应商。 Created an initializer file and it works on local host but on Heroku I get error.
创建了一个初始化程序文件,它可以在本地主机上运行,但是在Heroku上却报错。 NameError: uninitialized constant Net
NameError:未初始化的常量Net
Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: rake aborted!
remote: NameError: uninitialized constant Net
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/config/initializers/thameswater.rb:10:in `<top (required)>'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/engine.rb:648:in `block in load_config_initializer'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.7/lib/active_support/notifications.rb:161:in `instrument'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/engine.rb:647:in `load_config_initializer'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/engine.rb:611:in `each'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/engine.rb:611:in `block in <class:Engine>'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:30:in `instance_exec'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:30:in `run'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:55:in `block in run_initializers'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:44:in `each'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:44:in `tsort_each_child'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/initializable.rb:54:in `run_initializers'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/application.rb:300:in `initialize!'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/config/environment.rb:5:in `<top (required)>'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/application.rb:276:in `require_environment!'
remote: /tmp/build_b54ea2f35a53dd4aa97f18e85d315c40/vendor/bundle/ruby/2.0.0/gems/railties-4.1.7/lib/rails/application.rb:389:in `block in run_tasks_blocks'
require 'open-uri'
require 'nokogiri'
#Store URL to be scraped
uri = URI.parse('https://secure.thameswater.co.uk/dynamic/cps/rde/xchg/corp/hs.xsl/Thames_Water_Supply.xml')
https = Net::HTTP.new(uri.host,uri.port) **<---Error**
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
https.use_ssl = true
# we need to define a Post object and send the postcode to search
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('postcode1' => 'RG1 8DB')
# do the post
result = https.request(req)
supplied = 'http://www.thameswater.co.uk/your-account/605_5460.htm'
not_supplied = 'http://www.thameswater.co.uk/your-account/605_5459.htm'
# evaluates the post result
if result.header['Location'] == supplied
puts 'Your property is in our supply area'
elsif result.header['Location'] == not_supplied
puts 'Sorry, we cant find your postcode'
end
Add to your config/initializers/thameswater.rb
: 添加到您的
config/initializers/thameswater.rb
:
require 'open-uri'
require 'nokogiri'
require 'net/http'
#Store URL to be scraped
uri = URI.parse('https://secure.thameswater.co.uk/dynamic/cps/rde/xchg/corp/hs.xsl/Thames_Water_Supply.xml')
# other code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.