lib/yammer/http_adapter.rb in yammer-client-0.1.7 vs lib/yammer/http_adapter.rb in yammer-client-0.1.8

- old
+ new

@@ -3,10 +3,14 @@ require 'addressable/uri' module Yammer class HttpAdapter + def self.log=(output) + RestClient.log = output + end + attr_reader :site_url, :connection_options def initialize(site_url, opts={}) unless site_url =~ /^https?/ raise ArgumentError, "site_url must include either http or https scheme" @@ -39,31 +43,39 @@ raise ArgumentError, 'expected Hash' unless opts.is_a?(Hash) @connection_options = opts end def send_request(method, path, opts={}) - params = opts.fetch(:params, {}) + begin + params = opts.fetch(:params, {}) - req_opts = self.connection_options.merge({ - :method => method, - :headers => opts.fetch(:headers, {}) - }) + req_opts = self.connection_options.merge({ + :method => method, + :headers => opts.fetch(:headers, {}) + }) - case method - when :get, :delete - query = Addressable::URI.form_encode(params) - normalized_path = query.empty? ? path : [path, query].join("?") - req_opts[:url] = absolute_url(normalized_path) - when :post, :put - req_opts[:payload] = params - req_opts[:url] = absolute_url(path) - else - raise "Unsupported HTTP method, #{method}" - end + case method + when :get, :delete + query = Addressable::URI.form_encode(params) + normalized_path = query.empty? ? path : [path, query].join("?") + req_opts[:url] = absolute_url(normalized_path) + when :post, :put + req_opts[:payload] = params + req_opts[:url] = absolute_url(path) + else + raise "Unsupported HTTP method, #{method}" + end - resp = RestClient::Request.execute(req_opts) - - result = Yammer::ApiResponse.new(resp.headers, resp.body, resp.code) + resp = RestClient::Request.execute(req_opts) + + result = Yammer::ApiResponse.new(resp.headers, resp.body, resp.code) + rescue => e + if e.is_a?(RestClient::ExceptionWithResponse) + e.response + else + raise e + end + end end private def parsed_url Addressable::URI.parse(@site_url) \ No newline at end of file