Ruby, HTTParty, SSL error - ruby-on-rails

I have just installed ruby and have been following Ruby tutorials. I started this tutorial: https://www.distilled.net/resources/web-scraping-with-ruby-and-nokogiri-for-beginners/
Where I got the SSL error so I used the guide at http://guides.rubygems.org/ssl-certificate-update/
My problem is though that my version is ruby 2.6.7 when I ask for the version but when I run this code:
require 'Nokogiri'
require 'HTTParty'
require 'JSON'
require 'Pry'
require 'csv'
page = HTTParty.get('https://google.com/')
Pry.start(binding)
I get this error
C:\Ruby22\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/Tom/RubymineProjects/projectone/projectone.rb
C:/Ruby22/lib/ruby/2.2.0/net/http.rb:923:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:923:in `block in connect'
from C:/Ruby22/lib/ruby/2.2.0/timeout.rb:73:in `timeout'
from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:923:in `connect'
from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:852:in `start'
from C:/Ruby22/lib/ruby/2.2.0/net/http.rb:1375:in `request'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/httparty-0.14.0/lib/httparty/request.rb:118:in `perform'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/httparty-0.14.0/lib/HTTParty.rb:560:in `perform_request'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/httparty-0.14.0/lib/HTTParty.rb:486:in `get'
from C:/Ruby22/lib/ruby/gems/2.2.0/gems/httparty-0.14.0/lib/HTTParty.rb:598:in `get'
from C:/Users/Tom/RubymineProjects/projectone/projectone.rb:7:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
It seems to be using 2.2.0 still, I tried to google how to uninstall a version but still unsure what to do? Any help?
I ran the script in Ruby Mine if that helps.
I am running Windows 10.

From: How do I make HTTParty ignore SSL?
In the latest HTTParty, you can use the verify option to disable SSL verification;
HTTParty.get( "#{ #settings.api_server }#{ url }", :verify => false).parsed_response"
Note the use of :verify => false

Related

SSLv3 read server certificate error while using Mechanize to log into to flicker.com

I am trying to execute following code in Windows 7 machine using Mechanize and Ruby.
require 'mechanize'
a = Mechanize.new { |agent|
# Flickr refreshes after login
agent.follow_meta_refresh = true
}
a.get('https://www.flickr.com/') do |home_page|
signin_page = a.click(home_page.link_with(:text => /Sign In/))
puts signin_page.uri
my_page = signin_page.form_with(:id => 'mbr-login-form') do |form|
username_field = form.field_with(:id => 'login-username')
username_field.value = 'some_username'
password_field = form.field_with(:id => 'login-passwd')
password_field.value = 'some_password'
end.submit
end
I get following error message,
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `connect': SSL_connect
SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `block in
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `connect'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:751:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:631:in `connection_for'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:259:in `fetch'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize.rb:440:in `get'
from first.rb:19:in `<main>'
I installed gem 'Certified' but still getting error. Any insight?
Looks like most of the companies are moving away from using SSLv3, I had to tell mechanize to explicitly use ssl_version='TLSv1' to solve the problem.
a = Mechanize.new {|a| a.ssl_version, a.verify_mode = 'TLSv1',OpenSSL::SSL::VERIFY_NONE}
Thanks for the insight provided by this post.
You're running into this issue due to the target site no longer supporting SSLv3. A little Googling found howto-manually-add-trust-cert-to-rubygems.md may hold your solution. I apologize in advance that I don't do Ruby on Windows so I can't replicate. In Linux the problem is usually solved by updating openssl as I answered in this post.

Ruby(HardWay_Zed_Shaw(ex12)) << RUNTIME ERROR open(ruby-lang.org)

I'm working through the Hard Way exercises and have encountered a runtime error when attempting to run the lesson code for ex12.
Any suggestions? Thanks in advance!
INSTRUCTIONS
http://ruby.learncodethehardway.org/book/ex12.html
MY INPUT
require 'open-uri'
open("http://www.ruby-lang.org/en") do |f|
f.each_line {|line| p line}
puts f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
puts f.content_type # "text/html"
puts f.charset # "iso-8859-1"
puts f.content_encoding # []
puts f.last_modified # Thu Dec 05 02:45:02 UTC 2002
end
OUTPUT =>
$ ruby ex12.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:174:in `open_loop': redirection forbidden: http://www.ruby-lang.org/en -> https://www.ruby-lang.org/en (RuntimeError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:518:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:30:in `open'
from ex12.rb:3
It's an SSL-Related Redirection Issue
The stack trace gives this error:
`open_loop': redirection forbidden: http://www.ruby-lang.org/en -> https://www.ruby-lang.org/en (RuntimeError)
For whatever reason, open-uri is detecting a loop when redirecting from an http scheme to https. All you need to do to fix it is use the correct scheme in your URI when calling the Kernel#open method. For example:
open('https://www.ruby-lang.org/en')
Once that change is made, the script works fine.
On the same exercice, after correcting http to https, i got this :
/Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:851:in `start'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:313:in `open_http'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:708:in `buffer_open'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:688:in `open'
from /Users/FDS/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/open-uri.rb:34:in `open'
from openuri.rb:2:in `<main>'

Getting AWS S3 to work with rails

So, I'm trying to get S3 working with Paperclip. I'm in the process of setting up Aws with rails. So far I've installed the 'aws/s3' gem and setup my access_keys on AWS.
I then added the keys to a s3.yml file:
# config/s3.yml
access_key_id: ******************
secret_access_key: ******************
Next I ran:
2.0.0p195 :015 > require 'aws/s3'
=> false
2.0.0p195 :016 > AWS::S3::Base.establish_connection!(YAML.load_file('config/s3.yml').symbolize_keys!)
=> #<AWS::S3::Connection:0x007feb03ab2bd8 #options={:server=>"s3.amazonaws.com", :port=>80, :access_key_id=>"******************", :secret_access_key=>"******************"}, #access_key_id="*****************", #secret_access_key="******************", #http=#<Net::HTTP s3.amazonaws.com:80 open=false>>
2.0.0p195 :017 > AWS::S3::Bucket.create('****')
=> AWS::S3::BucketAlreadyOwnedByYou: Your previous request to create the named bucket succeeded and you already own it.
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/error.rb:38:in `raise'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/base.rb:72:in `request'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/base.rb:88:in `put'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/aws-s3-0.6.3/lib/aws/s3/bucket.rb:79:in `create'
from (irb):17
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /usr/local/rvm/gems/ruby-2.0.0-p195/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0p195 :018 >
I can't tell if it's working, or what to do next. I am using this as a guideline but my results look different. What am I doing wrong?
It seems your bucket has already created, you can try create one with different name

Rails application not starting

I have a new application that is giving me errors when I do
rails server
Here is the error:
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/script_rails_loader.rb:11: warning: Insecure world writable dir /home/agenadinik in PATH, mode 040777
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /home/agenadinik in PATH, mode 040777
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/builder.rb:35:in `eval': /home/agenadinik/workspace/udfr/config.ru:6: syntax error, unexpected keyword_end, expecting ')' (SyntaxError)
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/builder.rb:35:in `parse_file'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:162:in `app'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:248:in `wrapped_app'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:213:in `start'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/server.rb:65:in `start'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:30:in `block in <top (required)>'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:27:in `tap'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
It seems to be complaining about my config.ru file. Here it is:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Udfr::Application
Here is my environment.rb file:
# Load the rails application
require File.join(File.dirname(__FILE__), 'boot')
require File.expand_path('../application', __FILE__)
if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
# Initialize the rails application
Rails::Initializer.run do |config|
Udfr::Application.initialize!
I changed that file according to these instructions:
http://www.digitalsanctum.com/2007/07/24/jruby-deploying-a-rails-application-on-tomcat/
Any idea what the problem is?
The instructions you followed are for Rails 2 and will not work with your Rails 3 app.
The updated instructions for getting Ruby on Rails to work on jruby are to edit your gemfile and place your gem requirements there.
In Gemfile:
if defined?(JRUBY_VERSION)
gem 'activerecord-jdbc-adapter'
end
Then run bundle.

Can't run rails server after installing custom-err-msg

After I install the custom-err-msg plugin, I get an error when I try to run rails server:
$ rails server
=> Booting WEBrick
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/jason/rails/snip/vendor/plugins/custom-err-msg/init.rb:3: uninitialized constant ActiveRecord::Errors (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/plugin.rb:81
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
from /home/jason/rails/snip/config/environment.rb:5
from /home/jason/rails/snip/config.ru:3:in `require'
from /home/jason/rails/snip/config.ru:3
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /home/jason/rails/snip/config.ru:1:in `new'
from /home/jason/rails/snip/config.ru:1
Any idea why this is happening?
Here's an updated version that will work with Rails 3.
https://github.com/seansawyer/custom-err-msg
To fix the undefined method keys for nil:NilClass error, a method in lib/custom_error_message.rb needs to be changed to this:
def starts_with_humanized_column_followed_by_circumflex?(message)
keys.any? do |column|
humanized = #base.class.human_attribute_name column.to_s.split('.').last.to_s
message.match(/^#{humanized} \^/)
end
end
Someone, please send him a pull request!
This plugin is not compatible with Rails 3. I would recommend finding a fork that is or forking it and updating yourself.

Resources