Skip to content

Commit

Permalink
GeoPoint support
Browse files Browse the repository at this point in the history
  • Loading branch information
adelevie committed Feb 17, 2012
1 parent 2087753 commit 33b6d01
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/parse/datatypes.rb
Expand Up @@ -71,6 +71,9 @@ def to_json(*a)
end
end

# Increment and Decrement
# ------------------------------------------------------------

class Increment
# '{"score": {"__op": "Increment", "amount": 1 } }'
attr_accessor :amount
Expand Down Expand Up @@ -102,5 +105,32 @@ def to_json(*a)
}.to_json(*a)
end
end

# GeoPoint
# ------------------------------------------------------------

class GeoPoint
# '{"location": {"__type":"GeoPoint", "latitude":40.0, "longitude":-30.0}}'
attr_accessor :longitude, :latitude

def initialize(data)
@longitude = data["longitude"]
@latitude = data["latitude"]

if !@longitude && !@latitude
@longitude = data[:longitude]
@latitude = data[:latitude]
end
end

def to_json(*a)
{
Protocol::KEY_TYPE => Protocol::TYPE_GEOPOINT,
"latitude" => @latitude,
"longitude" => @longitude
}.to_json(*a)
end
end


end
15 changes: 15 additions & 0 deletions test/test_datatypes.rb
Expand Up @@ -34,12 +34,27 @@ def test_bytes
def test_increment
amount = 5
increment = Parse::Increment.new amount

assert_equal increment.to_json, "{\"__op\":\"Increment\",\"amount\":#{amount}}"
end

def test_decrement
amount = 5
increment = Parse::Decrement.new amount

assert_equal increment.to_json, "{\"__op\":\"Decrement\",\"amount\":#{amount}}"
end

def test_geopoint
# '{"location": {"__type":"GeoPoint", "latitude":40.0, "longitude":-30.0}}'
data = {
"longitude" => 40.0,
"latitude" => -30.0
}
gp = Parse::GeoPoint.new data

assert_equal JSON.parse(gp.to_json)["longitude"], data["longitude"]
assert_equal JSON.parse(gp.to_json)["latitude"], data["latitude"]
assert_equal JSON.parse(gp.to_json)[Parse::Protocol::KEY_TYPE], Parse::Protocol::TYPE_GEOPOINT
end
end

0 comments on commit 33b6d01

Please sign in to comment.