Ruby Soundcloud Gem: Cannot Test Uploaded File - ruby-on-rails

Haven't seen much on testing for the soundcloud gem. Most everything has been splendid - however I found that uploading files to SoundCloud through my specs is a bit tricky.
Here's the problem:
spec:
file = "#{Rails.root}/spec/factories/files/sample.mp3"
title = "From the test suite"
track = #client.post('/tracks', track: {title: title, asset_data: File.new(file)})
Whether or not I use
File.new
or
File.expand_path
the error is the same: NoMethodError:
undefined method `map' for #String:0x007fa1ce8425c0
The successful code in the controller is:
file = params["soundcloud_file"]
title = params["title"]
track = client.post('/tracks', track: {
title: title
asset_data: File.new(file.tempfile, 'rb')
})
Now I understand that params["soundcloud_file"] is an ActionDispatch::Http::UploadedFile, so that's what I've been attempting to work with for the past hour or so with no luck.
I'm open to any way to successfully test an upload.

The problem was with the 'webmock' gem. Even using the required version for VCR (<1.10.0) it throws:
NoMethodError: undefined method `map' for <#String: ... >
Replacing with the 'fakeweb' gem corrects the error.

Related

Unable to use Mongoid::GridFs put to upload files

I'm just trying to use the mongoid-grid_fs gem in order to upload images and files to MongoDB.
The controller code in question currently looks like this:
grid_fs = Mongoid::GridFs
encFile = File.open(attFile)
File.open(encFile, 'wb') do |f|
f.write(encData)
end
grid_file = grid_fs.put(encFile.path)
I'm getting this error:
undefined method `destroy' for nil:NilClass
/GEMS/bundler/gems/mongoid-grid_fs-2f0e12e85a24/lib/mongoid/grid_fs.rb:150:in `put'
(eval):2:in `put'
/x/app/controllers/runtime/attachments_api_controller.rb:126:in `upload'
FYI line 126 in attachments_api_controller is the grid_fs.put line.
I'm not sure what the issue is.
I have also declared require 'mongoid/grid_fs'
I have put bindings around the issue, I can confirm that encFile.path is correct and it contains valid data.
EDIT 1: I've tried different combinations of trying to put the file like
grid_fs.put(encFile) - grid_fs.put(File.open(encFile.path)) - grid_fs.put(File.read(encFile.path)) but all of these still return the same error.

Not able to load the default credentials for the google-cloud-text_to_speech Gem in Rails 5.1

In my Rails 5.1.7 application, I want to use a method within a rake-task to turn text into a mp3 audiofile. I'm using the google-cloud-text_to_speech gem for doing so. Here's part of my setup:
/Gemfile:
gem 'google-cloud-text_to_speech'
Here is the method in the task-file that is being called from within the task:
/lib/tasks/sync.rake:
require "google/cloud/text_to_speech"
def create_speech(content, id)
text_to_speech_client = Google::Cloud::TextToSpeech.new
text = content
input = { text: text }
language_code = "nl-NL"
voice = { language_code: language_code }
audio_encoding = :MP3
audio_config = { audio_encoding: audio_encoding }
response = text_to_speech_client.synthesize_speech(input, voice, audio_config)
File.write("speach_id_#{id}.mp3", response.audio_content, mode: "wb")
puts "Created speach_id_#{id}.mp3"
end
I authenticated it according to the following instructions:
https://googleapis.dev/ruby/google-cloud-text_to_speech/latest/file.AUTHENTICATION.html
/config/application.rb:
config.speech = config_for(:speech)
/config/speech.yml:
google:
project: ENV=["TEXTTOSPEECH_PROJECT"]
credentials: ENV=["TEXTTOSPEECH_CREDENTIALS"]
I'm using Figaro for my environment variables and therefore I have an application.yml file.
/config/application.yml:
development:
TEXTTOSPEECH_PROJECT: "project-id"
TEXTTOSPEECH_CREDENTIALS: "/path/to/file.json"
And then I run my task:
$ bundle exec rake sync:feeds
When I run the rake task without the extra method, everything works fine. But when I use the method, I'm getting:
"rake aborted!
Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials"
I've tried many different ways to offer the credentials, but I'm getting the same error time after time. Is there anyone who can help me out?

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

Rails access a file stored on S3 (`method_missing': undefined method `[]')

I am trying to instantiate a file stored on s3 with rails to parse it. The first step is to access the file. I have built a quick script to get familiar with amazon aws-sdk gem.
require 'aws-sdk'
s3 = Aws::S3::Resource.new(
access_key_id: 'XXXX',
secret_access_key: 'XXXX',
region: 'us-east-1')
bucket = s3.bucket('my_bucket')
bucket.objects.each do |obj|
puts obj.key
end
This code will give me the list of all the objects (files) on my drive. It looks like
uploads/doc1.pdf
uploads/doc2.pdf
Now I want to understand how I can retrieve one single object. I tried
object = bucket.objects['doc2.pdf']
puts object.key
But I get the error method_missing': undefined method[]' for Aws::Resources::Collection:0x0055e82a514ab0> (NoMethodError)
I also tried
object = bucket.objects.find(1)
puts object.name
But I get the error undefined method `name' for Enumerator:0x0055855a098de8> (NoMethodError)
Could anyone give me the right syntax to retrieve one single object (file)? Once I get it, I intend to use the "open" method to instantiate the file and then a pdf parser. Thanks!
This has worked for me
object = bucket.object('path to file')

Unknown Constants AssetsController::S3

I'm attempting to download files from my S3 File server via a rails app that I've written. However, I'm having a difficult time trying to figure out how to accomplish this. I've been attempting to use this reference from Amazon's blog to get it working.
in the get method in my controller, I have the following:
asset = current_user.assets.find_by_id(params[:id])
File.open('filename', 'wb') do |file|
reap = s3.get_object({ bucket:'bucket-name', key: URI.encode(asset.uploaded_file.url)}, target: file)
end
however I'm getting the following error:
uninitialized constant AssetsController::s3
I'm using the gem aws-sdk. Any suggestions would be much appreciated.
uninitialized constant AssetsController::s3
You need to define s3, the below should work
asset = current_user.assets.find_by_id(params[:id])
File.open('filename', 'wb') do |file|
s3 = Aws::S3::Client.new
reap = s3.get_object({ bucket:'bucket-name', key: URI.encode(asset.uploaded_file.url)}, target: file)
end

Resources