lib/totter/client.rb in totter-0.2.2 vs lib/totter/client.rb in totter-0.2.3

- old
+ new

@@ -1,10 +1,11 @@ require 'net/http' require 'net/https' require 'uri' require 'multi_json' require 'hashie' +require 'addressable/uri' require 'totter/client/users' require 'totter/client/decisions' require 'totter/client/slugs' require 'totter/client/timelines' @@ -72,20 +73,27 @@ @http.use_ssl = self.ssl? @http end def request(method, path, params = nil) + uri = Addressable::URI.parse("#{self.base_url}#{path}") + + # if the request requires parameters in the query string, merge them in + if params and !can_post_data?(method) + uri.query_values = (uri.query_values || {}).merge(params) + end + # Build request - request = build_request(method, URI.parse("#{self.base_url}#{path}")) + request = build_request(method, uri) # Add headers request['Authorization'] = "Bearer #{self.access_token}" if authenticated? request['X-Seesaw-Client-Token'] = @client_token if @client_token request['Content-Type'] = 'application/json' # Add params as JSON if they exist - request.body = MultiJson.dump(params) if [:post, :put].include?(method) and params + request.body = MultiJson.dump(params) if can_post_data?(method) and params # Request response = http.request(request) # Check for errors @@ -141,9 +149,13 @@ end def boolean_from_response(*args) response = request(*args) (200..299).include? response.code.to_i + end + + def can_post_data?(method) + [:post, :put].include?(method) end [:get, :post, :put, :delete].each do |method| define_method method do |*args| json_request(method, *args)