Digest::MD5 in Ruby 1.9.3 - ruby

I'm running into something weird here.
I have an "authenticator" that relies on ND5 to hash a certain string we match as password. The problem when I run my tests is this:
NoMethodError: undefined method `md5' for #<CASServer::Authenticators::Billing:0x007fd8e6c906a0>
./models/authenticators/billing.rb:63:in `validate'
./routes/login.rb:166:in `block (2 levels) in <class:Server>'
./routes/login.rb:158:in `each'
./routes/login.rb:158:in `block in <class:Server>'
(eval):2:in `click_button'
./features/step_definitions/when_steps.rb:32:in `/^I enter "(.*)" as username and the generated username password and log in$/'
./features/rubycas.login.feature:14:in `When I enter "username" as username and the generated username password and log in'
So basically he does not recognize the MD5 as part of the Digest library.
This problem occurs when running the tests in the IDE, as well as in the IRB console:
1.9.3-p125 :001 > require "digest/md5" and Digest::MD5("test")
NoMethodError: undefined method `MD5' for Digest:Module
However, when I run the following:
[root#DCUDEV01 /home/morn/rubycas/current]# ruby
require "digest/md5" and Digest::MD5("test")
I receive no errors, dumps or exceptions. Ruby just accepts it.
What am I missing in order to get this MD5 stuff working?

Digest::MD5 is not a method but a module. Try
Digest::MD5.digest("test")

I found it confusing, it doesn't seem like .digest is correct. I can't say it is wrong either...
Commenter #reconbot got it right, IMO, but I saw the comment only after coming back to improve this QA, I think a comment isn't visible enough.
http://ruby-doc.org/stdlib-2.4.0/libdoc/digest/rdoc/Digest/MD5.html
Below is an example usage for md5 hasing for gibbon usage, the mailchimp gem.
md5_hashed_email_address = Digest::MD5.hexdigest("john.doe#example.com")

Related

Using Rspec should receive

I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end

`<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError) -

When trying to run my twitter_ebooks bot using ruby run.rb I get this error:
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:14:in `<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'
Here is the git repo, of the bot I'm using, if that helps: https://github.com/mispy/twitter_ebooks
Here is what is inside the bots.rb file (I have removed the auth tokens and consumer key etc for security): http://pastebin.com/gYqcMYhM
What does this error mean and how can I fix it? Thanks.
EDIT: I tried running it again after restarting got pretty much the same error except instead of my_ebooks/bots.rb:14 it says my_ebooks/bots.rb:13
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:13:in `<top (required)>': undefined local variable or method `“raf_ebooks”' for main:Object (NameError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'
You have curly quotation marks that Ruby doesn't pick up. This happens when you use word processors that try to get fancy.
TWITTER_USERNAME = “raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = “raf_ebooks" # This should be the name of the text model
Notice the broken syntax highlighting. They should be "normal" straight quotes:
TWITTER_USERNAME = "raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = "raf_ebooks" # This should be the name of the text model
Much better.
Get yourself a decent editor. TextEdit is not suitable for coding. TextMate and Sublime Text are popular commercial editors. GitHub's Atom is a free editor, pretty nice too.
The opening quote of the TWITTER_USERNAME value isn't a "regular" quote, it's some sort of smart quote. Hence Ruby isn't recognising it as a string, and is getting rather confused.

Ruby Mandrill API Returning an Error in Terminal

ran into a little stump just now, been doing some pair programming trying to debug it and seen scouring over google and yet found no results.
Alright, so I am building a simple mail sender app with the Mandrill API in Ruby. I believe I have my bash profile set up correctly with the API key however when I execute the .rb file through the terminal i get this message:
Taimurs-MacBook-Pro:mail_sender taimurknaziri$ ruby mandrill.rb
D, [2014-06-19T12:00:30.473457 #3662] DEBUG -- : Net::NTLM is not available. Install via gem install rubyntlm.
/Library/Ruby/Gems/2.0.0/gems/mandrill-0.0.4/lib/mandrill/api.rb:35:in `initialize': wrong number of arguments (0 for 1..2) (ArgumentError)
from mandrill.rb:3:in `new'
from mandrill.rb:3:in `<main>'
After trying to debug the problem with a professor I am getting this:
Taimurs-MacBook-Pro:mail_sender taimurknaziri$ ruby mandrill.rb
/Library/Ruby/Gems/2.0.0/gems/mandrill-api-1.0.51/lib/mandrill.rb:28:in `initialize': You must provide a Mandrill API key (Mandrill::Error)
from mandrill.rb:3:in `new'
from mandrill.rb:3:in `<main>'
If someone could point me in the right direction as to what this means, that would be great!
Your API key wouldn't go in your bash profile. Create another file in your ruby project say .env and in there you can store your environment variables such as your api key, like so:
ENV['MANDRILL_API_KEY'] = "PUT YOUR KEY HERE"
Then you set your API key in your mandrill.rb program equal to MANDRILL_API_KEY. As an example you could do:
m = Mandrill::API.new(MANDRILL_API_KEY)

Rspec tests not running because of initializer Yaml files

I'm integrating tests with Rspec into quite a large / developed app. When I run the test I'm getting very odd errors from some of my initializers.
For example, my carrierwave.rb loads a yaml file, when I run the test command I get:
carrierwave.rb:11:in `block in <top (required)>': undefined method `[]' for nil:NilClass
which is referring to the second line of code below:
fog_config = YAML::load_file(Rails.root.join 'config/fog.yml')[Rails.env.to_s]
config.fog_directory = fog_config['directory']
Momentarily if I remove that line of code in carrierwave, omniauth starts to complain too:
omniauth.rb:4:in `block in <top (required)>': undefined method `symbolize_keys' for nil:NilClass (NoMethodError)
Again, another yaml file:
fb_config = YAML::load_file(Rails.root.join 'config/fb_app_version.yml')[Rails.env].symbolize_keys
I'm guessing the solution is to change my yaml files or include something in the tests, but I'm not sure what. Any ideas would be most appreciated, thanks.
It looks like you need to include test entries in the Yaml files in question.
For example, your config/fog.yml probably looks something like this:
production:
directory: the_prod_directory
other_keys: ...
development:
directory: the_dev_directory
other_keys: ...
You need to add a test key:
test:
directory: the_test_dir
other_keys: appropriate values

Ruby NoMethodError in Redmine from script/runner

I have some Ruby code I need to execute in the Redmine Ruby app in order to enable a module in all projects at once.
Surprisingly, ruby does access the Projects, but raises a NoMethodError when accessing a few particular methods inside each of the "Project" objects.
Here is the code:
Project.find(:all).each do |project|
print "Enabling modules for project '#{project.identifier}' ... "
puts project.methods.sort # this does print "enabled_module_names"
puts project.enabled_module_names
end
This fails with:
hostname:/srv/apps/redmine# script/runner vendor/plugins/customplugin/lib/enable_modules.rb
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing': undefined method `enabled_module_names' for #<Project:0x7f28985c1cb0> (NoMethodError)
from vendor/plugins/customplugin/lib/enable_modules.rb:14
from vendor/plugins/customplugin/lib/enable_modules.rb:7:in `each'
from vendor/plugins/customplugin/lib/enable_modules.rb:7
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:46
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
I have scratched my head a lot, but I can't grasp why would the code find the "Project" symbol but not the methods within, especially as "project.methods" does indeed list "enabled_module_names".
Any help most welcome.
Are you sure that enable_module_names is an instance method?
Is it within Project.instance_methods?
Edit (summary of the comments below):
In earlier versions, you have to use following:
enabled_module_names = project.enabled_modules.collect(&:name)
The getter is present in later versions only (see rev.4460 for details of this change)

Resources