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

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.

Related

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

How to specify memcache server to Rack::Session::Memcache?

I'm trying to configure my Rack app to use Memcache for sessions with Rack::Session::Memcache
How do I give it the options (such as server, username and password)?
Presently I have
use Rack::Session::Memcache
But I get the error
in `initialize': No memcache servers (RuntimeError)
Heroku has put the config in environment variables
MEMCACHE_PASSWORD:
MEMCACHE_SERVERS:
MEMCACHE_USERNAME:
I know I can get these in Ruby with ENV['MEMCACHE_PASSWORD'] but I don't know how to give them to Rack::Session::Memcache
Edit: or to Rack::Session::Dalli that would be great too https://github.com/mperham/dalli
This config worked for Heroku, Dalli is clever and knows to look in the environment variables
require 'dalli'
require 'rack/session/dalli'
use Rack::Session::Dalli, :cache => Dalli::Client.new
After reading the source code at https://github.com/mperham/dalli/commit/4ac5a99

Weird LoadError on custom ruby gem

I have a custom gem and am encountering a really weird LoadError when I install it as a gem and attempt to require it in irb.
Everything works fine with my rspec tests inside the project folder. This only occurs when using it as an actual gem in irb.
The file it throws a LoadError exception at (/lib/mws/api/order_response.rb) does in fact exist. I've tried renaming the file and updating the file that requires it (/lib/mws.rb). I've tried recreating the file thinking maybe there was a permissions issue. Nothing works.
If I comment out the require line for that specific file, everything works. There's nothing special about the file. There's 4 other files nearly identical to it (*_response.rb).
I feel like I'm taking crazy pills. I must being overlooking something but I sure don't see it.
Trace:
chris#Samus:~$ irb
1.9.3p194 :001 > require 'mws'
LoadError: cannot load such file -- mws/api/order_response
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws.rb:14:in `<top (required)>'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
File with the requires (/lib/mws.rb)
require 'mws/base'
require 'mws/connection'
require 'mws/utility'
require 'mws/api/seller'
require 'mws/api/product'
require 'mws/api/order'
require 'mws/api/report'
require 'mws/api/general_response'
require 'mws/api/product_response'
require 'mws/api/report_response'
require 'mws/api/seller_response'
require 'mws/api/order_response' # <--- the offending line
module MWS
# #see Base#initialize MWS::Base for instantiation details.
# #return [Base] returns MWS::Base object.
def self.new(merchant_id, access_key, secret_key)
MWS::Base.new(merchant_id, access_key, secret_key)
end
end
# The below is for documentation generation purposes.
# MWS is a wrapper for the Amazon Marketplace Web Service (MWS) API.
module MWS
# API handles all the Amazon MWS API specific stuff.
module API
end
# Utilities contains various functions needed throughout MWS. Utilities is a mixin to multiple classes.
module Utilities
end
end
File I'm requiring (/lib/mws/api/order_response.rb):
module MWS
module API
# Class for parsing Amazon's XML responses into managable objects.
class OrderResponse
# Include GeneralResponse instance methods as class methods
extend GeneralResponse
end
end
end
And my file structure
For anyone interested, I was using Jeweler to handle building this gem. As it turns out, Jeweler uses your Git repository when building a gemspec.
If you haven't added all required files to your git repository, Jeweler's gemspec rake task will not include them when generating a new gemspec file.
Can should check in /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws/api if the file lies there (and doesn't have obscure permissions).
If that's not the case, you probably forgot to add it in your gemspec.
If it is there, please try requiring/loading it with the absolute path (for debugging purpose).

Checking if proxy is working in 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.

Ruby require a code snippet from github

Is there anyway to get Ruby's require statement to download a file from somewhere like github rather than just the local file system?
Update: Sorry I should have made the question clearer. I want to download a file that contains Ruby module and import it into my script rather than just downloading an image or some other arbitrary file within my script.
In other words something like this
require 'http:\\github.com\myrepo\snippet.rb'
puts 'hi'
By default, this is not possible. Also, it's not a good idea for security reasons.
You have a couple of alternatives. If the file you want to include is a Gem and Git repository, then you can use Bundler to download and package the dependency in your project. Then you'll be able to require the file directly in your source code.
This is the best and safest way to include an external dependency.
If you trust the source and you really know what you are doing, you can download the file using Net::HTTP (or any other HTTP library) and eval the body directly in your Ruby code.
You can package everything in a custom require_remote function.
You could download and eval it
require "open-uri"
alias :require_old :require
def require(path)
return false if $".include?(path)
unless path=~ /\Ahttp:\/\/
return require_old(path)
end
eval(open(path).read)
$"<< path
true
end
Be aware, this code has no error checking for network outages nonexisting files, ... . I also believe it is in general not a good idea to require libraries this way, there are security and reliability problems in this approach. But maybe you have a valid usecase for this.
you can include a remote gem from within Gemfiles then it will download when you run bundle install
After reading this question and answers I wanted something a little more bullet proof and verbose that used a paradigm of creating a local file from a repo and then requiring it, only if it didn't already exist locally already. The request for the repo version is explicit via the method repo_require. Used on files you control, this approach improves security IMO.
# try local load
def local_require(filename, relative_path)
relative_flname = File.join(relative_path, filename)
require_relative(relative_flname)
end
# try loading locally first, try repo version on load error
# caution: only use with files you control access to!
def repo_require(raw_repo_prefix, filename, relative_path = '')
local_require(filename, relative_path)
rescue LoadError => e
puts e.message
require 'open-uri'
tempdir = Dir.mktmpdir("repo_require-")
temp_flname = File.join(tempdir, File.basename(filename))
return false if $LOADED_FEATURES.include?(temp_flname)
remote_flname = File.join(raw_repo_prefix, filename)
puts "file not found locally, checking repo: #{remote_flname}"
begin
File.open(temp_flname, 'w') do |f|
f.write(URI.parse(remote_flname).read)
end
rescue OpenURI::HTTPError => e
raise "Error: Can't load #{filename} from repo: #{e.message} - #{remote_flname}"
end
require(temp_flname)
FileUtils.remove_entry(tempdir)
end
Then you could call repo_require like this:
repo_require('https://raw.githubusercontent.com/username/reponame/branch',
'filename', 'relative_path')
The relative_path would the the relative path you would use for the file if the repo was locally installed. For example, you may have something like require_relative '../lib/utils.rb'. In this example filename='lib/utils.rb' and relative_path='..'. This information allows the repo url to be constructed correctly as it does not use the relative path portion.

Resources