-
-
Save anonymous/01dfeb8d14b4bfeb3988 to your computer and use it in GitHub Desktop.
Why am i getting an argument error on line 13?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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