How to make this rails method - windows compatible? - windows

I've grabbed the latest code base of a gem called socialstream but I'm noticing an issue with file uploads on windows machines. The issue seems to be with the code below...
# Monkey patches to Ruby on Rails
#
# Use unix file util to prove the content type sent by the browser
class ActionDispatch::Http::UploadedFile
def initialize_with_magic(*args, &block)
initialize_without_magic(*args, &block)
if (unix_file = `which file`.chomp).present? && File.exists?(unix_file)
`#{ unix_file } -v 2>&1` =~ /^file-(.*)$/
version = $1
#content_type =
if version >= "4.24"
`#{ unix_file } -b --mime-type #{ #tempfile.path }`.chomp
else
`#{ unix_file } -bi #{ #tempfile.path }`.chomp =~ /(\w*\/[\w+-\.]*)/
$1
end
end
end
alias_method_chain :initialize, :magic
end
the error is as follows
NoMethodError (undefined method chomp' for nil:NilClass):
social_stream-base (0.22.0) lib/rails/social_stream.rb:8:ininitialize_with_magic'
actionpack (3.2.5) lib/action_dispatch/http/upload.rb:39:in new'
actionpack (3.2.5) lib/action_dispatch/http/upload.rb:39:innormalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in block in normalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:ineach'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in normalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:innormalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in block in normalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:ineach'
actionpack (3.2.5) lib/action_dispatch/http/parameters.rb:73:in normalize_parameters'
actionpack (3.2.5) lib/action_dispatch/http/upload.rb:41:innormalize_parameters'
Is there a way to make this work on windows, either by using a gem, something i can install on windows, or rewriting the file?

Either delete or comment out the line:
alias_method_chain :initialize, :magic
This will stop it from hooking into ActionDispatch::HTTP::UploadFile#initialize
This also means you won't get its benefit of "proving" the content-type, but I don't know if that is really necessary for your application.

Related

odd number list for Hash (SyntaxError), though I am using Ruby 1.8.7 hash declaration

I am trying to connect to Oracle database using 'activerecord' and Ruby 1.8.7 and getting below error on my Windows 7 machine. I searched around for this issue and came across 1.8 and 1.9 ruby declaration of 'Hash' however I am using Ruby 1.8.7 and I feel I am using correct hash declaration, please correct me if I am wrong.
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `gem_original_require': C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhancedadapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:917: odd number list for Hash (SyntaxError)
read_committed: "READ COMMITTED",
^
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:917: syntax error, unexpected ':', expecting '}'
read_committed: "READ COMMITTED",
^
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:917: syntax error, unexpected ',', expecting kEND
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:919: syntax error, unexpected '}', expecting kEND
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:1471: dynamic constant assignment
DBMS_OUTPUT_BUFFER_SIZE = 10000 # can be 1-1000000
^
C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced-adapter-1.5.3/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:1555: syntax error, unexpected $end, expecting kEND
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:156:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:521:in `new_constants_in'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:156:in `require'
from C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `establish_connection'
from oracle_conn_testing.rb:5
This is how my code looks like
require 'rubygems'
gem "activerecord-oracle_enhanced-adapter"
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "oracle_enhanced",
:database => "someurl.net:1523/ABCAD",
:username => "testing1",
:password => "testing1")
class TestTable < ActiveRecord::Base
set_table_name "TABLE_NAME"
set_primary_key "ID"
end
TestTable.find(:all).each do |tt|
p tt
end
nac = TestTable.new()
Additional Information:
I have below gems on my machine :
* LOCAL GEMS *
actionmailer (2.3.4)
actionpack (2.3.4)
activerecord (2.3.4)
activerecord-oracle_enhanced-adapter (1.5.3)
activeresource (2.3.4)
activesupport (2.3.4)
json (1.8.1)
mysql (2.9.1 x86-mingw32)
rack (1.0.1)
rails (2.3.4)
rake (10.1.1)
ruby-oci8 (2.1.7 x86-mingw32)
rubygems-update (1.4.2)
I use below Rubygems version:
C:\Users\tester1>gem -v
1.4.2
I tried to connect using OCI8 and I was able to get response out of Oracle database so there is no connectivity issue from my machine.
irb(main):006:0> OCI8.new('testing1', 'testing1', 'someurl.net:1523/ABCAD').exec('select sysdate from dual') do |r| puts r.join(', ') end
Mon Mar 10 15:09:23 -0400 2014
=> 1
I used below link as my reference:
http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/rubyhol/instructions/rubyrails.htm
The error message shows that your error is coming from inside the activerecord-oracle_enhancedadapter gem, and that gem has a Ruby 1.9 style hash. You might find an older version of that gem that supports Ruby 1.8 and switch to that version.

Error while upgrading from Rails 3.1 to Rails 3.2

Upgrade steps were performed as mentioned in http://guides.rubyonrails.org/3_2_release_notes.html
Right now I'm on Ruby 1.8.7 and Rails 3.1.x (using system ruby). I installed RVM with ruby 1.8.7 and added to rails 3.2 and then I get the following error:
[app]$ rails console
Faraday: you may want to install system_timer for reliable timeouts
$HOME/src/qbol/tapp/config/environment.rb:16:in `add': undefined method `>' for nil:NilClass (NoMethodError)
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:55:in `add'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/tagged_logging.rb:61:in `info'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:86
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/lazy_load_hooks.rb:25:in `on_load'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activerecord-3.2.17/lib/active_record/railtie.rb:80
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `instance_exec'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:30:in `run'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:55:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `each'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/initializable.rb:54:in `run_initializers'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:136:in `initialize!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `send'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/railtie/configurable.rb:30:in `method_missing'
from $HOME/src/app/config/environment.rb:48
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:236:in `load_dependency'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/application.rb:103:in `require_environment!'
from $HOME/.rvm/gems/ruby-1.8.7-p374/gems/railties-3.2.17/lib/rails/commands.rb:40
from script/rails:6:in `require'
from script/rails:6
This is on my development box. Any idea why this is happening?
EDIT: Below is the environment.rb. The error is happening on the last line App::Application.initialize!
# Load the rails application
require File.expand_path('../application', __FILE__)
module ActiveSupport
class BufferedLogger
def self.current_user
Thread.current[:user]
end
def self.current_user=(user)
Thread.current[:user] = user
end
def add(severity, message = nil, progname = nil, &block)
return if #level > severity
message = (message || (block && block.call) || progname).to_s
level = {
0 => "DEBUG",
1 => "INFO ",
2 => "WARN ",
3 => "ERROR",
4 => "FATAL"
}[severity] || "UNKNOWN"
user=BufferedLogger.current_user
if(!user.nil?)
idstr = "uid:#{user.id}"
if !user.current_app_user.nil?
idstr.concat(", acid: #{user.current_app_user.account_id}")
end
else
idstr=""
end
message = "[%s: %s #{idstr}] %s" %
["#{level} pid: #{$$}", Time.now.strftime("%y-%m-%d %H:%M:%S"), message]
message = "#{message}\n" unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
end
# Initialize the rails application
App::Application.initialize!
The error is happening inside ActiveSupport::BufferedLogger#add method call and you appear to be monkey-patching this class. The internals of ActiveSupport::BufferedLogger have likely changing between the versions of Rails you're using.
Try removing all of the ActiveSupport::BufferedLogger code from your environment.rb to work past this error and get your app running. Then, if you still need the monkeypatch (don't know why), you'd have to rewrite it on top of the newer version of the class.

Load Error : no such file to load -- watir/testcase after upgrading to watir 4.0.2

I just upgraded to watir 4.0.2 from watir 2.0.4 and i am getting the below error,
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:60:in `gem_original
_require': no such file to load -- watir/testcase (LoadError)
I searched about the error and was not able to find a solution to it.
Gem List
abstract (1.0.0)
actionmailer (3.2.8, 3.0.0)
actionpack (3.2.8, 3.0.0)
activemodel (3.2.8, 3.0.0)
activerecord (3.2.8, 3.0.0)
activeresource (3.2.8, 3.0.0)
activesupport (3.2.8, 3.0.0)
addressable (2.3.2)
akami (1.2.0)
arel (3.0.2, 1.0.1)
Ascii85 (1.0.1)
builder (3.0.0, 2.1.2)
bundler (1.0.22)
childprocess (0.3.6)
commandline (0.7.10)
commonwatir (4.0.0, 2.0.4)
erubis (2.7.0, 2.6.6)
ffi (1.1.5)
gyoku (0.4.6)
hike (1.2.1)
hoe (3.0.8)
httpi (0.9.7)
i18n (0.6.1, 0.4.2)
journey (1.0.4)
libwebsocket (0.1.5)
mail (2.4.4, 2.2.19)
mime-types (1.19, 1.18)
mini_magick (3.2.1)
multi_json (1.3.6)
mysql2 (0.3.11 x86-mingw32, 0.2.18 x86-mingw32, 0.2.6 x86-mingw32)
nokogiri (1.5.5 x86-mingw32)
nori (1.1.3)
pdf-reader (1.1.1)
polyglot (0.3.3)
rack (1.4.1, 1.2.5)
rack-cache (1.2)
rack-mount (0.6.14)
rack-ssl (1.3.2)
rack-test (0.6.2, 0.5.7)
rails (3.0.0)
railties (3.0.0)
rake (0.8.7)
rautomation (0.7.3, 0.6.3)
ruby-rc4 (0.1.5)
rubygems-update (1.8.24)
rubyzip (0.9.9)
s4t-utils (1.0.4)
savon (0.9.9)
selenium-webdriver (2.26.0)
sprockets (2.1.3)
sqlite3 (1.3.6 x86-mingw32)
sqlite3-ruby (1.3.3)
subexec (0.0.4)
text-format (1.0.0)
text-hyphen (1.0.2)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
tzinfo (0.3.33)
user-choices (1.1.6.1)
wasabi (2.1.0)
watir (4.0.2 x86-mingw32, 2.0.4)
watir-classic (3.3.0)
watir-webdriver (0.6.2)
websocket (1.0.6)
win32-api (1.4.8 x86-mingw32)
win32-process (0.6.6)
win32screenshot (1.0.7)
windows-api (0.4.2)
windows-pr (1.2.2)
xml-simple (1.1.1)
Ruby Version:ruby 1.8.7 (2010-08-16 patchlevel 302) [i386-mingw32]
Any idea on this?
watir/testcase was removed from Watir recently. I can not find when it was removed, but the information is in CHANGES file or in Git history.
While it would probably be better to move away from Watir::TestCase, if you really need or want to use it, you can re-create it by copying the files from 3.0.
Assume you are using Ruby 1.8, which still has the test-unit gem installed by default, you can do the following:
1) Create a file named 'watir_testcase.rb' with the following code, which is basically a copy of the testcase.rb and assertions.rb file from 3.0:
require 'watir-classic'
require 'test/unit'
require 'test/unit/assertions'
module Watir
# Verification methods used by Watir::TestCase
module Assertions
include Test::Unit::Assertions
# Log a failure if the boolean is true. The message is the failure message logged.
# Whether true or false, the assertion count is incremented.
def verify boolean, message = 'verify failed.'
add_assertion
add_failure message.to_s, caller unless boolean
end
def verify_equal expected, actual, message=nil
full_message = build_message(message, <<EOT, expected, actual)
<?> expected but was
<?>.
EOT
verify(expected == actual, full_message)
end
def verify_match pattern, string, message=nil
pattern = case(pattern)
when String
Regexp.new(Regexp.escape(pattern))
else
pattern
end
full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
verify(string =~ pattern, full_message)
end
end
end
module Test::Unit::Assertions
def assert_false(boolean, message=nil)
_wrap_assertion do
assert_block("assert should not be called with a block.") { !block_given? }
assert_block(build_message(message, "<?> is not false.", boolean)) { !boolean }
end
end
end
module Watir
# This is a 'test/unit' testcase customized to exeucte test methods sequentially by default
# and extra assertions
#
class TestCase < Test::Unit::TestCase
include Watir::Assertions
##order = :sequentially
def initialize name
throw :invalid_test if name == :default_test && self.class == Watir::TestCase
super
end
class << self
attr_accessor :test_methods, :order
def test_methods
#test_methods ||= []
end
def order
#order || ##order
end
def default_order= order
##order = order
end
def sorted_test_methods
case order
when :alphabetically then test_methods.sort
when :sequentially then test_methods
when :reversed_sequentially then test_methods.reverse
when :reversed_alphabetically then test_methods.sort.reverse
else raise ArgumentError, "Execute option not supported: #{#order}"
end
end
def suite
suite = Test::Unit::TestSuite.new(name)
sorted_test_methods.each do |test|
catch :invalid_test do
suite << new(test)
end
end
if (suite.empty?)
catch :invalid_test do
suite << new(:default_test)
end
end
return suite
end
def method_added id
name = id.id2name
test_methods << name if name =~ /^test./
end
def execute order
#order = order
end
end
public :add_assertion
end
end
2) In your test case files, change the:
require 'watir/testcase'
to require the newly created file:
require './watir_testcase'
(ensuring that the path is correct for wherever you saved the file)

Rspec2 issue with Rcov

In my Rakefile, I have a task defined like this:
namespace :test do
desc "Run all specs."
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*_spec.rb'
t.verbose = false
end
RSpec::Core::RakeTask.new(:coverage) do |t|
t.rcov = true
t.rcov_opts = %q[--exclude "spec"]
t.verbose = true
end
end
When running test:coverage, I get this:
./spec/foo_spec.rb:3: undefined method `describe' for main:Object (NoMethodError)
from /Library/Ruby/Gems/1.8/gems/rcov-0.9.9/bin/rcov:516:in `load'
from /Library/Ruby/Gems/1.8/gems/rcov-0.9.9/bin/rcov:516
from /usr/bin/rcov:19:in `load'
from /usr/bin/rcov:19
rake aborted!
ruby -S rcov -Ispec:lib --exclude "spec" "./spec/foo_spec.rb" failed
Below my gem list:
diff-lcs (1.1.2)
rake (0.8.7)
rcov (0.9.9)
rspec (2.3.0)
rspec-core (2.3.1)
rspec-expectations (2.3.0)
rspec-mocks (2.3.0)
Any idea? Thanks in advance.
The solution, from David Chelimsky:
http://rubyforge.org/pipermail/rspec-users/2010-December/019077.html
require "rspec"
Cheers.

Vpim::Vcard - Parsing vcard 2.1

My code must parse Vcard 2.1 format.
I am using vpim(there are no other libs)
When I run Vpim::Vcard.decode(data) I get error:
undefined method `each' for #<String:0x0000000928e778>
StackTrace:
NoMethodError (undefined method `each' for #<String:0x0000000928e778>):
vpim (0.695) lib/vpim/rfc2425.rb:82:in `unfold'
vpim (0.695) lib/vpim/rfc2425.rb:308:in `decode'
vpim (0.695) lib/vpim/vcard.rb:692:in `decode'
app/models/event.rb:71:in `block (2 levels) in parse_data'
I tryed to run gem install vcard and to require 'vcard' directry(config.gem does not include this file after vpim gem) after RailsApp initialization.(config.gem 'vpim' is included in environment.rb)
By this way I get another error:
;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=
(Exception class is Vpim::InvalidEncodingError)
Vcard code I try parse:
BEGIN:VCARD
VERSION:2.1
REV:20090710T151929Z
TEL;CELL:80954130722
X-CLASS:private
END:VCARD
It is interesting that second error is when I am decoding inside a Rails model.
When I try to decode directly from script/console (after installinv vcard gem and direct including 'vcard') I succesfuly get Vcard object.
StackTrace:
Vpim::InvalidEncodingError (;=D0=9D=D0=B0=D0=B4=D1=80=D0=B0=20=D0=B1=D0=B0=D0=BD=D0=BA=20=D0=BE=D0=BB=):
vcard (0.1.1) lib/vcard/field.rb:106:in `decode0'
vcard (0.1.1) lib/vcard/field.rb:172:in `initialize'
vcard (0.1.1) lib/vcard/field.rb:183:in `new'
vcard (0.1.1) lib/vcard/field.rb:183:in `decode'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `block in decode'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `collect'
vcard (0.1.1) lib/vcard/rfc2425.rb:308:in `decode'
vcard (0.1.1) lib/vcard/vcard.rb:686:in `decode'
app/models/event.rb:71:in `block (2 levels) in parse_data'
app/models/event.rb:71 :
vcard = Vpim::Vcard.decode(contact.text)
Here is axample from irb:
95-25-164-74:~ smix$ irb
ruby-1.9.2-rc2 > str = <<EOS
ruby-1.9.2-rc2"> BEGIN:VCARD
ruby-1.9.2-rc2"> VERSION:2.1
ruby-1.9.2-rc2"> REV:20090710T151929Z
ruby-1.9.2-rc2"> TEL;CELL:80954130722
ruby-1.9.2-rc2"> X-CLASS:private
ruby-1.9.2-rc2"> END:VCARD
ruby-1.9.2-rc2"> EOS
=> "BEGIN:VCARD\nVERSION:2.1\nREV:20090710T151929Z\nTEL;CELL:80954130722\nX-CLASS:private\nEND:VCARD\n"
ruby-1.9.2-rc2 > require 'vpim'
=> true
ruby-1.9.2-rc2 > Vpim::Vcard.decode str
NoMethodError: undefined method `each' for #<String:0x000001010e0428>
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:82:in `unfold'
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/rfc2425.rb:308:in `decode'
from /Users/smix/.rvm/gems/ruby-1.9.2-rc2/gems/vpim-0.695/lib/vpim/vcard.rb:692:in `decode'
from (irb):10
from /Users/smix/.rvm/rubies/ruby-1.9.2-rc2/bin/irb:17:in `<main>'
ruby-1.9.2-rc2 >
How is it possible to parse Vcard 2.1 in rails?
The problem is that String#each has been removed in Ruby 1.9.1. (Its functionality endures in String#each_line). To fix/work around this bug, do any of the following:
Run your rails app on Ruby 1.8.7.
You can edit the Vpim code to use String#each_line appropriately (and send the vpim authors a patch)
You can monkey-patch the String class by defining the each method
class String
alias_method :each, :each_line
end
Switch to the vcard gem, which is taken from Vpim and which has Ruby 1.9.1 support.
I recommend the 4th option.

Resources