Search code examples
ruby-on-railsabbyy

Cant send a business_card.png to abbyy with ROR


I am just tryout some new technologies and find out about abbyy gem I created a free account on http://ocrsdk.com/plans-and-pricing/

I am following the instruction on the gem

class Client < ActiveRecord::Base
  def abbyy
    client = Abbyy::Client.new
    client.process_business_card self.business_card, exportFormat: 'xml', imageSource: 'photo'
    # Errno::ENOENT: No such file or directory - https://appname-dev.s3.amazonaws.com/uploads/client/business_card/1/bizcard.jpg
    client.get_task_status
    client.get
  end
end

but I am getting a this error

Errno::ENOENT: No such file or directory - https://appname-dev.s3.amazonaws.com/uploads/client/business_card/1/bizcard.jpg

I made sure that the directory I am uploading is public

here is a link to a demo app https://github.com/mzaragoza/abbyy


Solution

  • Add require 'open-uri' to the top of your file.

    Then download the file, and only then give it to abby:

    def abby
      require 'tempfile'
      card = Tempfile.new('business_card')
      card.binmode
      stream = open(self.business_card.url)
      card.write(stream.read)
      stream.close
      card.close
      client = Abbyy::Client.new
      client.process_business_card card.path, exportFormat: 'xml', imageSource: 'photo'
      client.get_task_status
      client.get
    ensure
      # ensuring every handle is closed, and ignoring exceptions, which could arise if handles already closed
      # or haven't been opened
      stream.close rescue nil
      card.close rescue nil
      card.unlink rescue nil
    end