Module: Rested::BaseMethods
Instance Method Summary collapse
- #client ⇒ Object
- #decode_json_response(message) ⇒ Object
- #decode_response(message) ⇒ Object
- #decode_xml_response(message) ⇒ Object
- #delete(uri) ⇒ Object
- #get(uri, params = nil) ⇒ Object
- #handle_error(message) ⇒ Object
- #handle_response(message) ⇒ Object
- #post(uri, params = nil) ⇒ Object
-
#setup_client ⇒ Object
override this method to customize HTTPClient.
Instance Method Details
#client ⇒ Object
29 30 31 |
# File 'lib/rested/base.rb', line 29 def client @client ||= setup_client() end |
#decode_json_response(message) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/rested/base.rb', line 74 def decode_json_response() begin JSON.load(.content) rescue => ex nil end end |
#decode_response(message) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rested/base.rb', line 62 def decode_response() ct = .contenttype if ct =~ /json|javascript/ then decode_json_response() elsif ct =~ /xml/ then decode_xml_response() else Rested.log_in { "\n" + .content } raise "Unknown response type" end end |
#decode_xml_response(message) ⇒ Object
82 83 84 |
# File 'lib/rested/base.rb', line 82 def decode_xml_response() raise NotImplementedError # TODO end |
#delete(uri) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/rested/base.rb', line 47 def delete(uri) url = self.base_url + uri Rested.log_out{"DELETE #{url}"} res = self.client.delete(url) handle_error(res) if .status >= 400 return res.status == 200 end |
#get(uri, params = nil) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rested/base.rb', line 33 def get(uri, params = nil) url = self.base_url + uri Rested.log_out{"GET #{url}"} Rested.log_out{" params: " + params.inspect} handle_response(self.client.get(url, params)) end |
#handle_error(message) ⇒ Object
86 87 88 89 90 |
# File 'lib/rested/base.rb', line 86 def handle_error() Rested.log_in{ puts; "HTTP/#{.version} #{.status} #{.reason}"} Rested.log_in{ .header.all.each{|h| Rested.log_in("#{h[0]}: #{h[1]}")}; puts } raise Rested::Error.new() end |
#handle_response(message) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/rested/base.rb', line 55 def handle_response() handle_error() if .status >= 400 Rested.log_in{ puts; "HTTP/#{.version} #{.status} #{.reason}"} Rested.log_in{ .header.all.each{|h| Rested.log_in("#{h[0]}: #{h[1]}")}; puts } decode_response() end |
#post(uri, params = nil) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/rested/base.rb', line 40 def post(uri, params = nil) url = self.base_url + uri Rested.log_out{"POST #{url}"} Rested.log_out{" params: " + params.inspect} handle_response(self.client.post(url, params)) end |
#setup_client ⇒ Object
override this method to customize HTTPClient
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rested/base.rb', line 11 def setup_client @client = ::HTTPClient.new if self.user and self.pass then @client.set_auth(self.base_url, self.user, self.pass) end if not Rested.debug.nil? @client.debug_dev = (Rested.debug.respond_to?("<<") ? Rested.debug : STDOUT) end if Object.const_defined? "Rack" then handler = lambda { |path| path =~ /(\..*?)$/ Rack::Mime.mime_type(($1.nil? ? nil : $1.downcase)) } HTTP::Message.mime_type_handler = handler end @client end |