stella_gcm 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/stella_gcm.rb +43 -0
- metadata +2 -2
- data/lib/stella/gcm.rb +0 -45
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 346c78764c3e91a83c2c7154fb333f466e3ae673
|
4
|
+
data.tar.gz: 4d5e7637fb3394e0896154d12cba209ac1b0594d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a425b83648cbab212f1e6ac4ce32c4c91a68989f099d61ef447bf2c532ca189e53606cc8eeaedabbba65dadd37803674243ea46bacb1479b129fbf0d094c5047
|
7
|
+
data.tar.gz: c2430c299675421de839c34728f671800cc2ea2a3a4ab4674572708184bf1c8602b531848f894bbf68759936d233ebefd2db2093292e0eaac972ee2d73d4a92c
|
data/lib/stella_gcm.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'net/http/persistent'
|
2
|
+
require 'active_support/json'
|
3
|
+
class StellaGCM
|
4
|
+
URL = 'https://android.googleapis.com/gcm/send'
|
5
|
+
|
6
|
+
def initialize(private_key, option = nil)
|
7
|
+
@http = Net::HTTP::Persistent.new 'StellaGcm'
|
8
|
+
@http.idle_timeout = option.nil? ? 1 :option[:idle_timeout]
|
9
|
+
@http.open_timeout = option.nil? ? 30 :option[:open_timeout]
|
10
|
+
@http.read_timeout = option.nil? ? 60 :option[:read_timeout]
|
11
|
+
@http.headers['Authorization'] = "key=#{private_key}"
|
12
|
+
@http.headers['Content-Type'] = "application/json"
|
13
|
+
end
|
14
|
+
|
15
|
+
def send_notification(ids, option)
|
16
|
+
gcm_server_uri = URI URL
|
17
|
+
request = Net::HTTP::Post.new gcm_server_uri.path
|
18
|
+
request.body = self.body(ids, option).to_json
|
19
|
+
|
20
|
+
begin
|
21
|
+
response = @http.request gcm_server_uri, request
|
22
|
+
rescue
|
23
|
+
return {:code => 404,
|
24
|
+
:success => false,
|
25
|
+
:msg => "GCM Server Not Connected."}
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
body = ActiveSupport::JSON.decode(response.body).with_indifferent_access
|
30
|
+
rescue
|
31
|
+
return {:code => 500,
|
32
|
+
:success => false,
|
33
|
+
:msg => "Json Parse failed Return Value."}
|
34
|
+
end
|
35
|
+
|
36
|
+
{:code => response.code.to_i,
|
37
|
+
:success => body[:success].to_i == 1}
|
38
|
+
end
|
39
|
+
|
40
|
+
def body(ids, option)
|
41
|
+
{:registration_ids => ids}.merge!(option)
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stella_gcm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Haru
|
@@ -44,7 +44,7 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
-
- lib/
|
47
|
+
- lib/stella_gcm.rb
|
48
48
|
homepage: http://www.forelf.com
|
49
49
|
licenses:
|
50
50
|
- GPL-2
|
data/lib/stella/gcm.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'net/http/persistent'
|
2
|
-
require 'active_support/json'
|
3
|
-
module Stella
|
4
|
-
class GCM
|
5
|
-
URL = 'https://android.googleapis.com/gcm/send'
|
6
|
-
|
7
|
-
def initialize(private_key, option = nil)
|
8
|
-
@http = Net::HTTP::Persistent.new 'StellaGcm'
|
9
|
-
@http.idle_timeout = option.nil? ? 1 :option[:idle_timeout]
|
10
|
-
@http.open_timeout = option.nil? ? 30 :option[:open_timeout]
|
11
|
-
@http.read_timeout = option.nil? ? 60 :option[:read_timeout]
|
12
|
-
@http.headers['Authorization'] = "key=#{private_key}"
|
13
|
-
@http.headers['Content-Type'] = "application/json"
|
14
|
-
end
|
15
|
-
|
16
|
-
def send_notification(ids, option)
|
17
|
-
gcm_server_uri = URI URL
|
18
|
-
request = Net::HTTP::Post.new gcm_server_uri.path
|
19
|
-
request.body = self.body(ids, option).to_json
|
20
|
-
|
21
|
-
begin
|
22
|
-
response = @http.request gcm_server_uri, request
|
23
|
-
rescue
|
24
|
-
return {:code => 404,
|
25
|
-
:success => false,
|
26
|
-
:msg => "GCM Server Not Connected."}
|
27
|
-
end
|
28
|
-
|
29
|
-
begin
|
30
|
-
body = ActiveSupport::JSON.decode(response.body).with_indifferent_access
|
31
|
-
rescue
|
32
|
-
return {:code => 500,
|
33
|
-
:success => false,
|
34
|
-
:msg => "Json Parse failed Return Value."}
|
35
|
-
end
|
36
|
-
|
37
|
-
{:code => response.code.to_i,
|
38
|
-
:success => body[:success].to_i == 1}
|
39
|
-
end
|
40
|
-
|
41
|
-
def body(ids, option)
|
42
|
-
{:registration_ids => ids}.merge!(option)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|