Method: Mudbug.process
- Defined in:
- lib/mudbug.rb
.process(resp, accept = nil) ⇒ Object
do stuff based on response’s Content-type accept is e.g. [:json, :html]
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 |
# File 'lib/mudbug.rb', line 51 def self.process(resp, accept = nil) @lager.debug { "accept: #{accept}" } @lager.debug { "response code: #{resp.code}" } @lager.debug { "response headers:\n" << resp.raw_headers.inspect } unless (200..299).include?(resp.code) @lager.warn { "processing with HTTP Status Code #{resp.code}" } end # do you even Content-type, bro? ct = resp.headers[:content_type] unless ct @lager.warn { "abandon processing -- no response Content-type" } return resp.body end # get the content-type ct, charset = ct.split(';').map { |s| s.strip } @lager.info { "got charset: #{charset}; ignoring" } if charset # raise if we got Content-type we didn't ask for if accept and !accept.include?(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] } @lager.warn { "abandon processing -- unrecognized Content-type: #{ct}" } return resp.body end |