Class: HttpSend
Instance Method Summary collapse
- #adjust(http, uri) ⇒ Object
- #create_request(uri, method) ⇒ Object
- #get_proxy_params ⇒ Object
-
#initialize ⇒ HttpSend
constructor
A new instance of HttpSend.
- #retry(url, method, headers, body) ⇒ Object
- #save_http(url, method, body, response_code, response_body) ⇒ Object
- #send(url, method, headers, body = nil) ⇒ Object
- #set_headers(request, headers) ⇒ Object
Methods included from ConfigReader
#config_dir, #config_file, #mkdir, #param, #user_file
Constructor Details
#initialize ⇒ HttpSend
Returns a new instance of HttpSend.
13 14 15 |
# File 'lib/http_send.rb', line 13 def initialize get_proxy_params end |
Instance Method Details
#adjust(http, uri) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/http_send.rb', line 36 def adjust(http, uri) # http.set_debug_output $stderr http.read_timeout = 60 http.open_timeout = 60 if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end end |
#create_request(uri, method) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/http_send.rb', line 23 def create_request(uri, method) case method when :put request = Net::HTTP::Put.new(uri) when :get request = Net::HTTP::Get.new(uri) when :post request = Net::HTTP::Post.new(uri) when :delete request = Net::HTTP::Delete.new(uri) end end |
#get_proxy_params ⇒ Object
7 8 9 10 11 |
# File 'lib/http_send.rb', line 7 def get_proxy_params @proxy = param("proxy") @proxy_username = param("proxy-Username") @proxy_password = param("proxy-Password") end |
#retry(url, method, headers, body) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/http_send.rb', line 102 def retry(url, method, headers, body) if @proxy_retries && @proxy_retries > 1 raise "Proxy Authentication Failed" else @proxy_retries = @proxy_retries ? @proxy_retries + 1 : 1 send(url, method, headers, body) end end |
#save_http(url, method, body, response_code, response_body) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/http_send.rb', line 46 def save_http(url, method, body, response_code, response_body) if File.exists? $http_save_file array = JSON.parse(File.open($http_save_file).read) else array = [ ] end o = Hash.new o["url"] = url o["method"] = method o["body"] = body o["response_code"] = response_code o["response_body"] = response_body array << o file = File.open($http_save_file + ".tmp", "w") file.write(JSON.pretty_generate(array)) file.close FileUtils.mv($http_save_file + ".tmp", $http_save_file, :force => true) end |
#send(url, method, headers, body = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/http_send.rb', line 65 def send(url, method, headers, body = nil) uri = URI(url) request = create_request(uri, method) if body request.body = body end set_headers(request, headers) if @proxy proxy_uri = URI(@proxy) http = Net::HTTP.new(uri.host, uri.port, proxy_uri.hostname, proxy_uri.port, @proxy_username, @proxy_password) else http = Net::HTTP.new(uri.host, uri.port) end adjust(http, uri) response_code = -1 response_body = nil begin response = http.request(request) response_code = response.code.to_i response_body = response.body rescue Net::HTTPServerException => e if e. == '407 "Proxy Authentication Required"' ProxyCredentials.proxy_credentials(@proxy) get_proxy_params return self.retry(url, method, headers, body) else response_body = "#{e.class} exception occurred:#{e.}" end rescue Exception => e response_body = "#{e.class} exception occurred:#{e.}" end if defined? $http_save_file save_http(url, method, body, response_code, response_body) end return response_code, response_body end |
#set_headers(request, headers) ⇒ Object
17 18 19 20 21 |
# File 'lib/http_send.rb', line 17 def set_headers(request, headers) headers.each { |name, value| request[name.to_s] = value } end |