I am working on a project with Sitespec, which is a static site generator using RSpec and uses any Rack app. I chose Padrino as a Rack app for Sitespec.
Then, I get the following errors when I do rspec at some URLs of Content-type: image/png type.
Failures:
1) Sitespec GET public/images/simple-image1.png Generate a static image public/images/simple-image1.png
Failure/Error: pathname.write(response.body)
Encoding::UndefinedConversionError:
"\x89" from ASCII-8BIT to UTF-8
Pathname.write(response.body) is going to try to convert it to UTF-8.
But when I use Sinatra instead of Padrino, the error does not occur.
I made simple sample projects for comparison:
Using Padrino https://github.com/Nyoho/sitespec-padrino-sample2
Using Sinatra https://github.com/Nyoho/simple-sitespec-sinatra-test
Question: Why does the Padrino project fail and how do I fix the errors?
By the way, doing monkey patch
module Sitespec
class Artifact
def generate_file
pathname.binwrite(response.body)
end
end
end
eliminates the error. (Replacing Pathname#write to Pathname#binwrite.)
Related
I want to upload a stringIO as a file to a server.
I'm not using rails to make the request, just ruby and some gems.
I have created a stringIO like below:
require 'rest-client'
require 'zip'
... some code
stream = Zip::OutputStream::write_buffer do |zip|
... code that download files and packs them
end
stream.rewind
RestClient.post('somepath', { file: stream.read }, headers)
RestClient is just some handy gem that makes it a bit easier to send http request.
so my problem is that on the server side in the controller when I receive the request I get an error
ArgumentError - invalid byte sequence in UTF-8:
activesupport (4.2.6) lib/active_support/core_ext/object/blank.rb:117:in `blank?'
carrierwave (0.10.0) lib/carrierwave/sanitized_file.rb:127:in `is_path?'
carrierwave (0.10.0) lib/carrierwave/sanitized_file.rb:93:in `size'
carrierwave (0.10.0) lib/carrierwave/sanitized_file.rb:136:in `empty?'
carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:119:in `cache!'
carrierwave (0.10.0) lib/carrierwave/mount.rb:329:in `cache'
carrierwave (0.10.0) lib/carrierwave/mount.rb:163:in `export='
carrierwave (0.10.0) lib/carrierwave/orm/activerecord.rb:39:in `export='
app/controllers/exports_controller.rb:17:in `upload'
(I'm using carrierwave to store the files)
I think that I'm not handling the stringIO properly but honestly don't exactly know how should I pass it to the post request so that it behaves like a file
any ideas?
turns out this was happening because I was not saving the zip as a file but used write_buffer that responds with stringIO.
stringIO read method returns a string that was uploaded to my controller and then I tried to save the file using carrierwave roughly like that:
... code in my controller
myObject = ObjectModel.find(params[:id])
myObject.fileUploader = StringIO.new(params[:file]) => this was the string i received from stringIO read method
myObject.save
but it won't work because StringIO objects don't have original_filename method anymore since Rails3 (As far as I understood from the carrierwave issue page)
The solutions was to create a wrapper around StringIO and add either an attribute or a method that is called original_filename afterwards file was being saved correctly.
I'm posting link to carrierwave wiki that deals with this:
https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Upload-from-a-string-in-Rails-3-or-later
and a sample wrapper like object
class StringIOWrapper < StringIO
attr_accessor :original_filename
end
afterwards this will work
myObject.fileUploader = StringIOWrapper.new(params[:file])
myObject.save
I'm trying to pack my REST app into into an executable with OCRA. I have a few gems required in my script:
require 'rack'
require 'rack/server'
require 'grape'
require 'grape-entity'
require 'rubygems'
I skip starting the server with this:
if not defined?(Ocra)
Rack::Server.start options
end
When I try to run my server.exe:
Temp/../server.rb:221:in `default_middleware_by_environment':
cannot load such file -- rack/content_length (LoadError)
Which means that it doesn't detect submodules of rack that exist, but aren't used and therefore not included. If I add a require 'rack/content_length' it continues with cannot load such file -- rack/chunkedEven` and so on.
When I interrupted my server by hand before I also had to call a few api endpoints to have everything included.
I think my options are either:
Tell OCRA to include all the submodules of rack and grape, but compiling that list is a bit time consuming and would increase the file size
I already tried ocra server.rb --gem-full=rack --gem-full=grape, which get my server started, but when calling the API 'rack/mount/strexp' is missing again..
Calling the API within my script, but I couldn't figure out how to do that. I can't add a block to Rack::Server.start options and it does only continue when I interrupt the server.
Any ideas to implement either option, or is there another solution?
If we run the rack app with a rack handler (webrick / thin / else), we can shutdown the server in another thread so that ocra can finish packing (not sure how to do same thing with Rack::Server).
app = Rack::Directory.new ENV['HOME'] # a sample app
handler = Rack::Handler.pick %w/ thin webrick /
handler.run app do |server|
# handler.run yields a server object,
# which we shutdown when ocra is packing
if ocra_is_packing # replace with proper condition
Thread.new { sleep 10; server.shutdown }
end
end
You may have to do something else (access the server etc.) to have ocra pack appropriate dependencies.
I am creating a Ruby command line application that will generate a Rack application. I want to test that the Rack application that is created is a valid Rack application. I have started using Aruba and Cucumber to test that the CLI creates the correct files and directory structure, but now I'm ready to run rackup and see that the application is running correctly. How can I get Cucumber and Capybara to interact with this newly created application?
Generate a root page for your rack application then have capybara visit whatever path you deem to be your root path and verify some sort of output on that page
def response
if request.path "/"
Rack::Response.new(render("index.html.erb"))
else
Rack::Response.new("Not Found", 404)
end
end
on rack startup: #startup_output = "foo"
generate some sort of output on your root page <%= #startup_output %>
capybara:
visit('/')
page.should have_content('foo')
If you need to have capybara start the app up try using a rake task inside the test:
system "rake rack:start"
I am trying to use ckeditor (4.0.6) with Using rails_admin (0.5.0) in Rails 4.0 on DigitalOcean server.
I have included it in the rails_admin.rb initializer as follows and it works in production mode on my local
config.model Faq do
field :display_order
field :question
field :answer, :ck_editor
end
However on DigitalOcean when I go into Rails_Admin and try to make a new FAQ object it won't load ckeditor because it can't find the js.
http://dummy.com/assets/ckeditor/ckeditor.js?_=1381313244552 404 (Not Found)
rails_admin-5daa9b7b76a226bdfa46a07fdaf2d77d.js:3
How can I fix this?
The problem is because Rails assets compile actually added a fingerprint on to the assets file of every CKeditor file, while the rails-admin is looking for a non fingerprint version of the file.
This issue only happens in the rails 4 with ckeditor. Actually the Readme.md of the ckeditor gem did mention about the issue and how to resolve it, but it isn't complete.
To resolve you could write a rake file to remove all the fingerprints and run this during deployment.
Here is my solution to resolve this issue.
Create a rake file in lib/tasks/ckeditor.rake with the following code
namespace :ckeditor do
desc 'Create nondigest versions of some ckeditor assets (e.g. moono skin png)'
task :create_nondigest_assets do
fingerprint = /\-[0-9a-f]{32}\./
for file in Dir[File.join('public/assets/ckeditor', '**', '*.js'),
File.join('public/assets/ckeditor', '**', '*.js.gz'),
File.join('public/assets/ckeditor', '**', '*.css'),
File.join('public/assets/ckeditor', '**', '*.png'),
File.join('public/assets/ckeditor', '**', '*.gif')]
next unless file =~ fingerprint
nondigest = file.sub fingerprint, '.' # contents-0d8ffa186a00f5063461bc0ba0d96087.css => contents.css
FileUtils.cp file, nondigest, verbose: true
end
end
end
For Capistrano user, make sure you include this in your deploy.rb
desc 'copy ckeditor nondigest assets'
task :copy_nondigest_assets, roles: :app do
run "cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} ckeditor:create_nondigest_assets"
end
after 'deploy:assets:precompile', 'copy_nondigest_assets'
For Heroku user, you would need to run the rake file manually each time before you check in your code. Make sure you do your rake assets:precompile before this.
rake ckeditor:create_nondigest_assets
Hope it helps
I don't know, have you precompiled your assets?
If you're switching from a different kind of host, like Heroku, you
may forget that you have to manually precompile your assets. You're
lucky, though – it's easy!
RAILS_ENV=production rake assets:precompile If you run into problems,
try running this instead:
RAILS_ENV=production rake assets:precompile:primary
From https://www.digitalocean.com/community/articles/how-to-launch-your-ruby-on-rails-app-with-the-digitalocean-one-click-image
I'm trying to send message to ZeroMQ from Rails 4 application.
I added gem "zmq" to my Gemfile, then I use this code in some method in my Application Controller.
context = ZMQ::Context.new(1)
Rails prints exception:
uninitialized constant ApplicationController::ZMQ
If I add require 'zmq'to application_controller.rb Rails prints other message:
cannot load such file -- zmq
I found the reason of this error. Gem zmq (rb-zmq) doesn't work correctly with ZeroMQ 3.x.x, only with 2.x.x:
https://github.com/zeromq/rbzmq/issues/25