Checking if proxy is working in Ruby - ruby

I'm new to Ruby and trying to find a way to check if a proxy is working. I've tried using the 'net/ping' library but I am just getting some errors while requiring it(in Interactive Ruby the script works even though there are the errors, it just closes itself when I run the script normally though)
I'm trying to find a solution to make it either ignore the error, fix the error or find another way to check if the proxy works.
Thanks for help.
This is the error message I get when I try to require the 'net/ping'
proxy.rb(main):005:0> require 'net/ping'
LoadError: no such file to load -- windows/console
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in'
from C:\Users\X\Desktop\proxy.rb:5:in require'
from C:\Users\X\Desktop\proxy.rb:5
from C:/Ruby191/bin/irb:12:in'

In my opinion the only way to check if a proxy is working is to connect through it and make a request.

Related

Ruby Rake ActiveRecord Migrate

I'm trying to build a simple ruby script that connects to a database and runs some basic queries.
The code is here: https://github.com/mastermindg/rack-activrecord-example
It's not a service - only a script that is run manually to do batch jobs. My problem is that I need to populate the database for testing purposes. I know how to do this in Sinatra and Rails but it's failing as-is:
NoMethodError: undefined method `set' for main:Object
Did you mean? send
/usr/src/app/app.rb:7:in `<top (required)>'
/usr/src/app/Rakefile:2:in `<top (required)>'
I've got the database.yml but I can't tell how to load it since set is failing.
How do I connect to and query a database using ActiveRecord with basic Rack?
1) Add this to your Rakefile after the requires:
DatabaseTasks.database_configuration = YAML.load(File.read(File.join(root, 'config/database.yml')))
2) Remove set :database_file, 'config/database.yml' from your app.rb file (I think set is a sinatra/activerecord method).
Running rake db:create may give you an error on your project now,because you're using json instead of JSON in your app.rb file (depending on your local versions), so. . .
3) Change puts json Resource.select('id', 'name').all to puts JSON Resource.select('id', 'name').all in your app.rb file.
Now rake db:create will throw a database error, but that's an error related to your specific database configuration, make an appropriate adjustment to that (this is off-topic from the original question, so I won't address it further) and your app should run as you desire.
More info:
This gist shows example contents of a Rakefile that you could use to run Active Record tasks without using Rails or Sinatra.

Initialize': getaddrinfo: Name or service not known (SocketError

I ve downloading cookbooks from object storage which has link https,
require 'open-uri'
open("/home/admini/Desktop/apache2.tgz","wb") do |write_file|
open("https://region-a.geo-1.objects.hpcloudsvc.com:443/v1/68342917034742/cookbooks/marketplace/production/apache2.tar","rb") do |read_file|
write_file.write(read_file.read)
end
end
it gets downloads for some providers but for some providers it doesnot get downloads ,showing like this:
from /opt/chef/embedded/lib/ruby/1.9.1/net/http.rb:762:in open'
from /opt/chef/embedded/lib/ruby/1.9.1/net/http.rb:762:inblock in connect'
from /opt/chef/embedded/lib/ruby/1.9.1/timeout.rb:54:in timeout'
from /opt/chef/embedded/lib/ruby/1.9.1/timeout.rb:99:intimeout'
I ve tried using resolv-replace gem also ,but didinot solve yet,any idea guys?
Is there any built in library method for supporting https in ruby? or we have to use gem only?
Regards,
indu
Try this library https://github.com/typhoeus/typhoeus#streaming-the-response-body it's pretty awesome, supports redirects (https://github.com/typhoeus/typhoeus#following-redirections), can do parallel downloads etc.

Heroku load error on require?

My Ruby application runs fine on my nitrous.io box, but when I push it to Heroku and it attempts to run a scheduled process, the logs show this error:
2013-12-23T22:37:11.902160+00:00 heroku[scheduler.4283]: State changed from starting to up
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/bin/rbtc:3:in `<main>'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- rbtc_arbitrage (LoadError)
2013-12-23T22:37:13.432972+00:00 heroku[scheduler.4283]: Process exited with status 1
2013-12-23T22:37:13.461438+00:00 heroku[scheduler.4283]: State changed from up to complete
This is the code in /app/bin/rbtc:3
#!/usr/bin/env ruby
require 'rbtc_arbitrage'
RbtcArbitrage::CLI.start ARGV
File structure link
I tried changing this to require_relative as in a answer to someone else on Stack Overflow to no avail.
I'm kinda at a loss here. Any help is appreciated!
Please, make sure that this file exists: lib/rbtc_arbitrage.rb which loads other files in your repo like this (syntax is valid if you are using bundler):
require 'rbtc_arbitrage/version'
require 'rbtc_arbitrage/file1'
# .. and so on
Now, adding this file should work alone, but if this does not work, try adding your lib directory to the LOAD PATH in your bin/rbtc file before any require statments, like this:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
This explicitly tells ruby to add the lib directory to the load path, and should solve your problem.

trouble setting up cucumber to test without actually changing the file system

I have been struggling for a while with this problem. I'm trying to test a ruby CLI that uses a lot of fileutils operations like making directories and copying files, etc. But when I'm writing tests, I need some way to test the functionality without touching and changing the actual filesystem. I am trying to use the fakefs gem but I am running into a error right away
When I successfully run `dotfiles init` # aruba-0.5.3/lib/aruba/cucumber.rb:71
No such file or directory - No such file or directory - /tmp/aruba-out20130817-15800-ke8nou (Errno::ENOENT)
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `open'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `block in initialize'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tmpdir.rb:142:in `create'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:136:in `initialize'
features/dotfiles_repository.feature:8:in `When I successfully run `dotfiles init`'
So its not finding /tmp/aruba which is where cucumber aruba does all of its file operations according to the readme.
The tests run fine without fakefs, so I'm assuming its something with the setup.
Here is my features/support/env.rb file
require 'aruba/cucumber'
require 'fakefs/safe'
Before do
FakeFS.activate!
end
After do
FakeFS::FileSystem.clear
FakeFS.deactivate!
end
Does anyone have any idea what I may be doing wrong? Any help is appreciated. Even if its an idea that doesn't use fakefs, I would like to hear it. Thanks

Metric Fu: RCov fails to load spec_helper

I am trying to run metric_fu on a Rails 3 application. All is good, with the exception of rcov. I have RSpec configured and my tests follow the spec/**/*.rb format and run fine in RSpec. Trying to check coverage with rcov, however, I get the following errors:
** Running the specs/tests in the [test] environment
No file to analyze was found. All the files loaded by rcov matched one of the
following expressions, and were thus ignored:
[/\A\/Users\/Eric\/\.rvm\/rubies\/ruby\-1\.8\.7\-p299\/lib/,
/\btc_[^.]*.rb/,
/_test\.rb\z/,
/\btest\//,
/\bvendor\//,
/\A\/Users\/Eric\/\.rvm\/gems\/ruby\-1\.8\.7\-p299#-backend\/gems\/rcov\-0\.9\.8\/lib\/rcov\/formatters\/base_formatter\.rb\z/,
/\bvendor\//,
/\bconfig\//,
/\benvironment\//,
/\/gems\//,
/\/Library\//,
/\/usr\//,
/spec/]
You can solve this by doing one or more of the following:
* rename the files not to be ignored so they don't match the above regexps
* use --include-file to give a list of patterns for files not to be ignored
* use --exclude-only to give the new list of regexps to match against
* structure your code as follows:
test/test_*.rb for the test cases
lib/**/*.rb for the target source code whose coverage you want
making sure that the test/test_*.rb files are loading from lib/, e.g. by
using the -Ilib command-line argument, adding
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
to test/test_*.rb, or running rcov via a Rakefile (read the RDoc
documentation or README.rake in the source distribution).
/Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- spec_helper (LoadError)
from /Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./spec/models/account_spec.rb:1
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19
When I run rcov directly, it appears that part of the issue is rcov not correctly loading the spec_helper in my specs, which is required as "require 'spec_helper'".
Any help on how to resolve this? Thanks.
Adding "-Ispec" to the Metric-Fu configuration fixes this.

Resources