Paperclip和S3错误:预期/app/lib/scraper/base.rb定义Base

时间:2014-10-16 20:01:57

标签: ruby-on-rails ruby amazon-s3 paperclip

我有一个使用PaperClip来管理图像和PDF的Rails 3应用程序。它正在使用文件系统,但现在我将其配置为使用s3存储。 因此,在我将模型配置为使用paperclip s3存储后,我收到了此错误:

LoadError: Expected /app/lib/scraper/base.rb to define Base
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:492:in `load_missing_constant'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:183:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `each'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:503:in `load_missing_constant'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:183:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `each'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:503:in `load_missing_constant'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:183:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `each'
  from /app/vendor/bundle/ruby/1.8/gems/activesupport-3.0.14/lib/active_support/dependencies.rb:181:in `const_missing'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/storage/s3.rb:96:in `extended'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/storage/s3.rb:78:in `instance_eval'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/storage/s3.rb:78:in `extended'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/attachment.rb:332:in `extend'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/attachment.rb:332:in `initialize_storage'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip/attachment.rb:67:in `initialize'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip.rb:386:in `new'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip.rb:386:in `attachment_for'
  from /app/vendor/bundle/ruby/1.8/gems/paperclip-2.3.15/lib/paperclip.rb:274:in `data'
  from /app/app/models/attachment.rb:30:in `url'
  from (irb):2irb(main):003:0> LoadError (Expected /app/lib/scraper/base.rb to define Base):

/app/lib/scraper/base.rb

的内容
require 'open-uri'

module Scraper

  # old version, used by gendepot scraper
  class Base
    include Scraper

    attr_reader :url, :document, :project, :browser, :options

    def initialize(url, options = {})
      @options = options
      @options[:test] ||= false

      @url = url
      @project = ScraperProject.find_by_scraper_class self.class.name.split('::').last
      raise ProjectNotDefined.new(url) unless @project

      @browser = Browser.new url
      @document = @browser.get
    end

    def scrape
      raise 'subclass responsability'
    end

    def test?
      options[:test]
    end

    def output(html)
      File.open(Rails.root + 'lib/tasks/data/scraper_output.html', 'w+') do |f|
        f << html
      end
    end

    def store(data)
      return if test?

      if !ScraperData.exists?(data)
        record = project.data.build data
        if record.save
          logger.debug "Scrape Data Created: #{data.inspect}"
        else
          error_info = [
            "Scraper: #{self.class.name}", 
            "Error: #{record.errors.full_messages}",
            "URL: #{url}",
            "Data: #{data.to_yaml}"
          ]
          errors.debug error_info.join("\n") 
        end

      elsif record = ScraperData.find_by_name(data[:name])
        data.stringify_keys!
        attrs = record.attributes
        attrs.delete_if { |k,v| !data.keys.include?(k) }

        if data != attrs
          record.update_attributes(data)
          logger.debug "Scrape Data ##{record.id} Updated: #{data.inspect}"
        end
      end
    end

  end

  class Browser
    include Scraper

    attr_reader :domain, :url

    def self.headers
      { "User-Agent" => Scraper::USER_AGENT }
    end

    def initialize(url)
      @uri = URI url
      @url = url
      @domain = scheme_host_port
    end

    def get(uri = '')
      if uri.empty?
        url = self.url
      else
        url = uri
        url = domain + url unless uri.include?('http://')
        logger.debug "Browser get: #{url}"
      end

      @url = url
      Nokogiri::HTML(open(url, Browser.headers).read)
    end

    private

      def scheme_host_port
        domain = @uri.scheme + '://' + @uri.host
        domain += ":#{@uri.port}" unless @uri.port == 80
        domain
      end
  end

  # Mechanize version
  class Agent < Base
    include Scraper

    attr_reader :page

    def initialize(url, options = {})
      super      
      @browser = Mechanize.new { |agent| agent.user_agent = Scraper::USER_AGENT }
      @browser.agent.keep_alive = false
      @browser.log = Logger.new "#{Rails.root}/log/mechanize.log"

      def @browser.get_with_retry(uri, trys = 3)
        begin
          page = self.get uri
          block_given? ? yield(page) : page
        rescue => e
          trys -= 1
          retry if trys > 0
        end
      end

      @page = @browser.get @url
    end

    def with_agent_alias(user_agent_alias, &block)
      @browser.user_agent_alias = user_agent_alias
      yield
      @browser.user_agent = Scraper::USER_AGENT
    end

  end



end

提前致谢!!!


看到other post with a similar problem后,我发现问题是rails加载代码的方式。 所以请记住代码是使用rails autoload配置加载的,我编辑它来加载我的所有lib但/ Scraper,因为该代码加载了Autoload

    config.autoload_paths += Dir["#{config.root}/lib/**/"] - ["#{config.root}/lib/scraper/"]

0 个答案:

没有答案