模型初始化-传递参数-Rails 5

时间:2019-03-08 00:05:17

标签: ruby-on-rails ruby model

我在初始化模型时遇到麻烦。

我正在尝试构建一个模型,该模型在初始化时使用url_address字符串,利用DocRaptor构建PDF,然后使用pdf-reader读取该PDF中的数据,最后删除存储的PDF。

当我有一个空白模型时,可以按照Rails 5入门指南在https://guides.rubyonrails.org/getting_started.html

中使用Rails 5表单进行保存。

我使用PostgreSQL数据库从头开始构建了一个新项目,并开始使用Guide。我能够使用空白的UrlDataModel定义保存模型。但是,当我编辑模型以包含检索和阅读PDF所需的功能时,我会得到

NoMethodError - undefined method '[]' for nil:Class

我确实知道此错误意味着我的模型为空,所以我正在尝试解决这种情况。

控制器出现故障

@url_data_model.save

我的仓库在这里:https://github.com/blueMesaEngineering/Minotaur-hoof

模型如下:


class UrlDataModel < ApplicationRecord
  attr_accessor :url_address, :pdf_version, :producer, :title, :metadata, :page_count

  def initialize(attributes = {})
    @url_address = attributes[:url_address]
    @pdf_version = attributes[:pdf_version]
    @producer = attributes[:producer]
    @title = attributes[:title]
    @metadata = attributes[:metadata]
    @page_count = attributes[:page_count]
  end

  def after_initialize
    buildModelFromURLViaPDF
  end

  def buildModelFromURLViaPDF
    convertURLToPDF
    readPDFData
    deletePDF
  end

  def convertURLToPDF
    require 'bundler/setup'
    Bundler.require

    DocRaptor.configure do |dr|
      dr.username = 'YOUR_API_KEY_HERE' # this key works for test documents
      # dr.debugging = true
    end

    $docraptor = DocRaptor::DocApi.new

    begin
      logPathName = './storage/Logs/standardOutput/output.txt'
      errorLogPathName = './storage/Logs/Error/'
      pathName = './storage/PDFs/'
      # url         = "http://docraptor.com/examples/invoice.html"
      url = 'http://www.docraptor.com'
      @url_address = url

      fileNamePDF  = 'docraptor-ruby.pdf'

      create_response = $docraptor.create_async_doc(
        test: true, # test documents are free but watermarked
        document_url: url, # or use a url
        name: fileNamePDF, # help you find a document later
        document_type: 'pdf' # pdf or xls or xlsx
      )

      loop do
        status_response = $docraptor.get_async_doc_status(create_response.status_id)

        # puts "doc status: #{status_response.status}"

        case status_response.status

        when 'completed'
          doc_response = $docraptor.get_async_doc(status_response.download_id)
          File.open('./storage/PDFs/docraptor-ruby.pdf', 'wb') do |file|
            file.write(doc_response)
          end
          # puts "Wrote PDF to " + pathName + fileNamePDF

          break

        when 'failed'

          # puts "FAILED"
          # puts status_response
          break

        else

          sleep 1

      end
      end
    rescue DocRaptor::ApiError => error
      # puts "#{error.class}: #{error.message}"
      # puts error.code          # HTTP response code
      # puts error.response_body # HTTP response body
      # puts error.backtrace[0..3].join("\n")
    end
  end

  def readPDFData
    require 'rubygems'
    require 'pdf/reader'

    fileName = './storage/PDFs/docraptor-ruby.pdf'

    PDF::Reader.open(fileName) do |reader|
      @pdf_version = reader.pdf_version
      # @producer   = reader.producer
      # @title     = reader.title
      @metadata = reader.metadata
      @page_count = reader.page_count
    end
  end

  def deletePDF
    require 'fileutils'

    FileUtils.rm_rf('./storage/PDFs/docraptor-ruby.pdf')
  end
end

您的想法是Penny?我将在这里度过余下的晚上,因此我将能够直接做出回应。谢谢您的光临!

干杯。

控制器:https://pastebin.com/QVaHCMep

堆栈跟踪:https://pastebin.com/uQbXgiaH

0 个答案:

没有答案