Class: HttpRequest
Constant Summary collapse
- VERSION =
version
'1.1.14'.freeze
Class Method Summary collapse
-
.available?(url, timeout = 5) ⇒ Boolean
check the http resource whether or not available.
-
.cookies ⇒ Object
return cookies.
-
.data(response, &block) ⇒ Object
return data with or without block.
-
.ftp(method, options, &block) ⇒ Object
for ftp, no plan to add new features to this method except bug fixing.
-
.http_methods ⇒ Object
available http methods.
-
.update_cookies(response) ⇒ Object
update cookies.
- .version ⇒ Object
Instance Method Summary collapse
- #data(response, &block) ⇒ Object
-
#request(method, options, &block) ⇒ Object
send request by some given parameters.
Class Method Details
.available?(url, timeout = 5) ⇒ Boolean
check the http resource whether or not available
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/http_request.rb', line 58 def available?(url, timeout = 5) timeout(timeout) { u = URI(url) s = TCPSocket.new(u.host, u.port) s.cspecify lose } return true rescue Exception => e return false end |
.cookies ⇒ Object
return cookies
53 54 55 |
# File 'lib/http_request.rb', line 53 def @@__cookies[@@__cookie_jar] end |
.data(response, &block) ⇒ Object
return data with or without block
38 39 40 41 |
# File 'lib/http_request.rb', line 38 def data(response, &block) response.url = @@__url if defined? @@__url block_given? ? block.call(response) : response end |
.ftp(method, options, &block) ⇒ Object
for ftp, no plan to add new features to this method except bug fixing
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/http_request.rb', line 131 def self.ftp(method, , &block) = {:url => } if .is_a? String = {:close => true}.merge() @@__url = [:url] = "ftp://#{[:url]}" unless [:url] =~ /^ftp:\/\// uri = URI([:url]) guest_name, guest_pass = 'anonymous', "guest@#{uri.host}" unless [:username] [:username], [:password] = uri.userinfo ? uri.userinfo.split(':') : [guest_name, guest_pass] end [:username] = guest_name unless [:username] [:password] = guest_pass if [:password].nil? ftp = Net::FTP.open(uri.host, [:username], [:password]) return data(ftp, &block) unless method stat = case method.to_sym when :get_as_string require 'tempfile' tmp = Tempfile.new('http_request_ftp') ftp.getbinaryfile(uri.path, tmp.path) ftp.response = tmp.read tmp.close unless block_given? ftp.close return ftp.response end when :get [:to] = File.basename(uri.path) unless [:to] ftp.getbinaryfile(uri.path, [:to]) when :put ftp.putbinaryfile([:from], uri.path) when :mkdir, :rmdir, :delete, :size, :mtime, :list, :nlst ftp.method(method).call(uri.path) when :rename ftp.rename(uri.path, [:to]) if [:to] when :status ftp.status else return ftp end if [:close] and not block_given? ftp.close stat else ftp.response = stat unless ftp.response data(ftp, &block) end end |
.http_methods ⇒ Object
available http methods
33 34 35 |
# File 'lib/http_request.rb', line 33 def http_methods %w{get head post put proppatch lock unlock options propfind delete move copy mkcol trace} end |
.update_cookies(response) ⇒ Object
update cookies
44 45 46 47 48 49 50 |
# File 'lib/http_request.rb', line 44 def (response) return unless response.header['set-cookie'] response.get_fields('set-cookie').each {|k| k, v = k.split(';')[0].split('=') @@__cookies[@@__cookie_jar][k] = CGI.unescape(v) } end |
.version ⇒ Object
30 |
# File 'lib/http_request.rb', line 30 def version;VERSION;end |
Instance Method Details
#data(response, &block) ⇒ Object
108 109 110 |
# File 'lib/http_request.rb', line 108 def data(response, &block) self.class.data(response, &block) end |
#request(method, options, &block) ⇒ Object
send request by some given parameters
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/http_request.rb', line 113 def request(method, , &block) # parse the @options (method, ) # parse and merge for the options[:parameters] parse_parameters # send http request and get the response response = send_request_and_get_response return data(response, &block) unless @options[:redirect] # redirect? process_redirection response, &block end |