-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters