Skip to content

Commit

Permalink
better testing and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
adelevie committed Feb 20, 2012
1 parent 5976e01 commit 94aa6a0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
13 changes: 10 additions & 3 deletions lib/parse/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lib/parse/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
19 changes: 11 additions & 8 deletions lib/parse/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Binary file modified pkg/parse-ruby-client-0.0.2.gem
Binary file not shown.
29 changes: 27 additions & 2 deletions test/test_client.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions test/test_init.rb
Original file line number Diff line number Diff line change
@@ -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.