Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/c2dm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def send_notifications(notifications = [])
rescue GCMError => e
response = e
end

{
"registration_id" => notification[:registration_id],
"response" => response
Expand All @@ -45,36 +45,36 @@ def initialize(api_key = nil)
# send_notification("aRegId...", {some_message: "hello", a_value: 5}, "optional collapse_key")
def send_notification(registration_id, data, collapse_key = nil)
raise GCMError.new("registration_id must be a String") unless registration_id.is_a?(String)

payload = {
registration_ids: [registration_id],
data: data,
collapse_key: collapse_key
:registration_ids => [registration_id],
:data => data,
:collapse_key => collapse_key
}

parse_response(send(payload))
end

private
def send(payload)
self.class.post(API_ENDPOINT, body: payload.to_json, headers: request_headers)
self.class.post(API_ENDPOINT, :body => payload.to_json, :headers => request_headers)
end

def request_headers
{
"Authorization" => "key=#{@api_key}",
"Content-Type" => "application/json"
}
end

def parse_response(response)
begin
parsed_response = JSON.parse(response.body)

if (parsed_response["results"].first["error"])
raise GCMError.new(parsed_response["results"].first["error"])
end

parsed_response
rescue JSON::ParserError
raise GCMError.new(response.body)
Expand Down