Permalink
Browse files

better testing and error handling

  • Loading branch information...
1 parent 5976e01 commit 94aa6a0ed719d7b34de29f20aa03f7507433eb9c @adelevie committed Feb 20, 2012
Showing with 80 additions and 20 deletions.
  1. +10 −3 lib/parse/client.rb
  2. +9 −7 lib/parse/error.rb
  3. +11 −8 lib/parse/object.rb
  4. BIN pkg/parse-ruby-client-0.0.2.gem
  5. +27 −2 test/test_client.rb
  6. +23 −0 test/test_init.rb
View
@@ -18,6 +18,7 @@ def initialize(data = {})
@application_id = data[:application_id]
@api_key = data[:api_key]
@session = Patron::Session.new
+ @session.timeout = 10
@session.base_url = "https://#{host}"
@session.headers["Content-Type"] = "application/json"
@@ -42,9 +43,9 @@ def request(uri, method = :get, body = nil, query = nil)
response = @session.request(method, uri, {}, options)
if response.status >= 400
- raise ParseProtocolError, response
+ raise ParseError, "#{JSON.parse(response.body)['code']}: #{JSON.parse(response.body)['error']}"
else
- if response.body
+ if response
return JSON.parse response.body
end
end
@@ -79,9 +80,15 @@ def delete(uri)
# Initialize the singleton instance of Client which is used
# by all API methods. Parse.init must be called before saving
# or retrieving any objects.
- def Parse.init(data = {:application_id => $PARSE_APPLICATION_ID, :api_key => $PARSE_REST_API_KEY})
+ def Parse.init(data = {:application_id => ENV["PARSE_APPLICATION_ID"], :api_key => ENV["PARSE_REST_API_KEY"]})
@@client = Client.new(data)
end
+
+ # Used mostly for testing. Lets you delete the api key global vars.
+ def Parse.destroy
+ @@client = nil
+ self
+ end
def Parse.client
if !@@client
View
@@ -3,7 +3,7 @@ module Parse
# Base exception class for errors thrown by the Parse
# client library. ParseError will be raised by any
# network operation if Parse.init() has not been called.
- class ParseError < Exception
+ class ParseError < StandardError #Exception ... why? A:http://www.skorks.com/2009/09/ruby-exceptions-and-exception-handling/
end
# An exception class raised when the REST API returns an error.
@@ -15,12 +15,14 @@ class ParseProtocolError < ParseError
attr_accessor :response
def initialize(response)
- @response = response
- if response.body
- data = JSON.parse response.body
- @code = data["code"]
- @message = data["error"]
- end
+ #@response = response
+ #if response.body
+ # data = JSON.parse response.body
+ # @code = data["code"]
+ # @message = data["error"]
+ #end
+
+ #{}"#{@code}: #{@message}"
end
end
View
@@ -45,14 +45,16 @@ def parse(data)
@updated_at = DateTime.parse data[Protocol::KEY_UPDATED_AT]
end
- data.each { |k,v|
- if k.is_a? Symbol
- k = k.to_s
- end
- if !Protocol::RESERVED_KEYS.include? k
- self[k] = v
- end
- }
+ self.merge! data
+
+ #data.each { |k,v|
+ # if k.is_a? Symbol
+ # k = k.to_s
+ # end
+ # if !Protocol::RESERVED_KEYS.include? k
+ # self[k] = v
+ # end
+ #}
end
private :parse
@@ -61,6 +63,7 @@ def parse(data)
# a new object, otherwise it will update the existing stored object.
def save
method = @parse_object_id ? :put : :post
+ Protocol::RESERVED_KEYS.each { |k| self.delete(k) }
body = self.to_json
data = Parse.client.request(self.uri, method, body)
Binary file not shown.
View
@@ -1,7 +1,32 @@
require 'helper'
class TestClient < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ def setup
+ Parse.init
+ end
+
+ def test_simple_save
+ test_save = Parse::Object.new "TestSave"
+ test_save["foo"] = "bar"
+ test_save.save
+
+ assert_equal test_save["foo"], "bar"
+ assert_equal test_save[Parse::Protocol::KEY_CREATED_AT].class, String
+ assert_equal test_save[Parse::Protocol::KEY_OBJECT_ID].class, String
+ end
+
+ def test_update
+ foo = Parse::Object.new "TestSave"
+ foo["age"] = 20
+ foo.save
+
+ assert_equal foo["age"], 20
+ assert_equal foo[Parse::Protocol::KEY_UPDATED_AT], nil
+
+ foo["age"] = 40
+ foo.save
+
+ assert_equal foo["age"], 40
+ assert_equal foo[Parse::Protocol::KEY_UPDATED_AT].class, String
end
end
View
@@ -0,0 +1,23 @@
+require 'helper'
+
+#Parse.init :application_id => $PARSE_APPLICATION_ID, :api_key => $PARSE_REST_API_KEY
+
+class TestInit < Test::Unit::TestCase
+ def setup
+ Parse.destroy
+ end
+
+ def test_no_api_keys_error
+ fake = Parse::Object.new "shouldNeverExist"
+ fake["foo"] = "bar"
+
+ begin
+ fake.save
+ rescue
+ error_triggered = true
+ end
+
+ assert_equal error_triggered, true
+ assert_equal fake[Parse::Protocol::KEY_OBJECT_ID], nil
+ end
+end

0 comments on commit 94aa6a0

Please sign in to comment.