SMS Fu Ruby gem throwing very strange YAML error - ruby

I'm trying to use the Ruby SMS Fu gem to send automated SMS messages to someone from my email address. Also, I'm not using Rails; this is a simple Ruby script that's getting some information from a site somewhere and then sending messages to someone based on said information. Unfortunately, I'm running into a very strange error when I try to use SMS Fu in my script:
/Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:377:in `parse': (<unknown>): found character that cannot start any token while scanning for the next token at line 7 column 12 (Psych::SyntaxError)
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:377:in `parse_stream'
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:325:in `parse'
from /Users/my_username/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/psych.rb:252:in `load'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:55:in `config_yaml'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:61:in `carriers'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:77:in `carrier'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:73:in `carrier_email'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:85:in `sms_address'
from /Users/my_username/.rvm/gems/ruby-2.4.0/gems/sms_fu-1.1.2/lib/sms_fu/sms_fu.rb:43:in `deliver'
from ./my_script.rb:69:in `<main>'
What I've got going in my script looks like this:
#!/usr/bin/env ruby
require "active_support" # action_mailer, one of sms_fu's dependencies, throws
# an error without this
require "sms_fu"
# gather information from our site...
# Pony delivery via SMTP
PONY_CONFIG = {
:via => :smtp,
:via_options => {
:address => "smtp.gmail.com",
:port => "587",
:user_name => "me#example.com",
:password => "pass",
:authentication => :plain,
:enable_starttls_auto => true,
:domain => "localhost.localdomain"
}}
sms_fu = SMSFu::Client.configure(:delivery => :pony, :pony_config => PONY_CONFIG)
sms_fu.deliver("123456789", "some carrier", "my message", from: "me#example") # this line triggers the error
The odd thing is that this error is complaining about parsing YAML, but according to SMS Fu's README file, I only need to worry about setting up a YAML file if I'm using Rails, which I'm not. However, it seems that, upon examining SMS Fu's sources, it is hard-coded to work with a YAML file in the SMSFu.config_yaml method, which is subsequently used indirectly or directly by other methods in SMS Fu, all the way back to SMSFu::Client#deliver, which is the one causing the error here.
So, I guess my question is: how can I get this error to stop happening so that I can just send my SMS message without all the YAML hassle?
EDIT:
Forgot to give some version / environment info: I'm running this script on OS X 10.12.2 and I'm using Ruby 2.4.0 (although this Ruby version is manifest in the error above).

Related

Error when send mail with gem pony on Ruby

On initialization .rb i take this error
/home/fucc/.rvm/gems/ruby-2.5.1/gems/pony-1.11/lib/pony.rb:250:in `block in build_mail': undefined method `body' for Pony:Module (NoMethodError)
Cod when i send mail
def place
# Actually order
Pony.mail(:to => StoreApplication::Admin.email,
:from => "My store <mail#gmail.com>",
:via => :smtp,
:via_options => {
adress: 'smtp.gmail.com',
port: '587',
user_name: 'mail#gmail.com',
password: '###########',
authentication: :plain,
domain: "mail.google.com" },
subject: "New order", body: "Check your admin page")
end
Ruby 2.5.1, Pony 1.12.
I do:
- update all gem's;
- install again gem Pony;
- down and up pony version
The answer is in the error message that you posted:
/home/fucc/.rvm/gems/ruby-2.5.1/gems/pony-1.11/lib/pony.rb:250:in `block in build_mail': undefined method `body' for Pony:Module (NoMethodError)
That means that in the gem's source code, in the file lib/pony.rb on line 250 there is an attempt to call the method Pony.body, but the module Pony does not have a method body so an exception is being raised.
In cases like this, it helps to look at the source in the git repository for the gem. The first thing to notice is that the latest version of that gem is 1.12 and you are using 1.11. So what changed between 1.11 and 1.12, and could it fix this issue?
Looking at the history for lib/pony.rb I see there was an commit in November 2017 named 'Fix NoMethodError when pony is used with mail 2.7.0'. This change modifies lib/pony.rb line 250 and changes the method call for body. This is the exact line of code and method call that raised the error you described.
The solution is to update your copy of pony, for example with gem update pony or if you are using bundler then bundle update. And if the gem is defined in a Gemfile then make sure there is no version restriction on it.
I noticed you said you're using 1.12 and that you've tried rolling the version forward and backward, but that's not accurate. You're using 1.11 and you can tell because the path in your error message is gems/pony-1.11. So however it is your application is configured, it is configured to use 1.11. If you still can't get your app to use 1.12 then please post a comment explaining how you are managing gems for your app.
Finally, I recommend that you reformat your code to make it a little more readable:
def place
Pony.mail(
to: StoreApplication::Admin.email,
from: 'My store <mail#gmail.com>',
via: :smtp,
via_options: {
address: 'smtp.gmail.com',
port: '587',
user_name: 'mail#gmail.com',
password: '###########',
authentication: :plain,
domain: 'mail.google.com'
},
subject: 'New order',
body: 'Check your admin page'
)
end
Changes made include:
Change double quotes to single quotes (single quotes except when using string interpolation)
Fix indentation (two spaces)
Fix hash keys (don't use :key => value, use key: value)
Fix spelling on adress to address
Remove extra spaces
Properly align blocks and closures
The Ruby Style Guide can help with making sense of the recommended changes.

Credentials issues using the rt-client rubygem (rest interface to request tracker ticket system)

I am having issues using the rt-client gem(link), as it keeps returning a "RT/4.0.8 401 Credentials Required". The REST interface for this site is working, as I have some perl scripts that are currently working with it in a similar fashion.
test.rb
#!/usr/bin/env ruby
require 'rt/client'
rt = RT_Client.new
id = rt.create( :Queue => "General",
:Subject => "Test",
:Requestor => "test#example.org",
:Text => "Ignore me"
)
.rtclientrc
server=http://example.org/
user=exampleuser
pass=examplepass
cookies=tmp
Versions
Gem Version: rt-client-0.5.0
RT Version: 4.0.8
Ruby Version: 1.9.3p327
Output
Payload for new ticket:
------xYzZY492386xYzZY
Content-Disposition: form-data; name="content";
Queue: General
Subject: Test
Requestor: test#example.org
Text: Ignore me
id: ticket/new
------xYzZY492386xYzZY--
"RT/4.0.8 401 Credentials required\n"
I am seeing the error when I do a "puts id.inspect" at the bottom of test.rb, as the ticket is not getting created.
Is this perhaps an issue with the handling of cookies? I was trying to avoid writing a custom solution in Net::HTTP if possible, but I will go that route if this continues to be a hassle.
Author of the rt-client ruby gem here.
This was resolved some time ago and I know this is old, but it is ranked highly in a Google search for the gem. If anyone finding this question still has issues with rt-client, the gem is now on github.com. If you wish, please clone it, make your fix and send me a pull request.
https://github.com/uidzip/rt-client

Chef : Create a process as another user

So I have some code for running a batch file as a specific user. This was my attempt to automate the following syntax
runas /user:thisguy "C:\ThisGuysScript.bat"
so it looks like this in Ruby
Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
So I try to put this in a recipe in chef and disaster strikes
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
Is failing with the following error
[Tue, 30 Oct 2012 15:57:03 +0000] FATAL: ArgumentError: You must supply a name when declaring a user resource
So it seems to not realise that I want to use the win32 flavour process. Chef seems to override the win32 module (I know recipes are the opscode DSL rather than really ruby right?)
Anyone been able to get this working? Or the same function with a different implementation. Checked out the windows cookbook but didn't spot much
It sounds like you want to make an LWRP for creating a process on a windows machine.
The error you are getting means you have something like
user do # Missing name
gid 500
home "..."
end
the correct syntax is
user "apache" do # or whatever the user name should be
# ...
end
If you don't have the above in your cookbook, it is possible that the included file has a variable named user which would also cause this issue.
To answer your subquestion, Chef is straight ruby with some functions made available and a frame work to run things. Note, there are several stages in a chef run. I think you are having issues in the compilation stage.
Making an LWRP seems like the way to go. If you don't want to go that far you could do something like.
ruby_block "Firing process lazers" do
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
end

Getting Pony or Mail gems to work with Sinatra / Heroku

I'm a Ruby newbie so apologies if this question is simple. And many thanks in advance for your help.
We have a Sinatra application that's been deployed onto Heroku. We're trying to add a page that sends a simple email. I've added the SendGrid add-on to the Heroku app. Now, I'm just trying to add the Ruby code that creates and sends the message using the SendGrid SMTP server information.
The problem I'm having is that even though I installed the Mail gem on the Heroku app (by adding to the Gemfile), I get an error when the Heroku app is launched complaining that 'treetop/runtime' is not installed:
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `require': no such file to load -- treetop/runtime (LoadError)
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:75:in `rescue in block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:69:in `block in <module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `each'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:68:in `<module:Mail>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /app/.bundle/gems/ruby/1.9.1/gems/mail-2.4.4/lib/mail.rb:2:in `<top (required)>'
←[32m2012-04-03T16:37:49+00:00 app[web.1]:←[0m from /usr/ruby1.9.2/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundler/runtime.rb:64:in `require'
Similarly, when I try to install the Pony gem, it complains that it can't load Mail. Oddly, this is all works on my local system, so I think it's a problem with Heroku. I just can't get Heroku to fully load everything it needs from either Pony or Mail to launch successfully. (Note: I can't even get Heroku to launch, so I can't even test the actual sending of the email code.)
Any specific help/insight would be greatly appreciated. Has anyone encountered this with Heroku? Are there other gems that might work for this simple purpose?
Thanks!
P.S. The bundle successfully installed mail (2.4.4) and pony (1.4).
It would be helpful to see code snippets for your primary routing ruby script. But for my sinatra app, I configured Mail configs as.
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.sendgrid.net',
:port => 587,
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => 'plain',
:enable_starttls_auto => true
}
end
And in my sending the contact email
mail = Mail.deliver do
to <youremail>
from <someone>
subject 'Feedback for my Sintra app'
text_part do
body <your feedback>
end
end
Hope this helps.
In case you want some settings for Pony instead of Mail (they're very similar):
configure :production do # I have a different hash for development settings.
email_options, {
:from => "noreply#midas.heroku.com",
:via => :smtp,
:via_options => {
:address => 'smtp.sendgrid.net',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
},
}
end
Pony.options = settings.email_options
Make sure you've set the sendgrid username and password via the config settings e.g.
heroku config:add SENDGRID_USERNAME=GIVEN_USERNAME SENDGRID_PASSWORD=GIVEN_PASSWORD
To send is very straightforward, just call Pony.mail and pass it a hash with 'to' and 'body' etc. (I'm sure you know this but I'm just trying to be thorough!)
I also use v1.4 but haven't had the same problem, but if it's saying it can't load Mail when you've switched to Pony I'd check for a rogue require statement.

Sending mails automatically through action mailer Rails 3.1

I have to send weekly emails to all the user about the latest things happening. I am using ActionMailer to accomplish other mailing task however I have no clue how to automate the weekly emails.
Update
I found whenever gem which could be used to schedule cron jobs. I guess this could be used to send weekly emails which I intend to. Still looking how to make it work with ActionMailer will update once I find the solution
Update 2
This is what I have done so far using whenever gem:-
in schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in users.rb
def self.weekly_update
#user = User.all
#user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end
If i try to run User.weekly_update from the console I am able to get the mails. I am testing in development mode and using rvm. I checked my crontab file and it has got the right stuff.
However I am not getting any mails automatically from the app. Any clue what might be wrong?
Thanks,
OK so it turns out to be a path issue with whenever gem, and the problem was created when I installed another version of ruby.
In my machine the new ruby version is installed in /usr/local/bin/ruby. In my rails app I had to go to the file script/rails and replace #!/usr/bin/env ruby with #!/usr/local/bin/ruby.
I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory
I made a cron.log file to log the cron error this is what I did in my schedule.rb code written in the question :-
every 2.minutes do
runner "User.weekly_update", :environment => 'development', :output => 'log/cron.log'
end
I am getting periodic mails now.
It seems like you haven't configured ActionMailer settings.
First check out the logs from console, whether the mailing process is working(paste your logs).
If yes then do following steps.
add this in your gemfile.
gem 'tlsmail'
run
bundle install
write these configuration setting in your config/environments/development.rb file
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:enable_starttls_auto => true,
:authentication => :login,
:user_name => "<address>#gmail.com",
:password => "<password>"
}
config.action_mailer.raise_delivery_errors = true
add your working password/email against user_name and password.
Don't forget to restart server.

Resources