lib/mudbug.rb in mudbug-0.4.7.3 vs lib/mudbug.rb in mudbug-0.5.0.1
- old
+ new
@@ -1,19 +1,20 @@
require 'rest-client'
require 'json'
require 'lager'
class Mudbug
- extend Lager
- log_to $stderr, :warn
-
def self.version
- vpath = File.join(File.dirname(__FILE__), '..', 'VERSION')
- File.read(vpath).chomp
+ file = File.expand_path('../../VERSION', __FILE__)
+ File.read(file).chomp
end
+ extend Lager
+ log_to $stderr, :warn
+
class StatusCodeError < RuntimeError; end
+ class ContentTypeError < RuntimeError; end
# this structure declares what we support in the request Accept: header
# and defines automatic processing of the response based on the
# response Content-type: header
#
@@ -43,10 +44,11 @@
i == 0 ? type : [type, quality].join(';')
}.join(', ')
end
# do stuff based on response's Content-type
+ # accept is e.g. [:json, :html]
#
def self.process(resp, accept = nil)
@lager.debug { "response code: #{resp.code}" }
@lager.debug { "response headers:\n" << resp.raw_headers.inspect }
@@ -55,17 +57,19 @@
end
# do you even Content-type, bro?
ct = resp.headers[:content_type]
unless ct
- @lager.warn { "abort processing -- no response Content-type" }
+ @lager.warn { "abandon processing -- no response Content-type" }
return resp.body
end
- # warn if we got Content-type we didn't ask for
+ # get the content-type
ct, charset = ct.split(';').map { |s| s.strip }
+
+ # raise if we got Content-type we didn't ask for
if accept and !accept.include?(ct)
- @lager.warn { "Asked for #{accept} but got #{ct}" }
+ raise ContentTypeError, "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]