uninitialized constant Google::Cloud::Vision::ImageAnnotator - ruby-on-rails

Running this example
https://cloud.google.com/vision/docs/face-tutorial?hl=zh-tw
by rails on 'google-cloud-vision', '~> 0.31.0'
got the error
uninitialized constant Google::Cloud::Vision::ImageAnnotator
here's the code
require "google/cloud/vision"
project_id = 'xxxxxx'
vision = Google::Cloud::Vision.new project: project_id
image_annotator = Google::Cloud::Vision::ImageAnnotator.new

Did you added google-cloud-ruby gem in your Gemfile and other google related authentication details into your ruby project.

It turns out that the error was caused because I haven't use the latest version of gcloud-vision(0.32.x)
the way to do what I wanna do in gcloud-vision(0.31.0) is
image = vision.image "path/to/face.jpg"
face = image.face
instead of using class
Google::Cloud::Vision::ImageAnnotator

Related

How to fix "undefined method `as_png'" for rqrcode gem

I'm getting this error when I try to use the rqrcode gem to turn a QR code into a PNG file:
undefined method `as_png' for #<RQRCodeCore::QRCode:0x00007f90b42ec330>
Here is the code:
self.secure_hex = SecureRandom.hex
self.qr_code = RQRCodeCore::QRCode.new("https://app.mapviapp.com/check_ins/new?d=#{self.secure_hex}")
self.qr_code_image = RQRCodeCore::QRCode.new("https://app.mapviapp.com/check_ins/new?d=#{self.secure_hex}").as_png
and the API documentation.
Any thoughts on what could be going wrong?
I'm not sure why you're initializing RGRCodeCore::QRCode instead of RQRCode::QRCode as dinjas pointed out. Try changing it this way as the documentation suggests to do:
require 'rqrcode'
self.secure_hex = SecureRandom.hex
self.qr_code = RQRCode::QRCode.new("https://app.mapviapp.com/check_ins/new?d=#{self.secure_hex}")
self.qr_code_image = RQRCode::QRCode.new("https://app.mapviapp.com/check_ins/new?d=#{self.secure_hex}").as_png
Also, in the code you shared, you create an instance of self.qr_code, which contains the exact string as the self.qr_code_image instance. Are you sure you need that?

Rails 5 gem gon not recognize jbuilder file

I have implemented rails gem gon in index method and it works fine. But when I try to implement in controller#new it is not working this is my error output:
No such file or directory # rb_sysopen - app/views/cuenta_proveedors/new.json.jbuilder
but i have already create this file.
this is my controller
def new
#cuenta_proveedor = CuentaProveedor.new
#cuentas = #negocio.cuenta_proveedors.order(:created_at)
gon.jbuilder // THIS LINE CAUSE THE ERROR SHOWN BY APPLICATION TRACE
end
i have the created the file new.json.jbuiler
json.cuenta_proveedores #cuentas do |cc|
json.proveedor cc.proveedor.nombre
json.inicial cc.monto_inicial
json.saldo cc.monto_adeudado
json.observacion cc.observacion
json.token cc.token
end
Why is this happening? I can not found any logic to this error. If you need more information ask me . Thanks

Ruby on Rails Google API

I'm currently doing a Ruby on Rails project where I try to query from the Google Calendar API. I'm using this line of code:
service = Google::Apis::CalendarV3::CalendarService.new
and I have the gem google-api-client in my Gemfile. However, when I try to run the app with this line, it fails saying that it is Unknown Constant: Google Apis. If I try to put in the line that I've seen on StackOverflow, where I put require 'google/apis/calendar_v3' in the ruby file, it fails. I've spent 4 hours online trying to find anyone with a similar solution. If I try another API, like require 'google/apis/drive_v2', it works fine, just calendar. :(
The following works for me on Rails 4.2.1 -- Note that there are two requires.
require 'google/apis'
=> true
require 'google/apis/calendar_v3'
=> true
service = Google::Apis::CalendarV3::CalendarService.new
=> #<Google::Apis::CalendarV3::CalendarService:0x007fca01ae60a0 #root_url="https://www.googleapis.com/", #base_path="calendar/v3/", #upload_path="upload/calendar/v3/", #batch_path="batch", #client_options=#<struct Google::Apis::ClientOptions application_name="unknown", application_version="0.0.0", proxy_url=nil, use_net_http=false>, #request_options=#<struct Google::Apis::RequestOptions authorization=nil, retries=0, header=nil, timeout_sec=nil, open_timeout_sec=20>>
My gem file contains:
gem 'google-api-client', '0.9'

Rails 5 cipher.key "key must be 32 bytes" error

Brand new Rails application.
Rails version 5.0.0.1, Ruby version 2.4.0preview2.
Create application "demo", run a simple scaffold generate Product, and get an error when trying to view the scaffold's overview page (base index file still loads the Welcome to Rails screen fine):
ArgumentError in ProductsController#index
key must be 32 bytes:
cipher = new_cipher
cipher.encrypt
cipher.key = #secret
# Rely on OpenSSL for the initialization vector
iv = cipher.random_iv
The problem line is apparently cipher.key = #secret.
I've seen various mentions on the github repo for Rails mentioning this issue, but all implied it was now resolved in Rails 5.0.0.1
Ok, there was a slight misunderstanding on my part, looks like the fix is coming in 5.0.1 not 5.0.0.1
https://github.com/rails/rails/issues/26694
Please use Digest::MD5 to achive 32 bytes
require 'openssl'
require 'digest'
require 'base64'
data = "encrypt me"
secret_key = "asd3dssdf34HDas"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::MD5.hexdigest(secret_key) # this will convert key length into 32
encrypted_data = c.update(data.to_s) + c.final
encrypted_data = Base64.urlsafe_encode64(encrypted_data, padding: false) #padding: false will remove '/', '+' from encrypted data
encrypted_data.gsub! "\n",""
Or Simply use secret key of length 32 bytes
data = "encrypt me"
secret_key = "Aswertyuioasdfghjkqwertyuiqwerty"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = secret_key
encrypted_data = c.update(data.to_s) + c.final
Finally found problem! It was from a bugfix... https://bugs.ruby-lang.org/issues/12561
If you are using cipher e.g. 'aes-256-cfb', the key_len is 32, found by:
require 'openssl'
cipher = OpenSSL::Cipher.new('aes-256-cfb')
cipher.key_len # => 32
We had mistakenly thought we needed to send a 256 character nonce, but actually you are supposed to send a 32 character nonce - or
use cipher.random_key (which internally uses the key_len). It never used to be a problem because openssl truncated the nonce... but now you need to send the right lengthed nonce.
We got this error upgrading ruby from 2.3.4 to 2.4.2.
This issue turns out to be connected to the key you are using. Without changing your key you can use the code below to transform your key to 32 bytes:
attr_encrypted :attribute, key: ENV['MY_KEY'].bytes[0..31].pack( "c" * 32 )
try this:
rake db:create
rake db:migrate
then, the most important thing:
bundle update
This works for me.
Use random_key so it always fit.
key = cipher.random_key
cipher.key = key
reference http://ruby-doc.org/stdlib-2.0.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html
Solution:
Edit your Gemfile
Add the following line: gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
bundle install
Optional: I am using ruby2-4.1 . (rvm install ruby-2.4.1)
Rational: The rails version prior to 5.0.0 seems to have a bug that causes this issue. The bug has been resolved in the latest version of Rails. If you hare following the Rails Installation Guide (http://railsapps.github.io/installrubyonrails-mac.html) you will probably encounter this problem as of this posting date.
This fix does work, and is verified by
I was having this problem too and fixed it by running
bundle update
Make sure that you have the latest version of rails installed.
Had the same error:
Running bundle update should do the trick

Rails asset pipeline Rails.application.assets.instance_variable_get('#environment') returns nil

I am trying to compile javascript dynamically and then adding it to the sprockets store so it is available. Everywhere I researched suggested the below code to register the javascript:
env = Rails.application.assets.is_a?(Sprockets::Index) ? Rails.application.assets.instance_variable_get('#environment') : Rails.application
Rails.application.config.assets.digests[file_name] = env[file_name].digest_path
in production, Rails.application.assets.instance_variable_get('#environment') always returns nil, is there something I am doing wrong? or should I be adding something else?
Rails.application.assets itself is an instance of Sprockets::Environment
#environment' is a variable of assets_manifest, that belongs to Rails.application, like this:
Rails.application.instance_variable_get('#assets_manifest').instance_variable_get('#environment')
I got similar problems with RAILS 3.2.15,
but it was Rails.application.assets returns nil
quiet_assets.rb:4:in': undefined method logger=' for nil:NilClass (NoMethodError)
the issued line was
Rails.application.assets.logger = Logger.new('logger.log')
I back to Rails console, and find Rails.application.assets just returned nil.
I fix this issue by this step:
include two gem in your Gemfile in case you don't have it.
gem 'sprockets'
gem 'sprockets-rails'
find the file cause the issue , and initial your assets object.
you can also put that in application.rb , in my case I put it in config/initializers/quiet_assets.rb , before I refer to logger.
add this line:
Rails.application.assets = Sprockets::Environment.new
before this issued line:
Rails.application.assets.logger = Logger.new('logger.log')
in application.rb , remember have activate assets pipeline .
config.assets.enabled = true
for production you may need to set config.assets.compile = true
hope this helps somehow
Build it yourself (which is needed for new versions)
env = Sprockets::Railtie.build_environment(Rails.application)

Resources