Class: NiceHttp
- Inherits:
-
Object
- Object
- NiceHttp
- Defined in:
- lib/nice_http.rb,
lib/nice_http/close.rb,
lib/nice_http/reset.rb,
lib/nice_http/defaults.rb,
lib/nice_http/add_stats.rb,
lib/nice_http/inherited.rb,
lib/nice_http/initialize.rb,
lib/nice_http/save_stats.rb,
lib/nice_http/validate_response.rb
Overview
Attributes you can access using NiceHttp.the_attribute: :host, :port, :ssl, :timeout, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port, :last_request, :last_response, :request_id, :use_mocks, :connections, :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured, :request, :requests, :async_wait_seconds, :async_header, :async_completed, :async_resource, :async_status, :connection_retry_attempts, :connection_retry_base_delay
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.
-
.async_completed ⇒ Object
Returns the value of attribute async_completed.
-
.async_header ⇒ Object
Returns the value of attribute async_header.
-
.async_resource ⇒ Object
Returns the value of attribute async_resource.
-
.async_status ⇒ Object
Returns the value of attribute async_status.
-
.async_wait_seconds ⇒ Object
Returns the value of attribute async_wait_seconds.
-
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
-
.capture ⇒ Object
Returns the value of attribute capture.
-
.captured ⇒ Object
Returns the value of attribute captured.
-
.connection_retry_attempts ⇒ Object
Returns the value of attribute connection_retry_attempts.
-
.connection_retry_base_delay ⇒ Object
Returns the value of attribute connection_retry_base_delay.
-
.connections ⇒ Object
Returns the value of attribute connections.
-
.create_stats ⇒ Object
Returns the value of attribute create_stats.
-
.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.
-
.log_headers ⇒ Object
Returns the value of attribute log_headers.
-
.log_path ⇒ Object
Returns the value of attribute log_path.
-
.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 ⇒ Object
Returns the value of attribute request.
-
.request_id ⇒ Object
Returns the value of attribute request_id.
-
.requests ⇒ Object
Returns the value of attribute requests.
-
.ssl ⇒ Object
Returns the value of attribute ssl.
-
.stats ⇒ Object
Returns the value of attribute stats.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
-
.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.
-
#async_completed ⇒ String
The value of the async_header to check if the async request is completed.
-
#async_header ⇒ String
The header to check if the async request is completed.
-
#async_resource ⇒ String
The resource to check if the async request is completed.
-
#async_status ⇒ String
The status to check if the async request is completed.
-
#async_wait_seconds ⇒ Integer
Number of seconds to wait until the async request is completed.
-
#auto_redirect ⇒ Boolean
If true, NiceHttp will take care of the auto redirections when required by the responses.
-
#capture ⇒ Boolean
If true, NiceHttp will store all requests and responses on NiceHttp.captured as strings.
-
#captured ⇒ Array
It contains all the http requests and responses if NiceHttp.capture is set to true.
-
#connections ⇒ Array
It will include all the active connections (NiceHttp instances).
-
#cookies ⇒ Hash
Cookies set.
-
#create_stats ⇒ Boolean
If true, NiceHttp will create stats of the http communication and store them on NiceHttp.stats hash.
-
#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".
-
#log_file ⇒ String
path and file name where the logs will be stored.
-
#log_headers ⇒ Object
Returns the value of attribute log_headers.
-
#log_headers. ⇒ Symbol
:all, :partial, :none (default :all) If :all will log all the headers.
-
#log_path ⇒ String
The path where the logs will be stored.
-
#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 ⇒ Hash
The last request with all the content sent.
-
#request_id ⇒ String
If the response includes a requestId, will be stored here.
-
#requests ⇒ Hash
The defaults for all requests.
-
#response ⇒ Hash
Contains the full response hash.
-
#ssl ⇒ Boolean
If you use ssl or not.
-
#stats ⇒ Hash
It contains detailed stats of the http communication.
-
#timeout ⇒ Integer
Max time to wait until connected to the host or getting a response.
-
#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
-
.add_stats(name, state, started, finished, item = nil) ⇒ Object
To add specific stats The stats will be added to NiceHttp.stats[:specific].
-
.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.
-
.save_stats(file_name = "") ⇒ Object
It will save the NiceHttp.stats on different files, each key of the hash in a different file.
-
.validate_response(resp, expected_structure, options = {}) ⇒ Hash
Validates that a response body matches an expected structure (keys and nesting).
Instance Method Summary collapse
-
#close ⇒ Object
Close HTTP connection.
-
#initialize(args = {}) ⇒ NiceHttp
constructor
Creates a new http connection.
Methods included from NiceHttpHttpMethods
#delete, #get, #head, #patch, #post, #put, #send_request
Methods included from NiceHttpManageResponse
#manage_response, #wait_async_operation
Methods included from NiceHttpManageRequest
Constructor Details
#initialize(args = {}) ⇒ NiceHttp
When connection creation fails with "too many open files" (EMFILE/ENFILE), the library retries up to connection_retry_attempts times with backoff. For sustained load or many threads, prefer reusing one connection per thread and calling close when done.
Creates a new http connection.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 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 162 163 164 165 166 167 168 169 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 |
# File 'lib/nice_http/initialize.rb', line 48 def initialize(args = {}) require "net/http" require "net/https" @host = self.class.host @port = self.class.port @prepath = "" @ssl = self.class.ssl @timeout = self.class.timeout @headers = self.class.headers.dup @values_for = self.class.values_for.dup @debug = self.class.debug @log = self.class.log @log_path = self.class.log_path @log_headers = self.class.log_headers @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 @create_stats = self.class.create_stats @capture = self.class.capture @async_wait_seconds = self.class.async_wait_seconds @async_header = self.class.async_header @async_completed = self.class.async_completed @async_resource = self.class.async_resource @async_status = self.class.async_status @connection_retry_attempts = self.class.connection_retry_attempts || 3 @connection_retry_base_delay = self.class.connection_retry_base_delay || 1.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) # 'http://www.example.com' 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) @timeout = args[:timeout] if args.keys.include?(:timeout) # Keep object reference when it has .generate (e.g. nice_hash); otherwise dup to avoid mutating caller's hash if args.keys.include?(:headers) h = args[:headers] @headers = (h.is_a?(Hash) && h.respond_to?(:generate)) ? h : h.dup end @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) @log_path = args[:log_path] if args.keys.include?(:log_path) @log_headers = args[:log_headers] if args.keys.include?(:log_headers) @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) @async_wait_seconds = args[:async_wait_seconds] if args.keys.include?(:async_wait_seconds) @async_header = args[:async_header] if args.keys.include?(:async_header) @async_completed = args[:async_completed] if args.keys.include?(:async_completed) @async_resource = args[:async_resource] if args.keys.include?(:async_resource) @async_status = args[:async_status] if args.keys.include?(:async_status) @connection_retry_attempts = args[:connection_retry_attempts] if args.keys.include?(:connection_retry_attempts) @connection_retry_base_delay = args[:connection_retry_base_delay] if args.keys.include?(:connection_retry_base_delay) end log_filename = "" if @log.kind_of?(String) or @log == :fix_file or @log == :file or @log == :file_run if @log.kind_of?(String) log_filename = @log.dup unless log_filename.start_with?(".") if caller.first.start_with?(Dir.pwd) folder = File.dirname(caller.first.scan(/(.+):\d/).join) else folder = File.dirname("#{Dir.pwd}/#{caller.first.scan(/(.+):\d/).join}") end folder += "/" unless log_filename.start_with?("/") or log_filename.match?(/^\w+:/) log_filename = folder + log_filename end require "fileutils" FileUtils.mkdir_p File.dirname(log_filename) unless Dir.exist?(File.dirname(log_filename)) @logger = Logger.new nil raise InfoMissing, :log, "Wrong directory specified for logs.\n" end elsif @log == :fix_file log_filename = "nice_http.log" elsif @log == :file log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log" elsif @log == :file_run log_filename = "#{caller.first.scan(/(.+):\d/).join}.log" end if Thread.current.name.to_s != "" log_filename.gsub!(/\.log$/, "_#{Thread.current.name}.log") end unless @log_path.to_s == "" log_filename.gsub!(Dir.pwd, ".") dpath = @log_path.split("/") dfile = log_filename.split("/") log_filenamepath = "" dfile.each_with_index do |d, i| if d == dpath[i] log_filenamepath << "#{d}/" else log_filename = @log_path + "#{log_filename.gsub(/^#{log_filenamepath}/, "")}" break end end log_filename = "./#{log_filename}" unless log_filename[0..1] == "./" log_filename = ".#{log_filename}" unless log_filename[0] == "." unless File.exist?(log_filename) require "fileutils" FileUtils.mkdir_p(File.dirname(log_filename)) end end if self.class.log_files.key?(log_filename) and File.exist?(log_filename) @logger = self.class.log_files[log_filename] else begin f = File.new(log_filename, "w") f.sync = true @logger = Logger.new f rescue Exception => stack @logger = Logger.new nil raise InfoMissing, :log end self.class.log_files[log_filename] = @logger end elsif @log == :screen @logger = Logger.new STDOUT elsif @log == :no @logger = Logger.new nil else raise InfoMissing, :log end @log_file = log_filename @logger.level = Logger::INFO 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, :timeout unless @timeout.is_a?(Integer) or @timeout.nil? 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) raise InfoMissing, :log_headers unless [:all, :none, :partial].include?(@log_headers) raise InfoMissing, :async_wait_seconds unless @async_wait_seconds.is_a?(Integer) or @async_wait_seconds.nil? raise InfoMissing, :async_header unless @async_header.is_a?(String) or @async_header.nil? raise InfoMissing, :async_completed unless @async_completed.is_a?(String) or @async_completed.nil? raise InfoMissing, :async_resource unless @async_resource.is_a?(String) or @async_resource.nil? raise InfoMissing, :async_status unless @async_status.is_a?(String) or @async_status.nil? raise InfoMissing, :connection_retry_attempts unless @connection_retry_attempts.is_a?(Integer) && @connection_retry_attempts >= 1 raise InfoMissing, :connection_retry_base_delay unless @connection_retry_base_delay.is_a?(Numeric) && @connection_retry_base_delay >= 0 begin attempt = 1 loop do 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 unless @timeout.nil? @http.open_timeout = @timeout @http.read_timeout = @timeout end @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 unless @timeout.nil? @http.open_timeout = @timeout @http.read_timeout = @timeout end @http.start end break rescue Exception => e if connection_retryable_error?(e) && attempt < @connection_retry_attempts delay = @connection_retry_base_delay + rand(0.25) @logger.warn "(#{self.object_id}): Too many open files (attempt #{attempt}/#{@connection_retry_attempts}), retrying in #{delay.round(2)}s..." sleep(delay) attempt += 1 else raise end end end = "(#{self.object_id}):" = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, timeout:#{@timeout}, 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 # for the case we have headers following nice_hash implementation @headers_orig = @headers.dup @headers = if @headers.is_a?(Hash) && @headers.respond_to?(:generate) @headers.generate else @headers.dup end self.class.active += 1 self.class.connections.push(self) rescue Exception => stack puts stack @logger.fatal stack raise stack end end |
Class Attribute Details
.active ⇒ Object
Returns the value of attribute active.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def active @active end |
.async_completed ⇒ Object
Returns the value of attribute async_completed.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def async_completed @async_completed end |
.async_header ⇒ Object
Returns the value of attribute async_header.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def async_header @async_header end |
.async_resource ⇒ Object
Returns the value of attribute async_resource.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def async_resource @async_resource end |
.async_status ⇒ Object
Returns the value of attribute async_status.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def async_status @async_status end |
.async_wait_seconds ⇒ Object
Returns the value of attribute async_wait_seconds.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def async_wait_seconds @async_wait_seconds end |
.auto_redirect ⇒ Object
Returns the value of attribute auto_redirect.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def auto_redirect @auto_redirect end |
.capture ⇒ Object
Returns the value of attribute capture.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def capture @capture end |
.captured ⇒ Object
Returns the value of attribute captured.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def captured @captured end |
.connection_retry_attempts ⇒ Object
Returns the value of attribute connection_retry_attempts.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def connection_retry_attempts @connection_retry_attempts end |
.connection_retry_base_delay ⇒ Object
Returns the value of attribute connection_retry_base_delay.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def connection_retry_base_delay @connection_retry_base_delay end |
.connections ⇒ Object
Returns the value of attribute connections.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def connections @connections end |
.create_stats ⇒ Object
Returns the value of attribute create_stats.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def create_stats @create_stats end |
.debug ⇒ Object
Returns the value of attribute debug.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def debug @debug end |
.headers ⇒ Object
Returns the value of attribute headers.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def headers @headers end |
.host ⇒ Object
Returns the value of attribute host.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def host @host end |
.last_request ⇒ Object
Returns the value of attribute last_request.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def last_request @last_request end |
.last_response ⇒ Object
Returns the value of attribute last_response.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def last_response @last_response end |
.log ⇒ Object
Returns the value of attribute log.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def log @log end |
.log_files ⇒ Object
Returns the value of attribute log_files.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def log_files @log_files end |
.log_headers ⇒ Object
Returns the value of attribute log_headers.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def log_headers @log_headers end |
.log_path ⇒ Object
Returns the value of attribute log_path.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def log_path @log_path end |
.port ⇒ Object
Returns the value of attribute port.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def port @port end |
.proxy_host ⇒ Object
Returns the value of attribute proxy_host.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def proxy_host @proxy_host end |
.proxy_port ⇒ Object
Returns the value of attribute proxy_port.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def proxy_port @proxy_port end |
.request ⇒ Object
Returns the value of attribute request.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def request @request end |
.request_id ⇒ Object
Returns the value of attribute request_id.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def request_id @request_id end |
.requests ⇒ Object
Returns the value of attribute requests.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def requests @requests end |
.ssl ⇒ Object
Returns the value of attribute ssl.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def ssl @ssl end |
.stats ⇒ Object
Returns the value of attribute stats.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def stats @stats end |
.timeout ⇒ Object
Returns the value of attribute timeout.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def timeout @timeout end |
.use_mocks ⇒ Object
Returns the value of attribute use_mocks.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def use_mocks @use_mocks end |
.values_for ⇒ Object
Returns the value of attribute values_for.
103 104 105 |
# File 'lib/nice_http.rb', line 103 def values_for @values_for end |
Instance Attribute Details
#active ⇒ Integer
Number of active connections
82 83 84 |
# File 'lib/nice_http.rb', line 82 def active @active end |
#async_completed ⇒ String
The value of the async_header to check if the async request is completed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def async_completed @async_completed end |
#async_header ⇒ String
The header to check if the async request is completed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def async_header @async_header end |
#async_resource ⇒ String
The resource to check if the async request is completed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def async_resource @async_resource end |
#async_status ⇒ String
The status to check if the async request is completed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def async_status @async_status end |
#async_wait_seconds ⇒ Integer
Number of seconds to wait until the async request is completed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def async_wait_seconds @async_wait_seconds end |
#auto_redirect ⇒ Boolean
If true, NiceHttp will take care of the auto redirections when required by the responses
82 83 84 |
# File 'lib/nice_http.rb', line 82 def auto_redirect @auto_redirect end |
#capture ⇒ Boolean
If true, NiceHttp will store all requests and responses on NiceHttp.captured as strings
82 83 84 |
# File 'lib/nice_http.rb', line 82 def capture @capture end |
#captured ⇒ Array
It contains all the http requests and responses if NiceHttp.capture is set to true
82 83 84 |
# File 'lib/nice_http.rb', line 82 def captured @captured end |
#connections ⇒ Array
It will include all the active connections (NiceHttp instances)
82 83 84 |
# File 'lib/nice_http.rb', line 82 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" } }
82 83 84 |
# File 'lib/nice_http.rb', line 82 def end |
#create_stats ⇒ Boolean
If true, NiceHttp will create stats of the http communication and store them on NiceHttp.stats hash
82 83 84 |
# File 'lib/nice_http.rb', line 82 def create_stats @create_stats end |
#debug ⇒ Boolean
In case true shows all the details of the communication with the host
82 83 84 |
# File 'lib/nice_http.rb', line 82 def debug @debug end |
#headers ⇒ Hash
Contains the headers you will be using on your connection
82 83 84 |
# File 'lib/nice_http.rb', line 82 def headers @headers end |
#host ⇒ String
The host to be accessed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def host @host end |
#last_request ⇒ String
The last request with all the content sent
82 83 84 |
# File 'lib/nice_http.rb', line 82 def last_request @last_request end |
#last_response ⇒ String
Only in case :debug is true, the last response with all the content
82 83 84 |
# File 'lib/nice_http.rb', line 82 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. :file_run, will generate a log file with the name where the object was created and extension .log, fex: myfile.rb.log String the path and file name where the logs will be stored.
82 83 84 |
# File 'lib/nice_http.rb', line 82 def log @log end |
#log_file ⇒ String
path and file name where the logs will be stored. (only reader)
82 83 84 |
# File 'lib/nice_http.rb', line 82 def log_file @log_file end |
#log_headers ⇒ Object
Returns the value of attribute log_headers.
119 120 121 |
# File 'lib/nice_http.rb', line 119 def log_headers @log_headers end |
#log_headers. ⇒ Symbol
:all, :partial, :none (default :all) If :all will log all the headers. If :partial will log the last 10 characters. If :none no headers.
82 83 84 |
# File 'lib/nice_http.rb', line 82 def log_headers. @log_headers. end |
#log_path ⇒ String
The path where the logs will be stored. By default empty string.
82 83 84 |
# File 'lib/nice_http.rb', line 82 def log_path @log_path 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"
82 83 84 |
# File 'lib/nice_http.rb', line 82 def logger @logger end |
#num_redirects ⇒ Integer
Number of consecutive redirections managed
82 83 84 |
# File 'lib/nice_http.rb', line 82 def num_redirects @num_redirects end |
#port ⇒ Integer
The port number
82 83 84 |
# File 'lib/nice_http.rb', line 82 def port @port end |
#proxy_host ⇒ String
the proxy host to be used
82 83 84 |
# File 'lib/nice_http.rb', line 82 def proxy_host @proxy_host end |
#proxy_port ⇒ Integer
the proxy port to be used
82 83 84 |
# File 'lib/nice_http.rb', line 82 def proxy_port @proxy_port end |
#request ⇒ Hash
The last request with all the content sent
82 83 84 |
# File 'lib/nice_http.rb', line 82 def request @request end |
#request_id ⇒ String
If the response includes a requestId, will be stored here
82 83 84 |
# File 'lib/nice_http.rb', line 82 def request_id @request_id end |
#requests ⇒ Hash
The defaults for all requests. keys: :headers and :data
82 83 84 |
# File 'lib/nice_http.rb', line 82 def requests @requests end |
#response ⇒ Hash
Contains the full response hash
82 83 84 |
# File 'lib/nice_http.rb', line 82 def response @response end |
#ssl ⇒ Boolean
If you use ssl or not
82 83 84 |
# File 'lib/nice_http.rb', line 82 def ssl @ssl end |
#stats ⇒ Hash
It contains detailed stats of the http communication
82 83 84 |
# File 'lib/nice_http.rb', line 82 def stats @stats end |
#timeout ⇒ Integer
Max time to wait until connected to the host or getting a response.
82 83 84 |
# File 'lib/nice_http.rb', line 82 def timeout @timeout end |
#use_mocks ⇒ Boolean
If true, in case the request hash includes a :mock_response key, it will be used as the response instead
82 83 84 |
# File 'lib/nice_http.rb', line 82 def use_mocks @use_mocks end |
#values_for ⇒ Hash
The default values to set on the data in case not specified others
82 83 84 |
# File 'lib/nice_http.rb', line 82 def values_for @values_for end |
Class Method Details
.add_stats(name, state, started, finished, item = nil) ⇒ Object
To add specific stats The stats will be added to NiceHttp.stats[:specific]
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/nice_http/add_stats.rb', line 22 def self.add_stats(name, state, started, finished, item = nil) self.stats[:specific] ||= {} self.stats[:specific][name] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 100000, average: 0 } } self.stats[:specific][name][:num] += 1 if started < self.stats[:specific][name][:finished] self.stats[:specific][name][:real_time_elapsed] += (finished - self.stats[:specific][name][:finished]) else self.stats[:specific][name][:real_time_elapsed] += (finished - started) end self.stats[:specific][name][:finished] = finished time_elapsed = self.stats[:specific][name][:time_elapsed] time_elapsed[:total] += finished - started if time_elapsed[:maximum] < (finished - started) time_elapsed[:maximum] = (finished - started) if !item.nil? time_elapsed[:item_maximum] = item elsif Thread.current.name.to_s != "" time_elapsed[:item_maximum] = Thread.current.name end end if time_elapsed[:minimum] > (finished - started) time_elapsed[:minimum] = (finished - started) if !item.nil? time_elapsed[:item_minimum] = item elsif Thread.current.name.to_s != "" time_elapsed[:item_minimum] = Thread.current.name end end time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][:num] self.stats[:specific][name][state] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 1000, average: 0 }, items: [] } self.stats[:specific][name][state][:num] += 1 if started < self.stats[:specific][name][state][:finished] self.stats[:specific][name][state][:real_time_elapsed] += (finished - self.stats[:specific][name][state][:finished]) else self.stats[:specific][name][state][:real_time_elapsed] += (finished - started) end self.stats[:specific][name][state][:finished] = finished self.stats[:specific][name][state][:items] << item unless item.nil? or self.stats[:specific][name][state][:items].include?(item) time_elapsed = self.stats[:specific][name][state][:time_elapsed] time_elapsed[:total] += finished - started if time_elapsed[:maximum] < (finished - started) time_elapsed[:maximum] = (finished - started) if !item.nil? time_elapsed[:item_maximum] = item elsif Thread.current.name.to_s != "" time_elapsed[:item_maximum] = Thread.current.name end end if time_elapsed[:minimum] > (finished - started) time_elapsed[:minimum] = (finished - started) if !item.nil? time_elapsed[:item_minimum] = item elsif Thread.current.name.to_s != "" time_elapsed[:item_minimum] = Thread.current.name end end time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][state][:num] end |
.defaults=(par = {}) ⇒ Object
Change the default values for NiceHttp supplying a Hash
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nice_http/defaults.rb', line 7 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) @timeout = par[:timeout] if par.key?(:timeout) @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_path = par[:log_path] if par.key?(:log_path) @log = par[:log] if par.key?(:log) @log_headers = par[:log_headers] if par.key?(:log_headers) @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) @create_stats = par[:create_stats] if par.key?(:create_stats) @capture = par[:capture] if par.key?(:capture) @async_wait_seconds = par[:async_wait_seconds] if par.key?(:async_wait_seconds) @async_header = par[:async_header] if par.key?(:async_header) @async_completed = par[:async_completed] if par.key?(:async_completed) @async_resource = par[:async_resource] if par.key?(:async_resource) @async_status = par[:async_status] if par.key?(:async_status) end |
.inherited(subclass) ⇒ Object
If inheriting from NiceHttp class
5 6 7 |
# File 'lib/nice_http/inherited.rb', line 5 def self.inherited(subclass) subclass.reset! end |
.reset! ⇒ Object
to reset to the original defaults
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nice_http/reset.rb', line 5 def self.reset! @host = nil @port = 80 @ssl = false @timeout = nil @headers = {} @values_for = {} @debug = false @log = :fix_file @log_path = "" @log_headers = :all @proxy_host = nil @proxy_port = nil @last_request = nil @request = nil @requests = nil @last_response = nil @request_id = "" @use_mocks = false @connections = [] @active = 0 @auto_redirect = true @log_files = {} @create_stats = false @stats = { all: { num_requests: 0, started: nil, finished: nil, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 1000000, average: 0, }, method: {}, }, path: {}, name: {}, } @capture = false @captured = [] @async_wait_seconds = 0 @async_header = "location" @async_completed = "" @async_resource = "" @async_status = "" @connection_retry_attempts = 3 @connection_retry_base_delay = 1.0 end |
.save_stats(file_name = "") ⇒ Object
It will save the NiceHttp.stats on different files, each key of the hash in a different file.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nice_http/save_stats.rb', line 15 def self.save_stats(file_name = "") if file_name == "" if self.log.is_a?(String) file_name = self.log else file_name = "./#{self.log_path}nice_http.log" end end require "fileutils" FileUtils.mkdir_p File.dirname(file_name) if file_name.match?(/\.json$/) require "json" self.stats.keys.each do |key| File.open("#{file_name.gsub(/.json$/, "_stats_")}#{key}.json", "w") { |file| file.write(self.stats[key].to_json) } end else require "yaml" self.stats.keys.each do |key| File.open("#{file_name.gsub(/.\w+$/, "_stats_")}#{key}.yaml", "w") { |file| file.write(self.stats[key].to_yaml) } end end end |
.validate_response(resp, expected_structure, options = {}) ⇒ Hash
Validates that a response body matches an expected structure (keys and nesting). Uses NiceHash.compare_structure. Best for JSON responses.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/nice_http/validate_response.rb', line 18 def self.validate_response(resp, expected_structure, = {}) data = resp.is_a?(Hash) ? resp[:data] : resp.data parsed = if data.is_a?(String) begin require "json" JSON.parse(data.to_s) rescue JSON::ParserError return { ok: false, error: "response data is not valid JSON" } end elsif data.is_a?(Hash) || data.is_a?(Array) data else return { ok: false, error: "response data is missing or not JSON" } end # Normalize to symbol keys so compare_structure/diff match typical expected_structure (symbol keys) parsed = deep_symbolize(parsed) if parsed.is_a?(Hash) || parsed.is_a?(Array) ok = NiceHash.compare_structure(expected_structure, parsed) result = { ok: ok } if !ok && [:include_diff] result[:diff] = NiceHash.diff(expected_structure, parsed) end result end |
Instance Method Details
#close ⇒ Object
Close HTTP connection
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/nice_http/close.rb', line 5 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 |