module Susu
Constants
- LOG_PREFIX
- VERSION
Public Class Methods
log(msg)
click to toggle source
# File lib/susu/susu.rb, line 11 def log(msg) Rails.logger.debug(msg) Rails.logger.flush end
log_all(method, uri, status, duration, post_params, response, headers, encoding, content_type)
click to toggle source
# File lib/susu/susu.rb, line 19 def log_all(method, uri, status, duration, post_params, response, headers, encoding, content_type) status = Rack::Utils.status_code(status) unless status == /\d{3}/ duration = duration.to_f.round(6) method = method.to_s.upcase response_body = parse_body(response, encoding, content_type) post_params = utf_encoded(post_params.to_s.dup) response_body = '' if [500, '500', 404, '404'].include?(status) # log("#{method.to_s.upcase} #{uri} completed with status code #{status} in #{seconds} seconds") log("\n#{LOG_PREFIX}method=#{method} path=#{uri} status=#{status} duration=#{duration} post_params=#{post_params} response=#{response_body}") end
log_connection(host, port = nil)
click to toggle source
# File lib/susu/susu.rb, line 16 def log_connection(host, port = nil) end
Private Class Methods
log_data_lines(data)
click to toggle source
# File lib/susu/susu.rb, line 70 def log_data_lines(data) data.each_line.with_index do |line, row| log("#{row + 1}: #{line.chomp}") end end
parse_body(body, encoding = nil, content_type=nil)
click to toggle source
# File lib/susu/susu.rb, line 33 def parse_body(body, encoding = nil, content_type=nil) unless text_based?(content_type) # log("Response: (not showing binary data)") return "Response: (not showing binary data)" end if body.is_a?(Net::ReadAdapter) # open-uri wraps the response in a Net::ReadAdapter that defers reading # the content, so the reponse body is not available here. # log("Response: (not available yet)") return "Response: (not available yet)" end if encoding =~ /gzip/ && body && !body.empty? sio = StringIO.new( body.to_s ) gz = Zlib::GzipReader.new( sio ) body = gz.read end data = utf_encoded(body.to_s, content_type) return data end
text_based?(content_type)
click to toggle source
# File lib/susu/susu.rb, line 62 def text_based?(content_type) # This is a very naive way of determining if the content type is text-based; but # it will allow application/json and the like without having to resort to more # heavy-handed checks. content_type =~ /^text/ || content_type =~ /^application/ && content_type != 'application/octet-stream' end
utf_encoded(data, content_type=nil)
click to toggle source
# File lib/susu/susu.rb, line 56 def utf_encoded(data, content_type=nil) charset = content_type.to_s.scan(/; charset=(\S+)/).flatten.first || 'UTF-8' data.force_encoding(charset) rescue data.force_encoding('UTF-8') data.encode('UTF-8', :invalid => :replace, :undef => :replace) end