lib/fb_graph/node.rb in fb_graph-1.7.5 vs lib/fb_graph/node.rb in fb_graph-1.8.0.alpha

- old
+ new

@@ -34,58 +34,49 @@ end protected def get(params = {}) - _params_ = stringfy_params(params) - _endpoint_ = build_endpoint(_params_.merge!(:method => :get)) handle_response do - RestClient.get(_endpoint_) + HTTPClient.new.get build_endpoint(params), build_params(params) end end def post(params = {}) - _params_ = stringfy_params(params) - _endpoint_ = build_endpoint(_params_.merge!(:method => :post)) handle_response do - RestClient.post(_endpoint_, _params_) + HTTPClient.new.post build_endpoint(params), build_params(params) end end def delete(params = {}) - _params_ = stringfy_params(params) - _endpoint_ = build_endpoint(_params_.merge!(:method => :delete)) + _endpoint_, _params_ = build_endpoint(params), build_params(params) + _endpoint_ = [_endpoint_, _params_.try(:to_query)].compact.join('?') handle_response do - RestClient.delete(_endpoint_) + HTTPClient.new.delete _endpoint_ end end private def build_endpoint(params = {}) - _endpoint_ = File.join([self.endpoint, params.delete(:connection), params.delete(:connection_scope)].compact.collect(&:to_s)) - params.delete_if do |k, v| - v.blank? - end - if [:get, :delete].include?(params.delete(:method)) && params.present? - _endpoint_ << "?#{params.to_query}" - end - _endpoint_ + File.join([self.endpoint, params.delete(:connection), params.delete(:connection_scope)].compact.collect(&:to_s)) end - def stringfy_params(params) + def build_params(params) _params_ = params.dup - _params_[:access_token] ||= self.access_token - if _params_[:access_token].is_a?(Rack::OAuth2::AccessToken::Legacy) - _params_[:access_token] = _params_[:access_token].access_token + _params_[:oauth_token] = (_params_.delete(:access_token) || self.access_token).to_s + _params_.delete_if do |k, v| + v.blank? end _params_.each do |key, value| if value.present? && ![Symbol, String, Numeric, IO].any? { |klass| value.is_a? klass } _params_[key] = value.to_json + else + _params_[key] = value.to_s end end - _params_ + _params_.blank? ? nil : _params_ end def handle_response response = yield case response.body @@ -101,29 +92,28 @@ raise NotFound.new('Graph API returned false, so probably it means your requested object is not found.') when 'null' nil else _response_ = JSON.parse(response.body) - case _response_ + _response_ = case _response_ when Array _response_.map!(&:with_indifferent_access) when Hash - _response_.with_indifferent_access + _response_ = _response_.with_indifferent_access + handle_httpclient_error(_response_) if _response_[:error] + _response_ end end - rescue RestClient::Exception => e - handle_restclient_error(e) + rescue JSON::ParserError + raise Exception.new(response.status, 'Unparsable Error Response') end - def handle_restclient_error(e) - _response_ = JSON.parse(e.http_body).with_indifferent_access - case _response_[:error][:type] + def handle_httpclient_error(response) + case response[:error][:type] when /OAuth/ - raise Unauthorized.new(e.message, e.http_body) + raise Unauthorized.new("#{response[:error][:type]} :: #{response[:error][:message]}") else - raise BadRequest.new(e.message, e.http_body) + raise BadRequest.new("#{response[:error][:type]} :: #{response[:error][:message]}") end - rescue JSON::ParserError - raise Exception.new(e.http_code, e.message, e.http_body) end end end \ No newline at end of file