How to search message in mailbox with net/imap in Ruby? - ruby

I have some script on Ruby 1.9.3:
require "net/imap"
imap = Net::IMAP.new(mail_imap_server)
imap.login(mail_login, mail_password)
imap.select("INBOX")
puts imap.search(["FROM", "homer#simpson.com"])
imap.logout
imap.disconnect
If the desired message is present, then all is well.
If the desired message is missing, an error:
/opt/local/lib/ruby1.9/1.9.1/net/imap.rb:1332:in `block in search_internal': undefined method `[]' for nil:NilClass (NoMethodError)
from /opt/local/lib/ruby1.9/1.9.1/monitor.rb:211:in `mon_synchronize'
from /opt/local/lib/ruby1.9/1.9.1/net/imap.rb:1326:in `search_internal'
from /opt/local/lib/ruby1.9/1.9.1/net/imap.rb:752:in `search'
from ./mail.rb:12:in `mail'
from ./mail.rb:26:in `<main>'
How can I solve this problem?

first check if there are messages in the result, use a rescue just in case
require "net/imap"
imap = Net::IMAP.new(mail_imap_server)
imap.login(mail_login, mail_password)
imap.select("INBOX")
begin
messages = imap.search(["FROM", "homer#simpson.com"])
puts messages if messages.length > 0
rescue
puts "Error while retrieving the message"
end
imap.logout
imap.disconnect

Related

Hanami: undefined method `size' for nil:NilClass

I keep learning hanami and ran into the following problem: when I initialize and check the parameters, I encounter the following error
Boot Error
Something went wrong while loading /Users/anewaccount/Projects/Mediateka/config.ru
NoMethodError: undefined method `size' for nil:NilClass
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/deprecated.rb:11:in `input_processor'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:165:in `default_options'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:35:in `new'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-validations-1.3.7/lib/hanami/validations.rb:109:in `validations'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/params.rb:152:in `params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `block in params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `class_eval'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `params'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:11:in `<class:SignUp>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:9:in `<module:Auth>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:8:in `<module:Controllers>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:7:in `<module:Web>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:6:in `<top (required)>'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require_relative'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `block in require!'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `each'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `for_each_file_in'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require!'
my action:
# frozen_string_literal: true
require 'pry-byebug'
require_relative 'auth_base_action'
module Web
module Controllers
module Auth
class SignUp < AuthBaseAction
params do
required(:user).schema do
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).filled(:str?, format?: /#/)
required(:password).filled(:str?).confirmation
end
end
def call params
binding.pry
end
end
end
end
end
I wrote the params block according to the example from the official documentation on the idea that it should work, but I catch NoMethodError: undefined method `size 'for nil: NilClass I also saw in the official documentation that these validations are delegated from Hanami :: Validations, I installed gem' hanami- validation 'according to their official documentation, but I had the same error, then I started looking at other people's code and they were successful using the example from the official documentation. Please tell me what is the error or please explain what am I doing wrong? I'm already very confused

undefined method 'execute' for nil:NilClass

I am making a tool in ruby which can interact with databases.
I am using amalgalite as an adapter for sqlite3.
Code:
require 'amalgalite'
# this is class RQuery
class RQuery
def db_open(db_name)
#db = Amalgalite::Database.new "#{db_name}.db"
make_class
end
def exec_this(query)
#db.execute(query)
end
def make_class
tables_list = exec_this("select name from sqlite_master where type='table'")
tables_list.each do |table|
#class_created = Object.const_set(table[0].capitalize, Class.new)
#class_created.class_eval do
define_singleton_method :first do
RQuery.new.exec_this("select * from #{table[0]} order by #{table[0]}.id ASC limit 1")
end
end
end
end
def eval_this(input)
instance_eval(input)
end
def code
print '>>'
input = gets
exit if input =~ /^q$/
puts eval_this(input)
code
end
end
Now when I am running the code everything works fine until I call table_name.first
It gives output
vbhv#fsociety ~/git/R-Query/bin $ ruby main.rb
Enter the code or q for quit
>>db_open('vbhv')
users
persons
people
programmers
>>Users.first
/home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval': undefined method `execute' for nil:NilClass (NoMethodError)
Did you mean? exec
from /home/vbhv/git/R-Query/lib/r-query.rb:29:in `block (3 levels) in make_class'
from (eval):1:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:43:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from main.rb:4:in `<main>'
Now the 'execute' function it is talking about is inside amalgalite. What am I doing wrong here?? Thanks in Advance!
The problem in this was that the new class formed dynamically doesn't know about the connection variable '#db'. Hence the code solves the problem.
#class_created.instance_variable_set(:#database, #db)
A big thanks to Jagdeep Singh.

Celluloid 0.17.3 giving unexpected "undefined method" error

I have started using Celluloid gem this morning for that first time. I am following this Railscasts tutorial and trying to figure things out.
I have a class called "SomeClass" and it has only one method. Here is the code:
require 'celluloid'
class SomeClass
include Celluloid
def initialize(name)
#name = name
end
def assholify()
puts "#{#name} has become an ASSHOLE."
end
end
When I create new instances of the class and call its method (with a bang i.e. "assholify!"), I am getting the undefined method 'assholify!', error. But Celluloid is supposed to trigger the method asynchronously when it is called with a bang. So here is how I am calling the method:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.assholify!
end
Here is the full backtrace of the error:
I, [2016-09-09T11:28:02.488618 #3682] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
/home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:42:in `rescue in check': undefined method `assholify!' for #<SomeClass:0x10897dc> (NoMethodError)
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:39:in `check'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:26:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
from (celluloid):0:in `remote procedure call'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
from some_class.rb:18:in `block in <main>'
from some_class.rb:16:in `each'
from some_class.rb:16:in `<main>'
Why am I getting this error? Is it the right way to call the function? Also how do I get rid of Celluloid 0.17.3 is running in BACKPORTED mode. warning?
The undefined method error occurred because actor methods are not called with a bang in the recent versions of celluloid gem. Instead you call the method like this: n.async.assholify. So here is what the code should look like:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.async.assholify # Instead of "n.assholify!"
end
For "Celluloid 0.17.0 is running in BACKPORTED mode" warning, take a look at this wiki. Backported Mode is the default, for a limited time. If you use require 'celluloid/current' instead of require 'celluloid', you should not see this warning.

ERROR -- : undefined method `each' for nil:NilClass (NoMethodError)

I am getting the above error when I try to run /update_project_permissions.rb script of Rally-User-Management.
update_project_permissions.rb
#include for rally json library gem
require 'rally_api'
require 'csv'
require './lib/multi_io.rb'
require './lib/rally_user_helper.rb'
require './lib/go_update_project_permissions.rb'
$project_identifier_arg = ARGV[0]
$new_permission_arg = ARGV[1]
if $project_identifier_arg.nil? || $new_permission_arg.nil? then
puts "Usage: ruby update_all_project_permissions.rb \"My Project\" \"Editor\""
puts "or: ruby update_all_project_permissions 12345678910 \"No Access\""
puts "Where in number form, the project identifier is the Project's ObjectID."
end
begin
go_update_project_permissions($project_identifier_arg, $new_permission_arg)
end
which calls file
lib/go_update_project_permissions.rb at
go_update_project_permissions
go_update_project_permissions
The command line argument is - ruby update_project_permissions.rb "40880544785" "No Access"
I am getting the following error
ERROR -- : undefined method `each' for nil:NilClass (NoMethodError)
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:92:in `is_workspace_admin'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:137:in `update_project_permissions'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:309:in `block in go_update_project_permissions'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `each'
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `go_update_project_permissions'
update_project_permissions.rb:38:in
[2015-08-10T15:38:28.854098 #15504] ERROR -- :
["c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:92:in `is_workspace_admin'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:137:in `update_project_permissions'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:309:in `block in go_update_project_permissions'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `each'", "
c:/Users/vorad/Documents/Rally-User-Management/lib/go_update_project_permissions.rb:308:in `go_update_project_permissions'", "update_project_permissions.rb:38:in "]E,
[2015-08-10T15:38:28.857098 #15504] ERROR -- : undefined method `each' for nil:NilClass
Thanks for finding this bug. There's an updated version here:
https://github.com/markwilliams970/Rally-User-Management
That should address the problem.

Ruby NameError undefined local variable or method `e

class TwitterProfile < ActiveRecord::Base
def send_status_update(status_update)
if publish?
client = Twitter::Client.new( :oauth_token => authentication.token,
:oauth_token_secret => authentication.secret)
client.update(status_update.to_twitter_string)
end
rescue Exception => e
logger.info "Error publishing to twitter: #{e.to_s}"
end
end
There is a StatusUpdate model and an observer that reposts them to Twitter in after_create. I sometimes get the following exception:
NameError (undefined local variable or method `e' for #<TwitterProfile:0x00000004e44ab8>):
app/models/twitter_profile.rb:23:in `rescue in send_status_update'
app/models/twitter_profile.rb:18:in `send_status_update'
app/models/status_update_observer.rb:6:in `block in after_create'
app/models/status_update_observer.rb:4:in `after_create'
app/models/workout_observer.rb:5:in `after_update'
app/controllers/frames_controller.rb:76:in `update'
app/controllers/application_controller.rb:24:in `call'
app/controllers/application_controller.rb:24:in `block (2 levels) in <class:ApplicationController>'
app/controllers/application_controller.rb:10:in `block in <class:ApplicationController>'
What am I missing here?
I have one thing I know and one that's just a wild guess.
The thing I know is that you don't need to call to_s on an overall #{} expression; that will happen automatically. But it does no harm.
My wild guess is that your test case is not really running the code you have posted. What happens if you change e to f?
I should note that rescuing Exception itself is usually a bad idea. You should rescue RuntimeError or StandardError at the highest, and preferably something more specific. You can get fairly strange errors when rescuing Exception because you interfere with threads and interpreter-level events.
You're missing the 'begin' block of the begin/rescue clause.

Resources