Class: HttpRequest

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/http_request.rb

Constant Summary collapse

VERSION =

version

'1.1.7'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?(url, timeout = 5) ⇒ Boolean

check the http resource whether or not available

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/http_request.rb', line 64

def available?(url, timeout = 5)
	timeout(timeout) {
		u = URI(url)
		s = TCPSocket.new(u.host, u.port)
		s.close
	}
	return true
rescue Exception => e
	return false
end

.cookiesObject

return cookies



59
60
61
# File 'lib/http_request.rb', line 59

def cookies
	@@__cookies
end

.data(response, &block) ⇒ Object

return data with or without block



44
45
46
47
# File 'lib/http_request.rb', line 44

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/http_request.rb', line 115

def self.ftp(method, options, &block)
	options = {:url => options} if options.is_a? String
	options = {:close => true}.merge(options)
	@@__url = options[:url] = "ftp://#{options[:url]}" unless options[:url] =~ /^ftp:\/\//
	uri = URI(options[:url])
	guest_name, guest_pass = 'anonymous', "guest@#{uri.host}"
	unless options[:username]
		options[:username], options[:password] = 
			uri.userinfo ? uri.userinfo.split(':') : [guest_name, guest_pass]
	end
	options[:username] = guest_name unless options[:username]
	options[:password] = guest_pass if options[:password].nil?
	ftp = Net::FTP.open(uri.host, options[:username], options[: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
					 options[:to] = File.basename(uri.path) unless options[:to]
					 ftp.getbinaryfile(uri.path, options[:to])
				 when :put
					 ftp.putbinaryfile(options[:from], uri.path)
				 when :mkdir, :rmdir, :delete, :size, :mtime, :list, :nlst
					 ftp.method(method).call(uri.path)
				 when :rename
					 ftp.rename(uri.path, options[:to]) if options[:to]
				 when :status
					 ftp.status
				 else
					 return ftp
				 end
	if options[:close] and not block_given?
		ftp.close
		stat
	else
		ftp.response = stat unless ftp.response
		data(ftp, &block)
	end
end

.http_methodsObject

avaiabled http methods



39
40
41
# File 'lib/http_request.rb', line 39

def http_methods
	%w{get head post put proppatch lock unlock options propfind delete move copy mkcol trace}
end

.method_missing(method_name, *options, &block) ⇒ Object

catch all available http requests



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/http_request.rb', line 99

def self.method_missing(method_name, *options, &block)
       options = if options.size.eql? 2
                     options.last.merge({:url => options.first})
                 else
                     options.first
                 end
       @@redirect_times = 0
	# we need to retrieve the cookies from last http response before reset cookies if it's a Net::HTTPResponse
	options[:cookies] = options[:cookies].cookies	if options[:cookies].is_a? Net::HTTPResponse
	@@__cookies = {}
	method_name = method_name.to_s.downcase
	raise NoHttpMethodException, "No such http method can be called: #{method_name}" unless self.http_methods.include?(method_name)
	self.instance.request(method_name, options, &block)
end

.update_cookies(response) ⇒ Object

update cookies



50
51
52
53
54
55
56
# File 'lib/http_request.rb', line 50

def update_cookies(response)
	return unless response.header['set-cookie']
	response.get_fields('set-cookie').each {|k|
		k, v = k.split(';')[0].split('=')
		@@__cookies[k] = v
	}
end

.versionObject



36
# File 'lib/http_request.rb', line 36

def version;VERSION;end

Instance Method Details

#data(response, &block) ⇒ Object



76
77
78
# File 'lib/http_request.rb', line 76

def data(response, &block)
	self.class.data(response, &block)
end

#request(method, options, &block) ⇒ Object

send request by some given parameters



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/http_request.rb', line 81

def request(method, options, &block)

	# parse the @options
	parse_options(method, options)

	# 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