Permalink
Browse files

Arrays and keys bind in the way expected

from the response.
  • Loading branch information...
1 parent 3e78460 commit 287b923fb9dbeead140ce7c48cb9e8a6e0c0fcdd @stewartmatheson committed Feb 16, 2012
Showing with 47 additions and 5 deletions.
  1. +13 −2 lib/response.rb
  2. +31 −0 spec/response_spec.rb
  3. +3 −3 spec/urbanairship_spec.rb
View
@@ -4,8 +4,7 @@ def initialize(response)
@response = response
if @response
if success? && !body.empty?
- json = JSON.parse(body).first
- json.keys.each{|k| self[k] = json[k]}
+ parse_body(body)
end
else
@timeout = true
@@ -28,4 +27,16 @@ def request_timeout?
@timeout
end
+ private
+
+ def parse_body(body)
+ json = JSON.parse(body)
+ if json.class == Array
+ json.each_with_index do |item, index|
+ self[index] = item
+ end
+ else
+ json.keys.each{|k| self[k] = json[k]}
+ end
+ end
end
View
@@ -64,4 +64,35 @@
# TODO : Test this...
end
end
+
+ context "body array accessor" do
+ subject { Urbanairship.feedback(Time.now) }
+
+ before do
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "[{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"},{\"device_token\":\"token2\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}]")
+ Urbanairship.application_secret = "my_app_secret"
+ Urbanairship.application_key = "my_app_key"
+ Urbanairship.master_secret = "my_master_secret"
+ end
+
+ it "should set up correct indexes" do
+ subject[0]['device_token'].should eql "token"
+ subject[1]['device_token'].should eql "token2"
+ end
+ end
+
+ context "non array response" do
+ subject { Urbanairship.feedback(Time.now) }
+
+ before do
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}")
+ Urbanairship.application_secret = "my_app_secret"
+ Urbanairship.application_key = "my_app_key"
+ Urbanairship.master_secret = "my_master_secret"
+ end
+
+ it "should set up correct keys" do
+ subject['device_token'].should eql "token"
+ end
+ end
end
@@ -402,9 +402,9 @@
it "returns an array of responses from the feedback API" do
response = Urbanairship.feedback(Time.now)
response.class.should == Urbanairship::Response
- response.should include("device_token")
- response.should include("marked_inactive_on")
- response.should include("alias")
+ response[0].should include("device_token")
+ response[0].should include("marked_inactive_on")
+ response[0].should include("alias")
end
it "returns false and doesn't parse JSON when the call doesn't return 200" do

0 comments on commit 287b923

Please sign in to comment.