lib/mudbug.rb in mudbug-0.4.6.6 vs lib/mudbug.rb in mudbug-0.4.6.7
- old
+ new
@@ -1,5 +1,6 @@
+require 'logger'
require 'rest-client'
require 'json'
class Mudbug
def self.version
@@ -26,10 +27,47 @@
type: 'text/plain',
proc: proc { |text| text },
},
}
+ def self.log_to dest=nil
+ case dest
+ when nil, 'stderr', 'STDERR'
+ dest = $stderr
+ when 'stdout', 'STDOUT'
+ dest = $stdout
+ when IO
+ # do nothing
+ when String
+ # assume file path, do nothing
+ else
+ raise "unable to log_to #{dest} (#{dest.class})"
+ end
+ if defined?(@@log)
+ l = Logger.new dest
+ l.formatter = @@log.formatter
+ l.level = @@log.level
+ @@log = l
+ else
+ @@log = Logger.new dest
+ @@log.formatter = proc { |sev, time, progname, msg|
+ line = "[#{time.strftime('%Y-%m-%d %H:%M:%S')}] #{sev.to_s.upcase}: "
+ line << "(#{progname}) " if progname
+ line << msg << "\n"
+ }
+ @@log.level = Logger::WARN
+ end
+ @@log
+ end
+
+ def self.log_level=(sym)
+ log_to unless defined?(@@log)
+ level = Logger.const_get(sym.to_s.upcase)
+ raise "unknown log level #{sym}" unless level
+ @@log.level = level
+ end
+
# map our internal symbols to HTTP content types
# assign q scores based on the parameter order
# construct the right side of the Accept: header
#
def self.accept_header(*types)
@@ -41,43 +79,49 @@
end
# do stuff based on response's Content-type
#
def self.process(resp, accept = nil)
- case resp.code
- when 200..299
- # 2xx, OK
- else
- warn "proceeding with HTTP Status Code #{resp.code}"
+ log_to unless defined?(@@log)
+
+ @@log.debug { "response code: #{resp.code}" }
+ @@log.debug { "response headers:\n" << resp.raw_headers.inspect }
+
+ unless (200..299).include?(resp.code)
+ @@log.warn { "processing with HTTP Status Code #{resp.code}" }
end
# do you even Content-type, bro?
ct = resp.headers[:content_type]
unless ct
- warn "process NOOP -- no response Content-type"
+ @@log.warn { "abort processing -- no response Content-type" }
return resp.body
end
# warn if we got Content-type we didn't ask for
ct, charset = ct.split(';').map { |s| s.strip }
- warn "Asked for #{accept} but got #{ct}" if accept and !accept.include?(ct)
+ if accept and !accept.include?(ct)
+ @@log.warn { "Asked for #{accept} but got #{ct}" }
+ end
# process the response for known content types
CONTENT.each { |sym, hsh|
return hsh[:proc].call(resp.body) if ct == hsh[:type]
}
- warn "process NOOP -- unrecognized Content-type: #{ct}"
- return response.body
+ @@log.warn { "abort processing -- unrecognized Content-type: #{ct}" }
+ return resp.body
end
+
attr_reader :options
attr_accessor :host
- def initialize(host, options = nil)
+ def initialize(host, options = {})
+ self.class.log_to options.delete :log_to
@host = host
- @options = options || {}
+ @options = options
accept :json, :html, :text
end
# Writes the Accept: header for you
# e.g.
@@ -96,13 +140,10 @@
# use this method directly if you want finer-grained request and response
# handling
#
def resource(path, options = {})
path = "/#{path}" unless path[0,1] == '/'
- url = "http://#{@host}#{path}"
- options = @options.merge(options)
-
- RestClient::Resource.new(url, options)
+ RestClient::Resource.new "http://#{@host}#{path}", @options.merge(options)
end
# no payload
#
[:get, :delete].each { |meth|