Skip to content

Commit c692b8c

Browse files
authored
Merge pull request #104 from pusher/1.2.0-rc3
1.2.0-rc3
2 parents 67e968a + ab048d3 commit c692b8c

File tree

4 files changed

+5
-198
lines changed

4 files changed

+5
-198
lines changed

lib/pusher/native_notification/client.rb

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ class Client
55

66
API_PREFIX = "server_api"
77
API_VERSION = "v1"
8-
GCM_TTL = 241920
9-
RESTRICTED_GCM_PAYLOAD_KEYS = [:to, :registration_ids]
10-
WEBHOOK_LEVELS = ["DEBUG", "INFO"]
118

129
def initialize(app_id, host, scheme, pusher_client)
1310
@app_id = app_id
@@ -45,69 +42,15 @@ def notify(interests, data = {})
4542
# @return [String]
4643
def payload(interests, data)
4744
interests = Array(interests).map(&:to_s)
48-
4945
raise Pusher::Error, "Too many interests provided" if interests.length > 1
50-
51-
data = deep_symbolize_keys!(data)
52-
validate_payload(data)
53-
54-
data.merge!(interests: interests)
55-
46+
data = deep_symbolize_keys!(data).merge(interests: interests)
5647
MultiJson.encode(data)
5748
end
5849

5950
def url(path = nil)
6051
URI.parse("#{@scheme}://#{@host}/#{API_PREFIX}/#{API_VERSION}/apps/#{@app_id}#{path}")
6152
end
6253

63-
# Validate payload
64-
# `time_to_live` -> value b/w 0 and 241920
65-
# If the `notification` key is provided, ensure
66-
# that there is an accompanying `title` and `icon`
67-
# field
68-
def validate_payload(payload)
69-
unless (payload.has_key?(:apns) || payload.has_key?(:gcm))
70-
raise Pusher::Error, "GCM or APNS data must be provided"
71-
end
72-
73-
if (gcm_payload = payload[:gcm])
74-
# Restricted keys
75-
RESTRICTED_GCM_PAYLOAD_KEYS.each { |k| gcm_payload.delete(k) }
76-
if (ttl = gcm_payload[:time_to_live])
77-
78-
if ttl.to_i < 0 || ttl.to_i > GCM_TTL
79-
raise Pusher::Error, "Time to live must be between 0 and 241920 (4 weeks)"
80-
end
81-
end
82-
83-
# If the notification key is provided
84-
# validate the `icon` and `title`keys
85-
if (notification = gcm_payload[:notification])
86-
notification_title, notification_icon = notification.values_at(:title, :icon)
87-
88-
if (!notification_title || notification_title.empty?)
89-
raise Pusher::Error, "Notification title is a required field"
90-
end
91-
92-
if (!notification_icon || notification_icon.empty?)
93-
raise Pusher::Error, "Notification icon is a required field"
94-
end
95-
end
96-
end
97-
98-
if (webhook_url = payload[:webhook_url])
99-
raise Pusher::Error, "Webhook url is invalid" unless webhook_url =~ /\A#{URI::regexp(['http', 'https'])}\z/
100-
end
101-
102-
if (webhook_level = payload[:webhook_level])
103-
raise Pusher::Error, "Webhook level cannot be used without a webhook url" if !payload.has_key?(:webhook_url)
104-
105-
unless WEBHOOK_LEVELS.include?(webhook_level.upcase)
106-
raise Pusher::Error, "Webhook level must either be INFO or DEBUG"
107-
end
108-
end
109-
end
110-
11154
# Symbolize all keys in the hash recursively
11255
def deep_symbolize_keys!(hash)
11356
hash.keys.each do |k|

lib/pusher/request.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def handle_response(status_code, body)
8585
when 200
8686
return symbolize_first_level(MultiJson.decode(body))
8787
when 202
88-
return true
88+
return body.empty? ? true : symbolize_first_level(MultiJson.decode(body))
8989
when 400
9090
raise Error, "Bad request: #{body}"
9191
when 401

lib/pusher/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Pusher
2-
VERSION = '1.2.0.rc2'
2+
VERSION = '1.2.0.rc3'
33
end

spec/client_spec.rb

Lines changed: 2 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,6 @@
550550
expect(@client.notification_host).to eq(@client.notification_client.host)
551551
end
552552

553-
it "should raise an error if the gcm or apns key isn't provided in the payload" do
554-
expect { @client.notify(["test"], { foo: "bar" }) }.to raise_error(Pusher::Error)
555-
end
556-
557553
it "should raise an error if more than one interest is provided" do
558554
payload = {
559555
gcm: {
@@ -567,70 +563,6 @@
567563
expect { @client.notify(["test1", "test2"], payload) }.to raise_error(Pusher::Error)
568564
end
569565

570-
it "should raise an error if the notification hash is missing the title field" do
571-
payload = {
572-
gcm: {
573-
notification: {
574-
icon: "someicon"
575-
}
576-
}
577-
}
578-
579-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
580-
end
581-
582-
it "should raise an error if the notification title is empty" do
583-
payload = {
584-
gcm: {
585-
notification: {
586-
title: "",
587-
icon: "myicon"
588-
}
589-
}
590-
}
591-
592-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
593-
end
594-
595-
it "should raise an error if the notification hash is missing the icon field" do
596-
payload = {
597-
gcm: {
598-
notification: {
599-
title: "sometitle"
600-
}
601-
}
602-
}
603-
604-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
605-
end
606-
607-
it "should raise an error if the notification icon is empty" do
608-
payload = {
609-
gcm: {
610-
notification: {
611-
title: "title",
612-
icon: ""
613-
}
614-
}
615-
}
616-
617-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
618-
end
619-
620-
it "should raise an error if the ttl field is provided and has an illegal value" do
621-
payload = {
622-
gcm: {
623-
time_to_live: 98091283,
624-
notification: {
625-
title: "title",
626-
icon: "icon",
627-
}
628-
}
629-
}
630-
631-
expect{ @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
632-
end
633-
634566
it "should send a request to the notifications endpoint" do
635567
notification_host_regexp = %r{nativepush-cluster1.pusher.com}
636568
payload = {
@@ -653,76 +585,8 @@
653585
:body => MultiJson.encode({ :foo => "bar" })
654586
})
655587

656-
@client.notify(["test"], payload)
657-
end
658-
659-
it "should delete restricted gcm keys before sending a notification" do
660-
notification_host_regexp = %r{nativepush-cluster1.pusher.com}
661-
payload = {
662-
interests: ["test"],
663-
gcm: {
664-
notification: {
665-
title: "Hello",
666-
icon: "icon",
667-
}
668-
}
669-
}
670-
671-
stub_request(
672-
:post,
673-
notification_host_regexp,
674-
).with(
675-
body: MultiJson.encode(payload)
676-
).to_return({
677-
:status => 200,
678-
:body => MultiJson.encode({ :foo => "bar" })
679-
})
680-
681-
payload[:gcm].merge!(to: "blah", registration_ids: ["reg1", "reg2"])
682-
@client.notify(["test"], payload)
683-
end
684-
685-
it "should raise an error for an invalid webhook url field" do
686-
payload = {
687-
gcm: {
688-
notification: {
689-
title: "Hello",
690-
icon: "icon"
691-
}
692-
},
693-
webhook_url: "totallyinvalid"
694-
}
695-
696-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
697-
end
698-
699-
it "should raise an error if the webhook level is not supported" do
700-
payload = {
701-
gcm: {
702-
notification: {
703-
title: "Hello",
704-
icon: "icon"
705-
}
706-
},
707-
webhook_url: "http://test.com/wh",
708-
webhook_level: "meh"
709-
}
710-
711-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
712-
end
713-
714-
it "should raise an error if the webhook level is used without the webhook url" do
715-
payload = {
716-
gcm: {
717-
notification: {
718-
title: "Hello",
719-
icon: "icon"
720-
}
721-
},
722-
webhook_level: "meh"
723-
}
724-
725-
expect { @client.notify(["test"], payload) }.to raise_error(Pusher::Error)
588+
res = @client.notify(["test"], payload)
589+
expect(res).to eq({foo: "bar"})
726590
end
727591
end
728592
end

0 commit comments

Comments
 (0)