higcm 0.0.1 → 0.0.2

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.
data/README.md CHANGED
@@ -7,12 +7,12 @@ use [Typhoeus](http://typhoeus.github.com/) as http client so it is able to send
7
7
 
8
8
  ## handler
9
9
  parse gcm response with GCM::Handler according to [GCM Response format](http://developer.android.com/guide/google/gcm/gcm.html#response),
10
- into serveral kinds of responses, say, success, fails, retry, unregister, you can only handle events you care about.
10
+ into serveral kinds of events, say, success, fails, retry, unregister, you can only handle events you care about.
11
11
 
12
12
  # Usage
13
13
 
14
14
  ## send a message
15
- <code>
15
+ ```ruby
16
16
  sender = HiGCM::Sender.new(your_api_key)
17
17
  registration_ids = [1, 2, 3]
18
18
  opts = {
@@ -21,10 +21,10 @@ opts = {
21
21
  }
22
22
  response = sender.send(registration_ids, opts)
23
23
  ...
24
- </code>
24
+ ```
25
25
 
26
26
  ## send a message with handler
27
- <code>
27
+ ```ruby
28
28
  # prepare handler for retry and unregister event
29
29
  handler = HiGCM::Handler.new
30
30
  handler.do_retry do | retry_ids, opts, response |
@@ -45,11 +45,11 @@ opts = {
45
45
  :collapse_key => "test"
46
46
  :data => { :mesg => "hello GCM" }
47
47
  }
48
- sender.send(registration_ids, opts)
49
- </code>
48
+ sender.send(registration_ids, opts, handler)
49
+ ```
50
50
 
51
51
  ## send a muti-messages in parallel way
52
- <code>
52
+ ```ruby
53
53
  sender = HiGCM::Sender.new(your_api_key)
54
54
 
55
55
  # queue your messages first
@@ -76,4 +76,4 @@ something.each do | registration_id, name |
76
76
  end
77
77
  # now fire
78
78
  sender.send_async_run
79
- </code>
79
+ ```
data/higcm.gemspec CHANGED
@@ -4,7 +4,7 @@ $:.unshift File.expand_path('../lib', __FILE__)
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "higcm"
7
- s.version = "0.0.1"
7
+ s.version = "0.0.2"
8
8
  s.authors = ["hifrank"]
9
9
  s.email = "frank_chen@htc.com"
10
10
  s.homepage = "https://github.com/hifrank/higcm"
data/lib/higcm/handler.rb CHANGED
@@ -4,14 +4,16 @@ module HiGCM
4
4
 
5
5
  class Handler
6
6
 
7
- attr_accessor :retry_conditions
7
+ attr_accessor :retry_conditions, :unregister_conditions
8
8
 
9
9
  def initialize
10
10
  @retry_ids = {}
11
- @fail_ids = {}
12
- @success_ids = {}
13
- @renew_ids = {}
14
- @retry_conditions =[ 'InternalServerError', 'Timout', 'Unavailable' ]
11
+ @unreg_ids = {}
12
+ @fail_ids = {}
13
+ @success_ids = {}
14
+ @renew_ids = {}
15
+
16
+ @retry_conditions = [ 'InternalServerError', 'Timout', 'Unavailable' ]
15
17
  @unregister_conditions = [ 'NotRegistered', 'InvalidRegistration' ]
16
18
  end
17
19
 
@@ -20,11 +22,12 @@ module HiGCM
20
22
  @code = response.code.to_i
21
23
  @body = JSON.parse(response.body)
22
24
  #Honor Retry-After
23
- @retry_after = (response.headers['Retry-After'].nil? ? 0 : response.headers['Retry-After'])
25
+ @retry_after = (response.headers['Retry-After'].nil? ? 0 : response.headers['Retry-After'])
24
26
  rescue Exception => e
25
27
  @code = 99
26
28
  @error_message = "unexpected error, response: #{response.body}, exception: #{e.inspect}"
27
29
  end
30
+
28
31
  case @code
29
32
  when 200
30
33
  #200 Message was processed successfully. The response body will contain more details about the message status, but its format will depend whether the request was JSON or plain text. See Interpreting a success response for more details.
@@ -34,10 +37,15 @@ module HiGCM
34
37
  #handle fail
35
38
  if rs.key?('error')
36
39
  @error_message = rs['error']
40
+ # figure out retry stuff
37
41
  if @retry_conditions.include?(rs['error'])
38
42
  @retry_ids[reg_id] = @retry_after
39
43
  @error_message << ", retry after #{@retry_after}"
40
44
  end
45
+ # figure out unregister stuff
46
+ if @unregister_conditions.include?(rs['error'])
47
+ @unreg_ids[reg_id] = rs['error']
48
+ end
41
49
  @fail_ids[reg_id] = @error_message
42
50
  #handle success
43
51
  elsif rs.key?('message_id')
@@ -49,16 +57,18 @@ module HiGCM
49
57
  #should not jump here
50
58
  end
51
59
  end
52
- @do_success.call(@success_ids, response) if @success_ids.count > 0 && @do_success
53
- @do_fail.call(@fail_ids, response) if @fail_ids.count > 0 && @do_fail
54
- @do_renew_token.call(@renew_ids, response) if @renew_ids.count > 0 && @do_renew_token
55
- @do_retry.call(@retry_ids, opts, response) if @retry_ids.count > 0 && @do_retry
60
+ @do_success.call(@success_ids, response) if @success_ids.count > 0 && @do_success
61
+ @do_fail.call(@fail_ids, response) if @fail_ids.count > 0 && @do_fail
62
+ @do_renew_token.call(@renew_ids, response) if @renew_ids.count > 0 && @do_renew_token
63
+ @do_retry.call(@retry_ids, opts, response) if @retry_ids.count > 0 && @do_retry
64
+ @do_unregister_token.call(@unreg_ids, response) if @unreg_ids.count > 0 && @do_unregister_token
65
+
56
66
  #TODO need to check what kinf of response will return
57
67
  when 400, 401
58
68
  #400 Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected). The exact failure reason is described in the response and the problem should be addressed before the request can be retried.
59
69
  #401 There was an error authenticating the sender account.
60
70
  registration_ids.each do | reg_id |
61
- if 400 == @code
71
+ if 400 == @code
62
72
  error_message = 'request could not be parsed as JSON'
63
73
  else
64
74
  error_message = 'There was an error authenticating the sender account'
@@ -101,5 +111,9 @@ module HiGCM
101
111
  @do_renew_token = block
102
112
  end
103
113
 
114
+ def do_unregister_token(&block)
115
+ @do_unregister_token = block
116
+ end
117
+
104
118
  end
105
119
  end
@@ -1,66 +1,86 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
3
  describe HiGCM::Handler do
4
- it "#handle should handle various response in when response code is 500" do
4
+ describe "#setting different criteria for retry & unregister" do
5
+ it "should take retry_conditions & unregister_conditions" do
6
+ handler = HiGCM::Handler.new
7
+ handler.unregister_conditions = ['foo']
8
+ handler.unregister_conditions.should == ['foo']
9
+
10
+ handler.retry_conditions = ['foo']
11
+ handler.retry_conditions.should == ['foo']
12
+ end
5
13
  end
6
14
 
7
- it "#handle should handle various response when response code is 200" do
8
- @raw_gcm_response = File.read("#{File.dirname(__FILE__)}/../../fixtures/gcm_response_200.json")
9
- @retry_after = 10
10
- @stub_gcm_response = Typhoeus::Response.new(
11
- :code => 200,
12
- :headers => {"Retry-After" => @retry_after},
13
- :body => @raw_gcm_response,
14
- :time => 0.1
15
- )
16
- @api_key = 'foo'
17
- sender = HiGCM::Sender.new(@api_key)
18
- sender.hydra = Typhoeus::Hydra.hydra
19
- sender.hydra.stub(:post, 'https://android.googleapis.com/gcm/send').and_return(@stub_gcm_response)
15
+ describe "#handle" do
16
+ it "#handle should handle various response in when response code is 500" do
17
+ end
20
18
 
21
- _fails = 0
22
- _success = 0
23
- _renew = 0
24
- _retry = 0
19
+ it "#handle should handle various response when response code is 200" do
20
+ @raw_gcm_response = File.read("#{File.dirname(__FILE__)}/../../fixtures/gcm_response_200.json")
21
+ @retry_after = 10
22
+ @stub_gcm_response = Typhoeus::Response.new(
23
+ :code => 200,
24
+ :headers => {"Retry-After" => @retry_after},
25
+ :body => @raw_gcm_response,
26
+ :time => 0.1
27
+ )
28
+ @api_key = 'foo'
29
+ sender = HiGCM::Sender.new(@api_key)
30
+ sender.hydra = Typhoeus::Hydra.hydra
31
+ sender.hydra.stub(:post, 'https://android.googleapis.com/gcm/send').and_return(@stub_gcm_response)
25
32
 
26
- _updated_token = { 5 => "32"}
33
+ _fails = 0
34
+ _success = 0
35
+ _renew = 0
36
+ _retry = 0
27
37
 
28
- handler = HiGCM::Handler.new
29
- handler.do_success do | succes_ids, response |
30
- @success_ids = succes_ids
31
- @success_response = response
32
- end
33
- handler.do_retry do | retry_ids, opts, response |
34
- @retry_ids = retry_ids
38
+ _updated_token = { 5 => "32"}
39
+
40
+ handler = HiGCM::Handler.new
41
+ handler.do_success do | succes_ids, response |
42
+ @success_ids = succes_ids
43
+ @success_response = response
44
+ end
45
+ handler.do_retry do | retry_ids, opts, response |
46
+ @retry_ids = retry_ids
35
47
  @retry_opts = opts
36
48
  @retry_response = response
37
- end
38
- handler.do_fail do | fail_ids, response |
39
- @fail_ids = fail_ids
49
+ end
50
+ handler.do_fail do | fail_ids, response |
51
+ @fail_ids = fail_ids
40
52
  @fail_response = response
41
- end
42
- handler.do_renew_token do | renew_ids, response |
43
- @renew_ids = renew_ids
53
+ end
54
+ handler.do_renew_token do | renew_ids, response |
55
+ @renew_ids = renew_ids
44
56
  @renew_response = response
45
- end
57
+ end
58
+ handler.do_unregister_token do | unreg_ids, response |
59
+ @unreg_ids = unreg_ids
60
+ @unreg_response = response
61
+ end
46
62
 
47
- registration_ids = [1, 2, 3, 4, 5, 6]
48
- sender.send_async(registration_ids, {}, handler)
49
- sender.send_async_run
63
+ registration_ids = [1, 2, 3, 4, 5, 6]
64
+ sender.send_async(registration_ids, {}, handler)
65
+ sender.send_async_run
50
66
 
51
- @fail_ids.should == { 2 => "Unavailable, retry after #{@retry_after}", 3 => "InvalidRegistration", 6 => "NotRegistered" }
52
- @fail_response.is_a?(Typhoeus::Response).should == true
67
+ @fail_ids.should == { 2 => "Unavailable, retry after #{@retry_after}", 3 => "InvalidRegistration", 6 => "NotRegistered" }
68
+ @fail_response.is_a?(Typhoeus::Response).should == true
53
69
 
54
- @retry_ids.should == { 2 => 10 }
55
- @retry_opts.should == {}
56
- @retry_response.is_a?(Typhoeus::Response).should == true
70
+ @retry_ids.should == { 2 => 10 }
71
+ @retry_opts.should == {}
72
+ @retry_response.is_a?(Typhoeus::Response).should == true
57
73
 
58
- @renew_ids.should == { 5 => "32" }
59
- @renew_response.is_a?(Typhoeus::Response).should == true
74
+ @renew_ids.should == { 5 => "32" }
75
+ @renew_response.is_a?(Typhoeus::Response).should == true
60
76
 
61
- @success_ids.should == { 1 => "1:0408", 4 => "1:1516", 5 => "1:2342" }
62
- @success_response.is_a?(Typhoeus::Response).should == true
77
+ @success_ids.should == { 1 => "1:0408", 4 => "1:1516", 5 => "1:2342" }
78
+ @success_response.is_a?(Typhoeus::Response).should == true
63
79
 
80
+ @unreg_ids.should == { 3 => "InvalidRegistration", 6 => "NotRegistered" }
81
+ @unreg_response.is_a?(Typhoeus::Response).should == true
82
+
83
+ end
64
84
  end
65
85
 
66
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: higcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-21 00:00:00.000000000Z
12
+ date: 2012-07-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
16
- requirement: &2177156140 !ruby/object:Gem::Requirement
16
+ requirement: &2185503900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.3.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2177156140
24
+ version_requirements: *2185503900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &2177142980 !ruby/object:Gem::Requirement
27
+ requirement: &2185503040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.6'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2177142980
35
+ version_requirements: *2185503040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: json
38
- requirement: &2177142280 !ruby/object:Gem::Requirement
38
+ requirement: &2185502040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2177142280
46
+ version_requirements: *2185502040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &2177141340 !ruby/object:Gem::Requirement
49
+ requirement: &2185501180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2177141340
57
+ version_requirements: *2185501180
58
58
  description: ruby wrapper for GCM google cloud messaging.
59
59
  email: frank_chen@htc.com
60
60
  executables: []