gcm 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c2094f1f941e71303802d03af0af0c60841598e
4
+ data.tar.gz: 533de2aad017e25cc44b8d56af68932655869881
5
+ SHA512:
6
+ metadata.gz: b9d5138d41d34d02bffc3b8c0c6a71d61b7862b4eb9c8de5647df9cc52e127b9e5510709de0bfb13a346823588eb6740b41e4de51c7fa19ef4192a5560b2cb83
7
+ data.tar.gz: a838377705386fe708f8cdfc281fba52d586c36d9da3472fe3d7086016a2ee1228ae6a22745b0049c8d5ad5d3afe25b242b61f894bf68c1b9dbbc6d164d2e648
data/.gitignore CHANGED
@@ -14,7 +14,7 @@ doc
14
14
  # jeweler generated
15
15
  pkg
16
16
 
17
- # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
17
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
18
  #
19
19
  # * Create a file at ~/.gitignore
20
20
  # * Include files you want ignored
@@ -47,3 +47,4 @@ gems
47
47
  specifications
48
48
  Gemfile.lock
49
49
  .rvmrc
50
+ spec/reports
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
data/Gemfile CHANGED
@@ -1,6 +1,7 @@
1
- source "http://rubygems.org"
2
-
1
+ source "https://rubygems.org"
3
2
  gemspec
4
3
 
4
+ gem 'rake'
5
5
  gem 'rspec'
6
6
  gem 'webmock'
7
+ gem "ci_reporter"
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Google Cloud Messaging for Android (GCM)
2
+ [![Build Status](https://secure.travis-ci.org/shamithc/gcm.png?branch=master)](http://travis-ci.org/shamithc/gcm)
2
3
 
3
4
  GCM sends notifications to Android devices via [GCM](http://developer.android.com/guide/google/gcm/gcm.html).
4
5
 
@@ -12,24 +13,27 @@ An Android device running 2.0 or newer and an API key as per [GCM getting starte
12
13
 
13
14
  ##Usage
14
15
 
15
-
16
16
  Sending notifications:
17
17
 
18
18
  ```ruby
19
+ require 'gcm'
20
+
19
21
  gcm = GCM.new(api_key)
20
22
  registration_ids= ["12", "13"] # an array of one or more client registration IDs
21
- options = {body: data: {key: "value"}}
23
+ options = {data: {score: "123"}, collapse_key: "updated_score"}
22
24
  response = gcm.send_notification(registration_ids, options)
23
25
  ```
24
26
 
25
27
  Currently `response` is just a hash containing the response `body`, `headers` and `status`.
26
28
 
27
- ##Copyright
29
+ If the above code is stored in a file like `trigger_gcm.rb`, thats how you can call it.
28
30
 
29
- * Copyright (c) 2012 Kashif Rasul and Shoaib Burq. See LICENSE.txt for details.
31
+ $ ruby -rubygems trigger_gcm.rb
30
32
 
31
- ##Thanks
33
+ ##MIT License
34
+
35
+ * Copyright (c) 2012 Kashif Rasul and Shoaib Burq. See LICENSE.txt for details.
32
36
 
33
- This gem is based on a fork of the older Google push service:
37
+ ##Many thanks to all the contributors
34
38
 
35
- * [Amro Mousa](https://github.com/amro/c2dm/)
39
+ * [Contributors](https://github.com/spacialdb/gcm/contributors)
data/Rakefile CHANGED
@@ -1 +1,17 @@
1
+ require 'rspec/core/rake_task'
1
2
  require "bundler/gem_tasks"
3
+ require "rake/tasklib"
4
+ require 'ci/reporter/rake/rspec'
5
+
6
+ RSpec::Core::RakeTask.new(:spec => ["ci:setup:rspec"]) do |t|
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ end
9
+
10
+
11
+ RSpec::Core::RakeTask.new(:spec) do |spec|
12
+ spec.pattern = 'spec/**/*_spec.rb'
13
+ spec.rspec_opts = ['--format documentation']
14
+ end
15
+
16
+
17
+ task :default => :spec
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "gcm"
6
- s.version = "0.0.2"
6
+ s.version = "0.0.4"
7
7
  s.authors = ["Amro Mousa", "Kashif Rasul", "Shoaib Burq"]
8
8
  s.email = ["amromousa@gmail.com", "kashif@spacialdb.com", "shoaib@spacialdb.com"]
9
9
  s.homepage = "http://github.com/spacialdb/gcm"
data/lib/gcm.rb CHANGED
@@ -31,22 +31,36 @@ class GCM
31
31
  post_body = build_post_body(registration_ids, options)
32
32
 
33
33
  params = {
34
- :body => post_body.to_json,
34
+ :body => post_body.to_json,
35
35
  :headers => {
36
- 'Authorization' => "key=#{@api_key}",
37
- 'Content-Type' => 'application/json',
36
+ 'Authorization' => "key=#{@api_key}",
37
+ 'Content-Type' => 'application/json',
38
38
  }
39
39
  }
40
40
 
41
41
  response = self.class.post('', params)
42
- {body: response.body, headers: response.headers, status: response.code}
42
+ build_response(response)
43
43
  end
44
44
 
45
45
  private
46
46
 
47
47
  def build_post_body(registration_ids, options={})
48
- body = {registration_ids: registration_ids}.merge(options)
49
- #raise exception if options[:time_to_live] && !options[:collapse_key]
48
+ { :registration_ids => registration_ids }.merge(options)
50
49
  end
51
50
 
51
+ def build_response(response)
52
+ case response.code
53
+ when 200
54
+ body = response.body || {}
55
+ { :response => 'success', :body => body, :headers => response.headers, :status_code => response.code }
56
+ when 400
57
+ { :response => 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.', :status_code => response.code }
58
+ when 401
59
+ { :response => 'There was an error authenticating the sender account.', :status_code => response.code }
60
+ when 500
61
+ { :response => 'There was an internal error in the GCM server while trying to process the request.', :status_code => response.code }
62
+ when 503
63
+ { :response => 'Server is temporarily unavailable.', :status_code => response.code }
64
+ end
65
+ end
52
66
  end
@@ -13,7 +13,7 @@ describe GCM do
13
13
  let(:api_key) { 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA' }
14
14
  let(:registration_ids) { [ "42" ]}
15
15
  let(:valid_request_body) do
16
- { registration_ids: registration_ids }
16
+ { :registration_ids => registration_ids }
17
17
  end
18
18
  let(:valid_request_headers) do
19
19
  {
@@ -24,35 +24,126 @@ describe GCM do
24
24
 
25
25
  before(:each) do
26
26
  stub_request(:post, GCM::PUSH_URL).with(
27
- body: valid_request_body.to_json,
28
- headers: valid_request_headers
27
+ :body => valid_request_body.to_json,
28
+ :headers => valid_request_headers
29
29
  ).to_return(
30
30
  # ref: http://developer.android.com/guide/google/gcm/gcm.html#success
31
- body: {},
32
- headers: {},
33
- status: 200
31
+ :body => {},
32
+ :headers => {},
33
+ :status => 200
34
34
  )
35
35
  end
36
36
 
37
37
  it "should send notification using POST to GCM server" do
38
38
  gcm = GCM.new(api_key)
39
- gcm.send_notification(registration_ids).should eq({body: {}, headers: {}, status: 200})
39
+ gcm.send_notification(registration_ids).should eq({:response => 'success', :body => {}, :headers => {}, :status_code => 200})
40
40
  end
41
41
 
42
42
  context "send notification with data" do
43
43
  let!(:stub_with_data){
44
44
  stub_request(:post, GCM::PUSH_URL).
45
- with(body: "{\"registration_ids\":[\"42\"],\"data\":{\"score\":\"5x1\",\"time\":\"15:10\"}}",
46
- headers: valid_request_headers ).
47
- to_return(status: 200, body: "", headers: {})
45
+ with(:body => "{\"data\":{\"score\":\"5x1\",\"time\":\"15:10\"},\"registration_ids\":[\"42\"]}",
46
+ :headers => valid_request_headers ).
47
+ to_return(:status => 200, :body => "", :headers => {})
48
48
  }
49
49
  before do
50
50
  end
51
51
  it "should send the data in a post request to gcm" do
52
52
  gcm = GCM.new(api_key)
53
- gcm.send_notification(registration_ids, { data: { score: "5x1", time: "15:10"} })
53
+ gcm.send_notification(registration_ids, { :data => { :score => "5x1", :time => "15:10"} })
54
54
  stub_with_data.should have_been_requested
55
55
  end
56
56
  end
57
+
58
+ context " when send_notification responds with failure" do
59
+
60
+ let(:mock_request_attributes) do
61
+ {
62
+ :body => valid_request_body.to_json,
63
+ :headers => valid_request_headers
64
+ }
65
+ end
66
+
67
+ subject { GCM.new(api_key) }
68
+
69
+ context "on failure code 400" do
70
+ before do
71
+ stub_request(:post, GCM::PUSH_URL).with(
72
+ mock_request_attributes
73
+ ).to_return(
74
+ # ref: http://developer.android.com/guide/google/gcm/gcm.html#success
75
+ :body => {},
76
+ :headers => {},
77
+ :status => 400
78
+ )
79
+ end
80
+ it "should not send notification due to 400" do
81
+ subject.send_notification(registration_ids).should eq({
82
+ :response => "Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.",
83
+ :status_code => 400
84
+ })
85
+ end
86
+ end
87
+
88
+ context "on failure code 401" do
89
+ before do
90
+ stub_request(:post, GCM::PUSH_URL).with(
91
+ mock_request_attributes
92
+ ).to_return(
93
+ # ref: http://developer.android.com/guide/google/gcm/gcm.html#success
94
+ :body => {},
95
+ :headers => {},
96
+ :status => 401
97
+ )
98
+ end
99
+
100
+ it "should not send notification due to 401" do
101
+ subject.send_notification(registration_ids).should eq({
102
+ :response => "There was an error authenticating the sender account.",
103
+ :status_code => 401
104
+ })
105
+ end
106
+ end
107
+
108
+ context "on failure code 500" do
109
+ before do
110
+ stub_request(:post, GCM::PUSH_URL).with(
111
+ mock_request_attributes
112
+ ).to_return(
113
+ # ref: http://developer.android.com/guide/google/gcm/gcm.html#success
114
+ :body => {},
115
+ :headers => {},
116
+ :status => 500
117
+ )
118
+ end
119
+
120
+ it "should not send notification due to 500" do
121
+ subject.send_notification(registration_ids).should eq({
122
+ :response => 'There was an internal error in the GCM server while trying to process the request.',
123
+ :status_code => 500
124
+ })
125
+ end
126
+ end
127
+
128
+ context "on failure code 503" do
129
+ before do
130
+ stub_request(:post, GCM::PUSH_URL).with(
131
+ mock_request_attributes
132
+ ).to_return(
133
+ # ref: http://developer.android.com/guide/google/gcm/gcm.html#success
134
+ :body => {},
135
+ :headers => {},
136
+ :status => 503
137
+ )
138
+ end
139
+
140
+ it "should not send notification due to 503" do
141
+ subject.send_notification(registration_ids).should eq({
142
+ :response => 'Server is temporarily unavailable.',
143
+ :status_code => 503
144
+ })
145
+ end
146
+ end
147
+ end
57
148
  end
58
- end
149
+ end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Amro Mousa
@@ -11,38 +10,34 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2012-07-02 00:00:00.000000000 Z
13
+ date: 2013-06-04 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: httparty
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
21
  version: '0'
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
25
  requirements:
29
- - - ! '>='
26
+ - - '>='
30
27
  - !ruby/object:Gem::Version
31
28
  version: '0'
32
29
  - !ruby/object:Gem::Dependency
33
30
  name: json
34
31
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
32
  requirements:
37
- - - ! '>='
33
+ - - '>='
38
34
  - !ruby/object:Gem::Version
39
35
  version: '0'
40
36
  type: :runtime
41
37
  prerelease: false
42
38
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
39
  requirements:
45
- - - ! '>='
40
+ - - '>='
46
41
  - !ruby/object:Gem::Version
47
42
  version: '0'
48
43
  description: gcm is a service that helps developers send data from servers to their
@@ -57,6 +52,7 @@ extra_rdoc_files: []
57
52
  files:
58
53
  - .gitignore
59
54
  - .rspec
55
+ - .travis.yml
60
56
  - Gemfile
61
57
  - LICENSE.txt
62
58
  - README.md
@@ -68,29 +64,27 @@ files:
68
64
  homepage: http://github.com/spacialdb/gcm
69
65
  licenses:
70
66
  - MIT
67
+ metadata: {}
71
68
  post_install_message:
72
69
  rdoc_options: []
73
70
  require_paths:
74
71
  - lib
75
72
  required_ruby_version: !ruby/object:Gem::Requirement
76
- none: false
77
73
  requirements:
78
- - - ! '>='
74
+ - - '>='
79
75
  - !ruby/object:Gem::Version
80
76
  version: '0'
81
77
  required_rubygems_version: !ruby/object:Gem::Requirement
82
- none: false
83
78
  requirements:
84
- - - ! '>='
79
+ - - '>='
85
80
  - !ruby/object:Gem::Version
86
81
  version: '0'
87
82
  requirements: []
88
83
  rubyforge_project: gcm
89
- rubygems_version: 1.8.23
84
+ rubygems_version: 2.0.0
90
85
  signing_key:
91
- specification_version: 3
86
+ specification_version: 4
92
87
  summary: send data to Android applications on Android devices
93
88
  test_files:
94
89
  - spec/gcm_spec.rb
95
90
  - spec/spec_helper.rb
96
- has_rdoc: