Skip to content

Commit

Permalink
add response class
Browse files Browse the repository at this point in the history
  • Loading branch information
aklaiber committed Jun 22, 2014
1 parent 0078350 commit 64dd7a8
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/rgcm/response.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
module Rgcm
class Response
attr_reader :body

def initialize(json)
@body = JSON.parse(json, symbolize_names: true)
end

def count_successes
self.body[:success]
end

def has_successes?
self.count_successes > 0
end

def count_failures
self.body[:failure]
end

def has_failures?
self.count_failures > 0
end

def results
self.body[:results]
end

def errors
self.results.select { |result| result.key?(:error) }
end

def successes
self.results.select { |result| !result.key?(:error) }
end

end
end
9 changes: 9 additions & 0 deletions spec/fixtures/responses/failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"multicast_id":5030383832068975557,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{ "error":"NotRegistered" }
]
}
25 changes: 25 additions & 0 deletions spec/fixtures/responses/multi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"multicast_id":5958304298192430760,
"success":3,
"failure":11,
"canonical_ids":1,
"results":[
{ "message_id":"0:1403426982650227%62dfd0f6e116c072"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{
"message_id":"0:1403426982651391%b930435be116c072",
"registration_id":"APA91bFgaPSr9eX0VKsgFT15bg8umQBftGGOVEsKjBm9KtLfU5pz1ytEZkCm7YKjXbpavurXSqMdh1Sviry4ZoXZIZmzLTDI2ti6mxFjuY4tNom5uO8VcvH2EjPu2mwFuZyfQomJpRmVsraCJWpyOzms5WMgrYIdQXb08UvOKKe72ByOcClxlAo"
},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "error":"NotRegistered"},
{ "message_id":"0:1403426982652703%b930435be116c072"}
]
}
9 changes: 9 additions & 0 deletions spec/fixtures/responses/successfully.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"multicast_id":5030383832068975557,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{ "message_id":"0:1403425286258916%62dfd0f6e116c072" }
]
}
57 changes: 57 additions & 0 deletions spec/rgcm/response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
require 'spec_helper'

describe Rgcm::Response do
let(:response) { Rgcm::Response.new(json) }

context 'with successfully response' do
let(:json) { File.open("#{FIXTURES_PATH}/responses/successfully.json").read }
it '#has_successes?' do
expect(response.has_successes?).to be_true
end
it '#has_failures?' do
expect(response.has_failures?).to be_false
end
it '#count_successes' do
expect(response.count_successes).to eql(1)
end
it '#has_failures?' do
expect(response.count_failures).to eql(0)
end
end

context 'with failure response' do
let(:json) { File.open("#{FIXTURES_PATH}/responses/failure.json").read }
it '#has_successes?' do
expect(response.has_successes?).to be_false
end
it '#has_failures?' do
expect(response.has_failures?).to be_true
end
it '#count_successes' do
expect(response.count_successes).to eql(0)
end
it '#has_failures?' do
expect(response.count_failures).to eql(1)
end
it '#errors' do
expect(response.errors.size).to eql(1)
end
end

context 'with failure response' do
let(:json) { File.open("#{FIXTURES_PATH}/responses/multi.json").read }
it '#has_successes?' do
expect(response.has_successes?).to be_true
end
it '#has_failures?' do
expect(response.has_failures?).to be_true
end
it '#count_successes' do
expect(response.count_successes).to eql(3)
end
it '#has_failures?' do
expect(response.count_failures).to eql(11)
end
it '#errors' do
expect(response.errors.size).to eql(11)
end
it '#successes' do
expect(response.successes.size).to eql(3)
end
end
end
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'rgcm'

FIXTURES_PATH = "#{File.dirname(__FILE__)}/fixtures"

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
Expand Down

0 comments on commit 64dd7a8

Please sign in to comment.