Skip to content

Instantly share code, notes, and snippets.

Created June 5, 2015 18:17
Show Gist options
  • Save anonymous/01dfeb8d14b4bfeb3988 to your computer and use it in GitHub Desktop.
Save anonymous/01dfeb8d14b4bfeb3988 to your computer and use it in GitHub Desktop.
Why am i getting an argument error on line 13?
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
module Bf
module Web
# Sends an http get request and returns an http response object
#
# * uri: String: "The desired uri"
# * username: String
# * password: String
# * print_headers_on_redirect: Bool: Will print headers if the request
# gets redirected. Defaults to false.
def Web.get(uri, username, password, print_headers_on_redirect=false)
# URI object
u = URI(uri)
# Start a session
Net::HTTP::start(u.host, u.port, :use_ssl => u.scheme == 'https') do |http|
# force https
if u.scheme == 'http'
u.scheme = 'https'
end
request = Net::HTTP::Get.new uri
# request.basic_auth(username, password)
response = http.request(request)
case response
when Net::HTTPSuccess
response
when Net::HTTPRedirection
location = response['location']
if print_headers_on_redirect
response.each_header {|key, val| puts "#{key}: #{val}"}
end
get(location)
else
response
end
end
end
end
end
# path = "http://www.github.com/users/miroswan/repos"
path = "https://google.com"
response_obj = Bf::Web.get(path, 'username', 'password')
puts response_obj.body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment