Class: NiceHttp
- Inherits:
-
Object
- Object
- NiceHttp
- Includes:
- NiceHttpHttpMethods, NiceHttpManageRequest, NiceHttpManageResponse
- Defined in:
- lib/nice_http.rb
Overview
Attributes you can access using NiceHttp.the_attribute: :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port, :last_request, :last_response, :request_id, :use_mocks, :connections, :active, :auto_redirect, :values_for
Constant Summary collapse
- Error =
Class.new StandardError
- InfoMissing =
Class.new Error do attr_reader :attribute def initialize(attribute) @attribute = attribute = "It was not possible to create the http connection!!!\n" += "Wrong #{attribute}, remember to supply http:// or https:// in case you specify an url to create the http connection, for example:\n" += "http = NiceHttp.new('http://example.com')" super end end
Class Attribute Summary collapse
-
.active ⇒ Object
Returns the value of attribute active.
-
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
-
.connections ⇒ Object
Returns the value of attribute connections.
-
.debug ⇒ Object
Returns the value of attribute debug.
-
.headers ⇒ Object
Returns the value of attribute headers.
-
.host ⇒ Object
Returns the value of attribute host.
-
.last_request ⇒ Object
Returns the value of attribute last_request.
-
.last_response ⇒ Object
Returns the value of attribute last_response.
-
.log ⇒ Object
Returns the value of attribute log.
-
.log_files ⇒ Object
Returns the value of attribute log_files.
-
.port ⇒ Object
Returns the value of attribute port.
-
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
-
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
.request_id ⇒ Object
Returns the value of attribute request_id.
-
.ssl ⇒ Object
Returns the value of attribute ssl.
-
.use_mocks ⇒ Object
Returns the value of attribute use_mocks.
-
.values_for ⇒ Object
Returns the value of attribute values_for.
Instance Attribute Summary collapse
-
#active ⇒ Integer
Number of active connections.
-
#auto_redirect ⇒ Boolean
If true, NiceHttp will take care of the auto redirections when required by the responses.
-
#connections ⇒ Array
It will include all the active connections (NiceHttp instances).
-
#cookies ⇒ Hash
Cookies set.
-
#debug ⇒ Boolean
In case true shows all the details of the communication with the host.
-
#headers ⇒ Hash
Contains the headers you will be using on your connection.
-
#host ⇒ String
The host to be accessed.
-
#last_request ⇒ String
The last request with all the content sent.
-
#last_response ⇒ String
Only in case :debug is true, the last response with all the content.
-
#log ⇒ String, Symbol
:fix_file, :no, :screen, :file, "path and file name".
-
#logger ⇒ Logger
An instance of the Logger class where logs will be stored.
-
#num_redirects ⇒ Integer
Number of consecutive redirections managed.
-
#port ⇒ Integer
The port number.
-
#proxy_host ⇒ String
the proxy host to be used.
-
#proxy_port ⇒ Integer
the proxy port to be used.
-
#request_id ⇒ String
If the response includes a requestId, will be stored here.
-
#response ⇒ Hash
Contains the full response hash.
-
#ssl ⇒ Boolean
If you use ssl or not.
-
#use_mocks ⇒ Boolean
If true, in case the request hash includes a :mock_response key, it will be used as the response instead.
-
#values_for ⇒ Hash
The default values to set on the data in case not specified others.
Class Method Summary collapse
-
.defaults=(par = {}) ⇒ Object
Change the default values for NiceHttp supplying a Hash.
-
.inherited(subclass) ⇒ Object
If inheriting from NiceHttp class.
-
.reset! ⇒ Object
to reset to the original defaults.
Instance Method Summary collapse
-
#close ⇒ Object
Close HTTP connection.
-
#initialize(args = {}) ⇒ NiceHttp
constructor
Creates a new http connection.
Constructor Details
#initialize(args = {}) ⇒ NiceHttp
Creates a new http connection.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/nice_http.rb', line 170 def initialize(args = {}) require "net/http" require "net/https" @host = self.class.host @port = self.class.port @prepath = '' @ssl = self.class.ssl @headers = self.class.headers.dup @values_for = self.class.values_for.dup @debug = self.class.debug @log = self.class.log @proxy_host = self.class.proxy_host @proxy_port = self.class.proxy_port @use_mocks = self.class.use_mocks @auto_redirect = false #set it up at the end of initialize auto_redirect = self.class.auto_redirect @num_redirects = 0 #todo: set only the cookies for the current domain #key: path, value: hash with key is the name of the cookie and value the value # we set the default value for non existing keys to empty Hash {} so in case of merge there is no problem = Hash.new { |h, k| h[k] = {} } if args.is_a?(String) uri = URI.parse(args) @host = uri.host unless uri.host.nil? @port = uri.port unless uri.port.nil? @ssl = true if !uri.scheme.nil? && (uri.scheme == "https") @prepath = uri.path unless uri.path=='/' elsif args.is_a?(Hash) && !args.keys.empty? @host = args[:host] if args.keys.include?(:host) @port = args[:port] if args.keys.include?(:port) @ssl = args[:ssl] if args.keys.include?(:ssl) @headers = args[:headers].dup if args.keys.include?(:headers) @values_for = args[:values_for].dup if args.keys.include?(:values_for) @debug = args[:debug] if args.keys.include?(:debug) @log = args[:log] if args.keys.include?(:log) @proxy_host = args[:proxy_host] if args.keys.include?(:proxy_host) @proxy_port = args[:proxy_port] if args.keys.include?(:proxy_port) @use_mocks = args[:use_mocks] if args.keys.include?(:use_mocks) auto_redirect = args[:auto_redirect] if args.keys.include?(:auto_redirect) end begin mode = "a" log_filename ='' if @log.kind_of?(String) #only the first connection in the run will be deleting the log file log_filename = @log mode = "w" unless self.class.log_files.include?(log_filename) f = File.new(log_filename, mode) f.sync = true @logger = Logger.new f elsif @log == :fix_file #only the first connection in the run will be deleting the log file log_filename = 'nice_http.log' mode = "w" unless self.class.log_files.include?(log_filename) f = File.new(log_filename, mode) f.sync = true @logger = Logger.new f elsif @log == :file log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log" #only the first connection in the run will be deleting the log file mode = "w" unless self.class.log_files.include?(log_filename) f = File.new(log_filename, mode) f.sync = true @logger = Logger.new f elsif @log == :screen @logger = Logger.new STDOUT elsif @log == :no @logger = Logger.new nil else raise InfoMissing, :log end @logger.level = Logger::INFO self.class.log_files << log_filename if mode == 'w' rescue Exception => stack @logger = Logger.new nil raise InfoMissing, :log end if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("https:")) uri = URI.parse(@host) @host = uri.host unless uri.host.nil? @port = uri.port unless uri.port.nil? @ssl = true if !uri.scheme.nil? && (uri.scheme == "https") @prepath = uri.path unless uri.path=='/' end raise InfoMissing, :port if @port.to_s == "" raise InfoMissing, :host if @host.to_s == "" raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass) raise InfoMissing, :debug unless @debug.is_a?(TrueClass) or @debug.is_a?(FalseClass) raise InfoMissing, :auto_redirect unless auto_redirect.is_a?(TrueClass) or auto_redirect.is_a?(FalseClass) raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass) raise InfoMissing, :headers unless @headers.is_a?(Hash) raise InfoMissing, :values_for unless @values_for.is_a?(Hash) begin if !@proxy_host.nil? && !@proxy_port.nil? @http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(@host, @port) @http.use_ssl = @ssl @http.set_debug_output $stderr if @debug @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http.start else @http = Net::HTTP.new(@host, @port) @http.use_ssl = @ssl @http.set_debug_output $stderr if @debug @http.verify_mode = OpenSSL::SSL::VERIFY_NONE @http.start end = "(#{self.object_id}):" = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} " @logger.info() += " Http connection: " if @ssl += "https://" else += "http://" end += "#{@host}:#{@port}" if @proxy_host.to_s != "" += " proxy:#{@proxy_host}:#{@proxy_port}" end @auto_redirect = auto_redirect self.class.active += 1 self.class.connections.push(self) rescue Exception => stack puts stack @logger.fatal stack end end |
Class Attribute Details
.active ⇒ Object
Returns the value of attribute active.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def active @active end |
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def auto_redirect @auto_redirect end |
.connections ⇒ Object
Returns the value of attribute connections.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def connections @connections end |
.debug ⇒ Object
Returns the value of attribute debug.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def debug @debug end |
.headers ⇒ Object
Returns the value of attribute headers.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def headers @headers end |
.host ⇒ Object
Returns the value of attribute host.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def host @host end |
.last_request ⇒ Object
Returns the value of attribute last_request.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def last_request @last_request end |
.last_response ⇒ Object
Returns the value of attribute last_response.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def last_response @last_response end |
.log ⇒ Object
Returns the value of attribute log.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def log @log end |
.log_files ⇒ Object
Returns the value of attribute log_files.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def log_files @log_files end |
.port ⇒ Object
Returns the value of attribute port.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def port @port end |
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def proxy_host @proxy_host end |
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def proxy_port @proxy_port end |
.request_id ⇒ Object
Returns the value of attribute request_id.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def request_id @request_id end |
.ssl ⇒ Object
Returns the value of attribute ssl.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def ssl @ssl end |
.use_mocks ⇒ Object
Returns the value of attribute use_mocks.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def use_mocks @use_mocks end |
.values_for ⇒ Object
Returns the value of attribute values_for.
65 66 67 |
# File 'lib/nice_http.rb', line 65 def values_for @values_for end |
Instance Attribute Details
#active ⇒ Integer
Number of active connections
44 45 46 |
# File 'lib/nice_http.rb', line 44 def active @active end |
#auto_redirect ⇒ Boolean
If true, NiceHttp will take care of the auto redirections when required by the responses
44 45 46 |
# File 'lib/nice_http.rb', line 44 def auto_redirect @auto_redirect end |
#connections ⇒ Array
It will include all the active connections (NiceHttp instances)
44 45 46 |
# File 'lib/nice_http.rb', line 44 def connections @connections end |
#cookies ⇒ Hash
Cookies set. The key is the path (String) where cookies are set and the value a Hash with pairs of cookie keys and values, example: { '/' => { "cfid" => "d95adfas2550255", "amddom.settings" => "doom" } }
44 45 46 |
# File 'lib/nice_http.rb', line 44 def end |
#debug ⇒ Boolean
In case true shows all the details of the communication with the host
44 45 46 |
# File 'lib/nice_http.rb', line 44 def debug @debug end |
#headers ⇒ Hash
Contains the headers you will be using on your connection
44 45 46 |
# File 'lib/nice_http.rb', line 44 def headers @headers end |
#host ⇒ String
The host to be accessed
44 45 46 |
# File 'lib/nice_http.rb', line 44 def host @host end |
#last_request ⇒ String
The last request with all the content sent
44 45 46 |
# File 'lib/nice_http.rb', line 44 def last_request @last_request end |
#last_response ⇒ String
Only in case :debug is true, the last response with all the content
44 45 46 |
# File 'lib/nice_http.rb', line 44 def last_response @last_response end |
#log ⇒ String, Symbol
:fix_file, :no, :screen, :file, "path and file name". :fix_file will log the communication on nice_http.log. (default). :no will not generate any logs. :screen will print the logs on the screen. :file will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log. String the path and file name where the logs will be stored.
44 45 46 |
# File 'lib/nice_http.rb', line 44 def log @log end |
#logger ⇒ Logger
An instance of the Logger class where logs will be stored. You can access on anytime to store specific data, for example: my_http.logger.info "add this to the log file"
44 45 46 |
# File 'lib/nice_http.rb', line 44 def logger @logger end |
#num_redirects ⇒ Integer
Number of consecutive redirections managed
44 45 46 |
# File 'lib/nice_http.rb', line 44 def num_redirects @num_redirects end |
#port ⇒ Integer
The port number
44 45 46 |
# File 'lib/nice_http.rb', line 44 def port @port end |
#proxy_host ⇒ String
the proxy host to be used
44 45 46 |
# File 'lib/nice_http.rb', line 44 def proxy_host @proxy_host end |
#proxy_port ⇒ Integer
the proxy port to be used
44 45 46 |
# File 'lib/nice_http.rb', line 44 def proxy_port @proxy_port end |
#request_id ⇒ String
If the response includes a requestId, will be stored here
44 45 46 |
# File 'lib/nice_http.rb', line 44 def request_id @request_id end |
#response ⇒ Hash
Contains the full response hash
44 45 46 |
# File 'lib/nice_http.rb', line 44 def response @response end |
#ssl ⇒ Boolean
If you use ssl or not
44 45 46 |
# File 'lib/nice_http.rb', line 44 def ssl @ssl end |
#use_mocks ⇒ Boolean
If true, in case the request hash includes a :mock_response key, it will be used as the response instead
44 45 46 |
# File 'lib/nice_http.rb', line 44 def use_mocks @use_mocks end |
#values_for ⇒ Hash
The default values to set on the data in case not specified others
44 45 46 |
# File 'lib/nice_http.rb', line 44 def values_for @values_for end |
Class Method Details
.defaults=(par = {}) ⇒ Object
Change the default values for NiceHttp supplying a Hash
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/nice_http.rb', line 109 def self.defaults=(par = {}) @host = par[:host] if par.key?(:host) @port = par[:port] if par.key?(:port) @ssl = par[:ssl] if par.key?(:ssl) @headers = par[:headers].dup if par.key?(:headers) @values_for = par[:values_for].dup if par.key?(:values_for) @debug = par[:debug] if par.key?(:debug) @log = par[:log] if par.key?(:log) @proxy_host = par[:proxy_host] if par.key?(:proxy_host) @proxy_port = par[:proxy_port] if par.key?(:proxy_port) @use_mocks = par[:use_mocks] if par.key?(:use_mocks) @auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect) end |
.inherited(subclass) ⇒ Object
If inheriting from NiceHttp class
97 98 99 |
# File 'lib/nice_http.rb', line 97 def self.inherited(subclass) subclass.reset! end |
.reset! ⇒ Object
to reset to the original defaults
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/nice_http.rb', line 73 def self.reset! @host = nil @port = 80 @ssl = false @headers = {} @values_for = {} @debug = false @log = :fix_file @proxy_host = nil @proxy_port = nil @last_request = nil @last_response = nil @request_id = "" @use_mocks = false @connections = [] @active = 0 @auto_redirect = true @log_files = [] end |
Instance Method Details
#close ⇒ Object
Close HTTP connection
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/nice_http.rb', line 312 def close begin pos = 0 found = false self.class.connections.each { |conn| if conn.object_id == self.object_id found = true break else pos += 1 end } if found self.class.connections.delete_at(pos) end unless @closed if !@http.nil? @http.finish() @http = nil @logger.info "the HTTP connection was closed: #{@message_server}" else @http = nil @logger.fatal "It was not possible to close the HTTP connection: #{@message_server}" end @closed = true else @logger.warn "It was not possible to close the HTTP connection, already closed: #{@message_server}" end rescue Exception => stack @logger.fatal stack end self.class.active -= 1 end |