ruby unzip - fails with uninitialized constant ZIP::File - ruby-on-rails

I have the following ruby code :
require 'HTTPClient'
require 'rubygems'
require 'zip'
def self.unzip(data, dest_dir)
::Zip::File.open_buffer(data) do |fzip|
fzip.each do |entry|
path = File.join(dest_dir, entry.name)
puts "here"
FileUtils::mkdir_p(File.dirname(path))
fzip.extract(entry, path) unless File.exist?(path)
#fzip.close
end
end
end
def self.fetch_from_url(url, dest_dir)
response = HTTPClient.get(url, follows_redirect: true)
if response.status == 200
unzip(response.body, dest_dir)
else
raise 'Could not fetch files from 3scale'
end
end
url = 'link/artifactory/zip-release-local/djin/3Sroxy/1.0.5/configuration.zip'
fetch_from_url(url, "/Users/something/")
When I run this in Mac ruby 2.0.0p481 it works fine and unzips the folder, but when I run the same in centOS 6.6 in ruby 1.8.7 it fails with following :
[root#ip-10-201-90-206 ~]# sudo ruby test/ex.rb
test/ex.rb:7:in `unzip': uninitialized constant Zip::File (NameError)
from test/ex.rb:20:in `fetch_from_url'
from test/ex.rb:28
also, I've done :
gem install zip
Successfully installed zip-2.0.2
1 gem installed
Installing ri documentation for zip-2.0.2...
Installing RDoc documentation for zip-2.0.2...

As Prashant4224 stated, you need to install
gem install rubyzip
You seem to use the zip Gem...

In Gemfile you need to add. Everything else didn't work for me.
gem 'rubyzip', '>= 1.0.0'
gem 'zip-zip'

Related

Bundler 2 is not installing inside docker

I'm trying to install bundler v2 in docker file and it installs successfully. when i run rubocop i get below error.
warn_for_outdated_bundler_version': You must use Bundler 2 or greater with this lockfile. (Bundler::LockfileError)
which still seem to think that I am using bundler older version, how can I fix this error
Dockerfile is
FROM ruby:2.3.4-alpine
WORKDIR "/var/www/app"
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler && bundle install
bin/bundle file is
require "rubygems"
m = Module.new do
module_function
def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end
def env_var_version
ENV["BUNDLER_VERSION"]
end
def gemfile
gemfile = ENV["BUNDLE_GEMFILE"]
return gemfile if gemfile && !gemfile.empty?
File.expand_path("../../Gemfile", __FILE__)
end
def bundler_version
#bundler_version ||= begin
env_var_version || cli_arg_version ||
lockfile_version || "#{Gem::Requirement.default}.a"
end
end
def load_bundler!
ENV["BUNDLE_GEMFILE"] ||= gemfile
# must dup string for RG < 1.8 compatibility
activate_bundler(bundler_version.dup)
end
def activate_bundler(bundler_version)
if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
bundler_version = "< 2"
end
gem_error = activation_error_handling do
gem "bundler", bundler_version
end
return if gem_error.nil?
require_error = activation_error_handling do
require "bundler/version"
end
return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
exit 42
end
def activation_error_handling
yield
nil
rescue StandardError, LoadError => e
e
end
end
m.load_bundler!
if m.invoked_as_script?
load Gem.bin_path("bundler", "bundle")
end
and the bundler version inside Gemfile.lock is 2.0.2
set BUNDLER_VERSION environment variable inside Dockerfile:
ENV BUNDLER_VERSION=2.0.2
or if you have docker.env:
BUNDLER_VERSION=2.0.2
I faced this problem also but as well as defining the BUNDLER_VERSION environment variable, I also needed to install that version into my container.
My Dockerfile therefore would look like:
FROM ruby:2.3.4-alpine
WORKDIR "/var/www/app"
ENV BUNDLER_VERSION=2.0.2
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN gem install bundler -v "$BUNDLER_VERSION" && bundle install

Cucumber and Capybara keep saying that I have an undefined method called `visit'

I keep getting the error message undefined method 'visit' (NoMethodError) when I try to run an example given to me from Instant Cucumber BDD How-to by Wayne Ye. I have lots of gems installed, and I think I have run all the necessary commands on my project folder.
My setup notes for what I have installed are:
gem install selenium-cucumber
gem install bundler
gem install cucumber-rails
gem install launchy
gem install database_cleaner
gem install factory_girl_rails
gem install webmock
gem install pickle
gem install spork-rails
gem install nokogiri
gem install ffi
gem install capybara
chdir C:\DevKit
ruby dk.rb init
ruby dk.rb install
gem install json
gem install sqlite3
<project folder>cucumber --init
<project folder>bundler init
<project folder>bundler install
The example I am trying to run is from the book and looks like this.
Amazon.features
Feature: Shopping in Amazon
As an internet user
I want to search stuff on Amazon
So that I can choose and buy items I like
#javascript
Scenario: Search for baseball gloves
Given I am on Amazon homepage
When I enter "baseball glove" in the search box
And I click "Go" button
Then I should see a list of results related with Baseball Gloves
step_definitions/amazon_search_steps.rb
Given /^I am on Amazon homepage$/ do
visit "http://www.amazon.com"
end
When /^I enter "(.*?)" in the search box$/ do |keywords|
fill_in "Search", :with => keywords
end
When /^I click "Go" button$/ do
click_button "Go"
end
Then /^I should see a list of results related with Baseball Gloves/ do
page.should have_content("#centerBelow")
end
When I run this, I get the following error message.
Looking online, the solution appears to be to add the following line to my spec_helper.rb file, but I don't know which one, and none of the ones I have tried have worked.
config.include Capybara::DSL
EDIT: As requested my env.rb file.
Full path is C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\cucumber-rails-1.4.3\features\support\env.rb
$:.unshift(File.dirname(__FILE__) + '/../../lib')
require 'rubygems'
require 'bundler/setup'
require 'rspec/expectations'
require 'aruba/cucumber'
if(ENV['ARUBA_REPORT_DIR'])
# Override reporting behaviour so we don't document all files, only the ones
# that have been created after #aruba_report_start (a Time object). This is
# given a value after the Rails app is generated (see cucumber_rails_steps.rb)
module Aruba
module Reporting
def children(dir)
children = Dir["#{dir}/*"].sort
# include
children = children.select do |child|
File.directory?(child) ||
(#aruba_report_start && File.stat(child).mtime > #aruba_report_start)
end
# exclude
children = children.reject do |child|
child =~ /Gemfile/ ||
child =~ /\.log$/
end
children
end
end
end
end

no such file to load -- RMagick.rb Rails, Ubuntu

I have ruby and my gems installed in my home directory using rvm. 'require RMagick' works from the console and irb, but not from within my Rails app. I have the gem listed in my Gemfile.
require 'RMagick'
class PetsController < ApplicationController
def image
image = Image.new(image_path("base/Base.png"))
#image = Pet.composite(0, '4a4a4a')
#response.headers["Content-type"] = image.mime_type
render :text => image.to_blob
end
end
If I remove the require from my controller, I get
uninitialized constant PetsController::Image
I also have tried require 'RMagick' in boot.rb, and by referring to the Image object using Magick::Image (which gives me uninitialized constant PetsController::Magick.
What am I doing wrong?
Solved it! Restarting the server fixed it. I guess I hadn't restarted the server when I added rmagick to my gemfile.
Is it possible that you are running the rails app with another user than the one that has the RMagick gem installed?

'no such file to load -- net/ssh' from rails Controller on Ubuntu

I have a very simple controller:
require 'net/ssh'
class MyController < ApplicationController
def foo
render :text => 'bar'
end
end
But when I request http://server:3000/my/foo I get:
MissingSourceFile in MyController#foo
no such file to load -- net/ssh
The gem is installed
> gem list net-ssh
*** LOCAL GEMS ***
net-ssh (2.0.11)
Also, I tried require 'net/ssh' in IRB, and it works.
MyController works fine on Windows, but fail on Ubuntu.
What can be wrong?
In a project I am working on we have used the config/environment.rb file to hold the gem require stuff. So
Rails::Initializer.run do |config|
# ...
config.gem 'net-ssh'
config.gem 'daemons'
config.gem 'slave'
config.gem 'vpim'
config.gem 'json'
# ...
end
I think you will require 'net-ssh' rather than 'net/ssh'. However we did run into a problem where have a hyphen in the name of the gem led to failures. Then we had to do
config.gem 'Ruby-IRC', :lib => 'IRC'
so that version maybe required for you. So that would be
config.gem 'net-ssh', :lib => 'net/ssh'
in case of rails 3.0
this solution if OK.
add this in the yourapp/Gemfile,
gem 'net-ssh
This may help:
Rails Gem Dependencies and Plugin Errors
This is also worth watching:
Railscasts: Gem Dependencies
In my case, since it's a stand alone ruby app, I only needed to require rubygems.
You can also use Dr Nic's ''gemsonrails'' and load vendored gems as plugins, check:
http://gemsonrails.rubyforge.org
I think, the original problem was that I used normal user instead of root:
$ gem install net-ssh
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /home/alex/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
So, I guess, rails could not find this gem.

Why is this line breaking Rails with Passenger on DreamHost?

Ok, so I have a Rails app set up on DreamHost and I had it working a while ago and now it's broken. I don't know a lot about deployment environments or anything like that so please forgive my ignorance. Anyway, it looks like the app is crashing at this line in config/environment.rb:
require File.join(File.dirname(__FILE__), 'boot')
config/boot.rb is pretty much normal, but I'll include it here anyway.
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end
def booted?
defined? Rails::Initializer
end
def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end
def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end
def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end
def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end
class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end
class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end
class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end
def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end
class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end
def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end
def load_rubygems
require 'rubygems'
min_version = '1.1.1'
unless rubygems_version >= min_version
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end
rescue LoadError
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end
def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end
private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end
# All that for this:
Rails.boot!
Does anyone have any ideas? I am not getting any errors in the log or on the page.
-fREW
I had the same problem on DreamHost. Freezing rails and unpacking all gems got me past it.
rake rails:freeze:gems
rake gems:unpack:dependencies
My guess would be that you're breaking because of a newer version of the Rails gems on Dreamhost. At least, that's been my issue when things have blown up in something like boot.rb.
Try freezing the gems from your development environment into your vendor/rails directory.
Ya - the problem is not really in boot.rb - it's just that boot.rb is where rails is actually loaded.
So you'll get an error like this if you've specified a version of Rails that just doesn't exist on your dreamhost slice. This can happen if you either upgrade your project, start a new project (and forget that you upgraded rails in the meanwhile) or if you're still using an old version of rails and it's now been removed from the dreamhost server that you're on.
To figure out which is is, look in config/environment.rb for the line that'll read something like:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
Then ssh to your dreamhost server and type gem list and see if your version is in the list.
If not, you an try several options. Lets say the version you're using is 2.3.4
To begin with, try: gem install rails -v=2.3.4 then restart. That may be all that is required.
If that doesn't work, then try freezing and unpacking the gems (as per the other answer here).
There is also another possibility - that you're actually missing a gem that rails depends on but which is failing silently - eg a dependency on a certain version of rack caught me out once. But you may also have other gem dependencies
If you run rake gems you will be able to list all the gems that your project knows about that it needs - make sure they're installed to begin with.
Then, as a sort of rough smoke-test, try running script/console - if you're missing an important rails gem, script/console won't load and should fail, giving you a notice about the gem you need.
Update:
If you're trying to run v 2.3.5, you may also be suffering from this problem:
Bypassing rack version error using Rails 2.3.5
In which case you'll need to follow the instructions there.

Resources