lib/rufus/verbs/endpoint.rb in rufus-verbs-1.0.0 vs lib/rufus/verbs/endpoint.rb in rufus-verbs-1.0.1
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2008-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2008-2012, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -66,14 +66,23 @@
#
# The endpoint initialization opts (Hash instance)
#
attr_reader :opts
+
+ #
+ # Configure default parsers for this EndPoint, example:
+ # ep.parsers['application/json'] = Yajl::Parser
+ # ep.parsers['application/xhtml+xml'] = Nokogiri::HTML::Document
+ #
+
+ attr_reader :parsers
def initialize (opts)
@opts = opts
+ @parsers = {}
compute_target @opts
@opts[:http_basic_authentication] =
opts[:http_basic_authentication] || opts[:hba]
@@ -268,15 +277,23 @@
opts[:scheme] = r[0] || @opts[:scheme]
opts[:host] = r[1] || @opts[:host]
opts[:port] = r[2] || @opts[:port]
opts[:path] = r[3] || @opts[:path]
-
+
+ # Merge EndPoint params and Request params
+
+ def_params = @opts[:query] || @opts[:params]
+ req_params = opts[:query] || opts[:params]
+
+ mer_params = def_params.merge req_params if def_params && req_params
+
opts[:query] =
r[4] ||
- opts[:params] || opts[:query] ||
- @opts[:query] || @opts[:params] ||
+ mer_params ||
+ req_params ||
+ def_params ||
{}
opts.delete :path if opts[:path] == ''
opts[:c_uri] = [
@@ -567,13 +584,15 @@
return request(method, opts)
#
# following the redirection
end
-
+
decompress res
+ parse_body res
+
res
end
#
# Returns an array of symbols, like for example
@@ -633,9 +652,38 @@
gz = Zlib::GzipReader.new io
res.deflated_body = gz.read
gz.close
end
end
+
+ #
+ # Parses the response body if a parser in defined for it's content-type
+ #
+
+ def parse_body (res)
+
+ content_type = res.header['content-type']
+
+ if content_parser = self.parsers[content_type]
+
+ class << res
+ attr_accessor :parsed_body
+
+ alias :old_body :body
+
+ def body
+ parsed_body || old_body
+ end
+ end
+
+ res.parsed_body = content_parser.send(:parse, res.body)
+
+ end
+
+ res
+
+ end
+
end
end
end