S3 Obfuscated const_missing_from_s3_library error - ruby

I'm using the typical Mac/Ruby 1.9.3p125
irb>
require 'aws/s3'
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
Service.buckets
(Same error with Bucket.find or almost anything!)
Gives me:
NameError: uninitialized constant Service
from ~/.rvm/gems/ruby-1.9.3-p125/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206
:in `const_missing_from_s3_library'
from (irb):23
from ~/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'
I'm following documentation almost to spec but I'm so confused as to why this is happening?

You need to either include AWS::S3 in your class or do AWS::S3.Service.
Here's some code samples:
require 'aws/s3'
class MyClass
include AWS::S3
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
Service.buckets
end
or
require 'aws/s3'
class MyClass
AWS::S3::Base.establish_connection!(:access_key_id => 'AccessKey',:secret_access_key => 'SecretKey' )
AWS::S3::Service.buckets
end

Related

Where to put auth information

So I'm making a small program in VisualRuby that can tweet. So here's what my main.rb looks like:
#!/usr/bin/ruby
require 'vrlib'
require 'twitter_oauth'
#make program output in real time so errors visible in VR.
STDOUT.sync = true
STDERR.sync = true
#everything in these directories will be included
my_path = File.expand_path(File.dirname(__FILE__))
require_all Dir.glob(my_path + "/bin/**/*.rb")
LoginWindow.new.show
and my LoginWindow.rb looks like this
require 'twitter_oauth'
class LoginWindow #(change name)
include GladeGUI
client = TwitterOAuth::Client.new(
:consumer_key => '****',
:consumer_secret => '****',
:token => '****-****',
:secret => '****'
)
def show()
load_glade(__FILE__) #loads file, glade/MyClass.glade into #builder
set_glade_all() #populates glade controls with insance variables (i.e. Myclass.label1)
show_window()
end
def button1__clicked(*argv)
if client.authorized?
puts "true"
end
end
end
And finally my window looks like this:
Now when I run this and click the login button, VR spits this out
C:/Users/*/visualruby/Test/bin/LoginWindow.rb:22:in `button1__clicked': undefined local variable or method `client' for #<LoginWindow:0x3f56aa8>
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `block (3 levels) in parse_signals'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `main'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `show_window'
from C:/Users/*/visualruby/Test/bin/LoginWindow.rb:17:in `show'
from main.rb:14:in `<main>'
I don't think I'm supposed to put the client = Twitter.... stuff inside the LoginWindow class but I can't think of anywhere else to put it. Any ideas on how to fix this issue?
This is a quick solution for what you need.
in your LoginWindow.rb
def initialize
#client = TwitterOAuth::Client.new(
:consumer_key => '****',
:consumer_secret => '****',
:token => '****-****',
:secret => '****'
)
end
def button1__clicked(*argv)
if #client.authorized?
puts "true"
end
end
The problem with this solution is now you can't call button1_clicked without initializing LogginWindow before, so be careful.

"NameError: uninitialized constant UserAgent" when using Watir

I'm new to ruby, I seem to have successfully installed watir-webdriver and webdriver-user-agent gems, but when I was trying to follow the instructions here I've stumbled. How to proceed?
>> require 'watir-webdriver'
=> true
>> require 'webdriver-user-agent'
=> true
>> driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
NameError: uninitialized constant UserAgent
from (irb):3
from /usr/bin/irb:12:in `<main>'
Try using Webdriver::UserAgent

How to use RSpec expectations in irb

I'd want to use [1,2,3].should include(1) in irb. I tried:
~$ irb
1.9.3p362 :001 > require 'rspec/expectations'
=> true
1.9.3p362 :002 > include RSpec::Matchers
=> Object
1.9.3p362 :003 > [1,2,3].should include(1)
TypeError: wrong argument type Fixnum (expected Module)
from (irb):3:in `include'
from (irb):3
from /home/andrey/.rvm/rubies/ruby-1.9.3-p362/bin/irb:16:in `<main>'
But it doesn't work though it's a valid case. How can I use [1,2,3].should include(1)?
You are close, but calling include on top-level you will be calling Module#include. To get around it you need to remove the original include method so that RSpec's include gets called instead.
First let's figure out where the system include comes from:
> method :include
=> #<Method: main.include>
Ok. It looks like it's defined in main. This is the Ruby top-level object. So let's rename and remove the original include:
> class << self; alias_method :inc, :include; remove_method :include; end
Now we can get down to business:
> require 'rspec'
> inc RSpec::Matchers
> [1,2,3].should include(1)
=> true

Shoulda superclass mismatch on unit tests

Trying to write a simple unit test using shoulda and rails 3.
test/unit/user_test.rb
class UserTest < Test::Unit::TestCase
should validate_presence_of(:password, :on => :create)
should validate_presence_of(:handle, :email)
should validate_confirmation_of(:password)
should validate_length_of(:handle, :within => 6..15)
should validate_uniqueness_of(:handle)
should validate_format_of(:handle, :with => /\A\w+\z/i)
should validate_length_of(:email, :within => 6..100)
end
Relevant parts of Gemfile
group :test do
gem 'shoulda'
gem 'rspec-rails', '2.0.0.beta.12'
end
When I try to run this using rake test --trace I receive the following error:
** Execute test:units
/Users/removed/removed/removed/app_name/test/unit/user_test.rb:5: superclass mismatch for class UserTest (TypeError)
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:227:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9:in `each'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:9
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5:in `each'
from /Library/Ruby/Gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader.rb:5
I understand the error, I just don't get where another UserTest class would be defined that's giving me this issue. Any thoughts?
Mike
Check the output of find . | xargs grep -l UserTest against accidental duplicated uses of the class name.
The only way I could imagine avoiding this mistake is by doing the following:
UserTest = Class.new(Test::Unit::TestCase)
class UserTest # Or class UserTest < Test::Unit::TestCase is also allowed
should validate_presence_of(:password, :on => :create)
should validate_presence_of(:handle, :email)
should validate_confirmation_of(:password)
should validate_length_of(:handle, :within => 6..15)
should validate_uniqueness_of(:handle)
should validate_format_of(:handle, :with => /\A\w+\z/i)
should validate_length_of(:email, :within => 6..100)
end
if you were to repeat
UserTest = Class.new(Test::Unit::TestCase) # repeated
you'd get
warning: already initialized constant UserTest
But this approach would look a little weird.

Why am I getting "Test is not a class" in this Ruby script?

I've got a problem with this class
require "test/unit"
require "selenium/client"
class Test < Test::Unit::TestCase
def setup
#verification_errors = []
#selenium = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*chrome",
:url => "http://change-this-to-the-site-you-are-testing/",
:timeout_in_second => 60
#selenium.start_new_browser_session
end
def teardown
#selenium.close_current_browser_session
assert_equal [], #verification_errors
end
def test_test
#selenium.open "/apj/gestionnaire/flux.ex"
#selenium.wait_for_pop_up "_self", "30000"
end
end
it says to me that it's not a class :
/test.rb:4: Test is not a class (TypeError)
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from C:/Documents and Settings/Micro/My Documents/Aptana RadRails Workspace/.metadata/.plugins/org.rubypeople.rdt.testunit/ruby/RemoteTestRunner.rb:301
anyone have any idea ?
Regards
Bussiere
Using Test as your class name is a bad idea. It's an existing constant (referring to a module) as soon you require test/unit
require "test/unit"
Test.class # => Module
Use a different name for your test case.
Using Test as your class name is a bad idea.
Wrong ! Today a new version of the rspec-rails gem has been released fixing this issue in some cases.
You can take a look at the the changelog file:
Fix "Test is not a class (TypeError)" error when using a custom Test class in Rails 4.1 and 4.2. (Aaron Kromer, #1295)

Resources