@@ -7,6 +7,7 @@ class Client
7
7
API_VERSION = "v1"
8
8
GCM_TTL = 241920
9
9
RESTRICTED_GCM_PAYLOAD_KEYS = [ :to , :registration_ids ]
10
+ WEBHOOK_LEVELS = [ "DEBUG" , "INFO" ]
10
11
11
12
def initialize ( app_id , host , pusher_client )
12
13
@app_id = app_id
@@ -74,21 +75,18 @@ def validate_payload(payload)
74
75
raise Pusher ::Error , "GCM or APNS data must be provided"
75
76
end
76
77
77
- if payload . has_key? ( :gcm )
78
- gcm_payload = payload [ :gcm ]
78
+ if ( gcm_payload = payload [ :gcm ] )
79
+ if ( ttl = gcm_payload [ :time_to_live ] )
79
80
80
- if gcm_payload . has_key? ( :time_to_live )
81
- ttl = gcm_payload [ :time_to_live ] . to_i
82
-
83
- if ttl < 0 || ttl > GCM_TTL
84
- raise Pusher ::Error , "time_to_live key must have a value between 0 and 241920 (4 weeks)"
81
+ if ttl . to_i < 0 || ttl . to_i > GCM_TTL
82
+ raise Pusher ::Error , "Time to live must be between 0 and 241920 (4 weeks)"
85
83
end
86
84
end
87
85
88
86
# If the notification key is provided
89
87
# validate the `icon` and `title`keys
90
- if gcm_payload . has_key? ( :notification )
91
- notification_title , notification_icon = gcm_payload [ : notification] . values_at ( :title , :icon )
88
+ if ( notification = gcm_payload [ :notification ] )
89
+ notification_title , notification_icon = notification . values_at ( :title , :icon )
92
90
93
91
if ( !notification_title || notification_title . empty? )
94
92
raise Pusher ::Error , "Notification title is a required field"
@@ -99,6 +97,18 @@ def validate_payload(payload)
99
97
end
100
98
end
101
99
end
100
+
101
+ if ( webhook_url = payload [ :webhook_url ] )
102
+ raise Pusher ::Error , "Webhook url is invalid" unless webhook_url =~ /\A #{ URI ::regexp ( [ 'http' , 'https' ] ) } \z /
103
+ end
104
+
105
+ if ( webhook_level = payload [ :webhook_level ] )
106
+ raise Pusher ::Error , "Webhook level cannot be used without a webhook url" if !payload . has_key? ( :webhook_url )
107
+
108
+ unless WEBHOOK_LEVELS . includes? ( webhook_level . upcase )
109
+ raise Pusher ::Error , "Webhook level must either be INFO or DEBUG"
110
+ end
111
+ end
102
112
end
103
113
104
114
# Symbolize all keys in the hash recursively
0 commit comments