SimpleGCM 0.1

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.
Files changed (2) hide show
  1. data/lib/simple_gcm.rb +93 -0
  2. metadata +45 -0
@@ -0,0 +1,93 @@
1
+ require "bundler/setup"
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ module SimpleGCM
7
+ URL = "https://android.googleapis.com/gcm/send"
8
+
9
+ def self.notify devices, options={}
10
+ devices = Array(devices)
11
+ request_data = {registration_ids: devices}
12
+ request_data.merge! filter_options(options,
13
+ :collapse_key,
14
+ :data,
15
+ :delay_while_idle,
16
+ :time_to_live,
17
+ :dry_run
18
+ )
19
+
20
+ uri = URI.parse(URL)
21
+ http = Net::HTTP.new(uri.host, uri.port)
22
+ http.use_ssl = true
23
+
24
+ request = Net::HTTP::Post.new(uri.request_uri)
25
+ request.body = request_data.to_json
26
+ request.initialize_http_header(
27
+ 'Authorization' => "key=#{options[:key]}",
28
+ 'Content-Type' => 'application/json'
29
+ )
30
+
31
+ res = http.request(request)
32
+
33
+ data = {}
34
+ begin
35
+ data = JSON.parse(res.body)
36
+ rescue JSON::ParserError
37
+ end
38
+
39
+ Response.new(res, data, devices)
40
+ end
41
+
42
+ def self.filter_options options, *keys
43
+ keys = keys.collect &:to_sym
44
+ options.select do |key, value|
45
+ keys.any? {|k| k == key.to_sym}
46
+ end
47
+ end
48
+
49
+ class Response
50
+ attr_accessor :http_response
51
+ attr_accessor :data
52
+ attr_accessor :registration_ids
53
+
54
+ def initialize(res, data, devices)
55
+ self.http_response = res
56
+ self.data = HashWithIndifferentAccess.new_from_hash_copying_default(data)
57
+ self.registration_ids = devices
58
+
59
+ @results = {}
60
+ self.registration_ids.each_with_index do |item, index|
61
+ @results[item] = self.data[:results] && self.data[:results][index]
62
+ end
63
+ end
64
+
65
+ def errors?
66
+ self.http_response.code != '200' || self.data[:failures] != 0
67
+ end
68
+
69
+ def results
70
+ @results
71
+ end
72
+
73
+ def each_error
74
+ @results.each_key do |reg_id|
75
+ result = @results[reg_id]
76
+ error = result[:error]
77
+ yield reg_id, error if error
78
+ end
79
+ end
80
+
81
+ def each_registration_id
82
+ @results.each_key do |old_id|
83
+ result = @results[key]
84
+ new_id = result[:registration_id]
85
+ yield old_id, new_id if new_id
86
+ end
87
+ end
88
+
89
+ def status
90
+ self.http_response.code
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: SimpleGCM
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benjamin Taylor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple library for connecting and communicating with Google Cloud Messaging
15
+ email: ben.taylor@snepo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/simple_gcm.rb
21
+ homepage: http://github.com/snepo/simple-gcm
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Simple Google Cloud Messaging
45
+ test_files: []