rgcm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d996818f09f71cff4eb15f5192e5139f5fe384d
4
+ data.tar.gz: f53dd56ea6fe510e402b0df4135849b7ddfdba43
5
+ SHA512:
6
+ metadata.gz: c3e4be4a1b81007d31d5ddd0d16d8a5155e4369b7ef36c17a502e3e0e628dd478b586aad7316bdebab04f12fc9db6c1476dc7da4ee4fd369593b51e1185e60b3
7
+ data.tar.gz: c5dac96c370149f9e3f6570cc003b4a54cda7a75c93bbcb0f0ab19dca08bebc7d0529dcaab5683a7dcc2515ea26b34c55c28c6906dba76e089de6b749deec5d4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ script: "bundle exec rake spec"
2
+ rvm:
3
+ - 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rgcm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Klaiber
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Rgcm
2
+
3
+ [![Build Status](https://api.travis-ci.org/aklaiber/rgcm.svg)][travis]
4
+ [![Gem Version](http://img.shields.io/gem/v/rgcm.svg)][gem]
5
+ [![Code Climate](https://codeclimate.com/github/aklaiber/rgcm.png)][codeclimate]
6
+ [![Dependencies Status](http://img.shields.io/gemnasium/aklaiber/rgcm.svg)][gemnasium]
7
+
8
+ [travis]: https://travis-ci.org/aklaiber/rgcm
9
+ [gem]: https://rubygems.org/gems/rgcm
10
+ [codeclimate]: https://codeclimate.com/github/aklaiber/rgcm
11
+ [gemnasium]: https://gemnasium.com/aklaiber/rgcm
12
+
13
+
14
+ Ruby mapper for [Google Cloud Messaging](http://developer.android.com/guide/google/gcm/index.html)
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'rgcm'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install rgcm
29
+
30
+ ## Usage
31
+
32
+ TODO: Write usage instructions here
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( http://github.com/<my-github-username>/rgcm/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,14 @@
1
+ module Rgcm
2
+ class Config
3
+
4
+ URI = 'https://android.googleapis.com/gcm/send'.freeze
5
+
6
+ class << self
7
+ attr_writer :config
8
+ end
9
+
10
+ def self.api_key
11
+ @config['api_key']
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module Rgcm
2
+ class Message
3
+
4
+ attr_reader :api_key
5
+
6
+ def initialize(config = Rgcm::Config)
7
+ if config.kind_of?(String)
8
+ @api_key = config
9
+ elsif config.kind_of?(Hash)
10
+ @api_key = config[:api_key]
11
+ elsif config.kind_of?(Class)
12
+ @api_key = config.api_key
13
+ end
14
+ end
15
+
16
+ def post(registration_ids, collapse_key, data)
17
+ request = RequestBuilder.new(api_key, registration_ids, collapse_key, data).build
18
+
19
+ response = request.run
20
+
21
+ Rgcm::Response.new(response.body)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module Rgcm
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'setup rgcm' do
5
+ config_file = Rails.root.join('config', 'rgcm.yml')
6
+ if config_file.file?
7
+ Rgcm::Config.config = YAML.load(ERB.new(config_file.read).result)[Rails.env]
8
+ end
9
+ config.after_initialize do
10
+ puts "\nRgcm config not found. Create a config file at: config/rgcm.yml" unless config_file.file?
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module Rgcm
2
+ class RequestBuilder
3
+
4
+ URI = 'https://android.googleapis.com/gcm/send'.freeze
5
+
6
+ attr_reader :api_key, :registration_ids, :collapse_key, :data
7
+
8
+ def initialize(api_key, registration_ids, collapse_key, data)
9
+ @api_key = api_key
10
+ @registration_ids = registration_ids.kind_of?(Array) ? registration_ids : [registration_ids]
11
+ @collapse_key = collapse_key
12
+ @data = data
13
+ end
14
+
15
+ def build
16
+ Typhoeus::Request.new(URI, headers: headers, method: :post, body: body.to_json, followlocation: true)
17
+ end
18
+
19
+ private
20
+
21
+ def headers
22
+ {'Authorization' => "key=#{api_key}", 'Content-Type' => 'application/json'}
23
+ end
24
+
25
+ def body
26
+ body = {'registration_ids' => registration_ids}
27
+ body.merge!('collapse_key' => collapse_key) if collapse_key.present?
28
+ body.merge!('data' => data) if data.present?
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ module Rgcm
2
+ class Response
3
+ attr_reader :body
4
+
5
+ def initialize(json)
6
+ @body = JSON.parse(json, symbolize_names: true)
7
+ end
8
+
9
+ def count_successes
10
+ self.body[:success]
11
+ end
12
+
13
+ def has_successes?
14
+ self.count_successes > 0
15
+ end
16
+
17
+ def count_failures
18
+ self.body[:failure]
19
+ end
20
+
21
+ def has_failures?
22
+ self.count_failures > 0
23
+ end
24
+
25
+ def results
26
+ self.body[:results]
27
+ end
28
+
29
+ def errors
30
+ self.results.select { |result| result.key?(:error) }
31
+ end
32
+
33
+ def successes
34
+ self.results.select { |result| !result.key?(:error) }
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Rgcm
2
+ VERSION = "0.0.1"
3
+ end
data/lib/rgcm.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+
3
+ require 'active_support/core_ext'
4
+ require 'typhoeus'
5
+
6
+ require 'rgcm/config'
7
+ require 'rgcm/response'
8
+ require 'rgcm/request_builder'
9
+ require 'rgcm/message'
10
+ require 'rgcm/version'
11
+ require 'rgcm/railtie' if defined?(Rails)
12
+
13
+ module Rgcm
14
+
15
+ end
data/rgcm.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rgcm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rgcm"
8
+ spec.version = Rgcm::VERSION
9
+ spec.authors = ["Alexander Klaiber"]
10
+ spec.email = ["alex.klaiber@gmail.com"]
11
+ spec.summary = "Google Cloud Messaging"
12
+ spec.description = "Google Cloud Messaging"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency "activesupport"
26
+ spec.add_dependency "typhoeus"
27
+ end
@@ -0,0 +1,9 @@
1
+ {
2
+ "multicast_id":5030383832068975557,
3
+ "success":0,
4
+ "failure":1,
5
+ "canonical_ids":0,
6
+ "results":[
7
+ { "error":"NotRegistered" }
8
+ ]
9
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "multicast_id":5958304298192430760,
3
+ "success":3,
4
+ "failure":11,
5
+ "canonical_ids":1,
6
+ "results":[
7
+ { "message_id":"0:1403426982650227%62dfd0f6e116c072"},
8
+ { "error":"NotRegistered"},
9
+ { "error":"NotRegistered"},
10
+ { "error":"NotRegistered"},
11
+ { "error":"NotRegistered"},
12
+ { "error":"NotRegistered"},
13
+ { "error":"NotRegistered"},
14
+ { "error":"NotRegistered"},
15
+ { "error":"NotRegistered"},
16
+ {
17
+ "message_id":"0:1403426982651391%b930435be116c072",
18
+ "registration_id":"APA91bFgaPSr9eX0VKsgFT15bg8umQBftGGOVEsKjBm9KtLfU5pz1ytEZkCm7YKjXbpavurXSqMdh1Sviry4ZoXZIZmzLTDI2ti6mxFjuY4tNom5uO8VcvH2EjPu2mwFuZyfQomJpRmVsraCJWpyOzms5WMgrYIdQXb08UvOKKe72ByOcClxlAo"
19
+ },
20
+ { "error":"NotRegistered"},
21
+ { "error":"NotRegistered"},
22
+ { "error":"NotRegistered"},
23
+ { "message_id":"0:1403426982652703%b930435be116c072"}
24
+ ]
25
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "multicast_id":5030383832068975557,
3
+ "success":1,
4
+ "failure":0,
5
+ "canonical_ids":0,
6
+ "results":[
7
+ { "message_id":"0:1403425286258916%62dfd0f6e116c072" }
8
+ ]
9
+ }
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rgcm::Message do
4
+
5
+ let(:data) { {score: '4x8', time: '15:16.2342'} }
6
+
7
+ before do
8
+ Typhoeus.stub(Rgcm::Config::URI).and_return(Typhoeus::Response.new(code: 200, body: json))
9
+ end
10
+
11
+ let(:json) { File.open("#{FIXTURES_PATH}/responses/successfully.json").read }
12
+
13
+ it 'returns rgcm response' do
14
+ response = Rgcm::Message.new('TEST_API_KEY').post('test', 'test', data)
15
+
16
+ expect(response).to be_kind_of(Rgcm::Response)
17
+ end
18
+
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rgcm::RequestBuilder do
4
+
5
+ let(:api_key) { 'TEST_API_KEY' }
6
+ let(:registration_id) { 123 }
7
+ let(:collapse_key) { 'collapse key' }
8
+ let(:data) { {score: '4x8', time: '15:16.2342'} }
9
+ let(:request) { Rgcm::RequestBuilder.new(api_key, registration_id, collapse_key, data) }
10
+
11
+ it 'returns uri' do
12
+ expect(request.build.base_url).to eql(Rgcm::Config::URI)
13
+ end
14
+
15
+ it 'returns authorization' do
16
+ expect(request.build.options[:headers]['Authorization']).to eql("key=#{api_key}")
17
+ end
18
+
19
+ it 'returns content type' do
20
+ expect(request.build.options[:headers]['Content-Type']).to eql('application/json')
21
+ end
22
+
23
+ it 'returns registration_id' do
24
+ expect(json(request.build.options[:body])[:registration_ids]).to eql([registration_id])
25
+ end
26
+
27
+ it 'returns collapse_key' do
28
+ expect(json(request.build.options[:body])[:collapse_key]).to eql(collapse_key)
29
+ end
30
+
31
+ it 'returns data' do
32
+ expect(json(request.build.options[:body])[:data]).to eql(data)
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rgcm::Response do
4
+ let(:response) { Rgcm::Response.new(json) }
5
+
6
+ context 'with successfully response' do
7
+ let(:json) { File.open("#{FIXTURES_PATH}/responses/successfully.json").read }
8
+ it '#has_successes?' do
9
+ expect(response.has_successes?).to be_true
10
+ end
11
+ it '#has_failures?' do
12
+ expect(response.has_failures?).to be_false
13
+ end
14
+ it '#count_successes' do
15
+ expect(response.count_successes).to eql(1)
16
+ end
17
+ it '#has_failures?' do
18
+ expect(response.count_failures).to eql(0)
19
+ end
20
+ end
21
+
22
+ context 'with failure response' do
23
+ let(:json) { File.open("#{FIXTURES_PATH}/responses/failure.json").read }
24
+ it '#has_successes?' do
25
+ expect(response.has_successes?).to be_false
26
+ end
27
+ it '#has_failures?' do
28
+ expect(response.has_failures?).to be_true
29
+ end
30
+ it '#count_successes' do
31
+ expect(response.count_successes).to eql(0)
32
+ end
33
+ it '#has_failures?' do
34
+ expect(response.count_failures).to eql(1)
35
+ end
36
+ it '#errors' do
37
+ expect(response.errors.size).to eql(1)
38
+ end
39
+ end
40
+
41
+ context 'with failure response' do
42
+ let(:json) { File.open("#{FIXTURES_PATH}/responses/multi.json").read }
43
+ it '#has_successes?' do
44
+ expect(response.has_successes?).to be_true
45
+ end
46
+ it '#has_failures?' do
47
+ expect(response.has_failures?).to be_true
48
+ end
49
+ it '#count_successes' do
50
+ expect(response.count_successes).to eql(3)
51
+ end
52
+ it '#has_failures?' do
53
+ expect(response.count_failures).to eql(11)
54
+ end
55
+ it '#errors' do
56
+ expect(response.errors.size).to eql(11)
57
+ end
58
+ it '#successes' do
59
+ expect(response.successes.size).to eql(3)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ require 'support/json_helper'
2
+
3
+ require 'rgcm'
4
+
5
+ FIXTURES_PATH = "#{File.dirname(__FILE__)}/fixtures"
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+
13
+ config.include(JsonHelper)
14
+ end
@@ -0,0 +1,5 @@
1
+ module JsonHelper
2
+ def json(str)
3
+ JSON.parse(str, symbolize_names: true)
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rgcm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Klaiber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: typhoeus
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Google Cloud Messaging
84
+ email:
85
+ - alex.klaiber@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/rgcm.rb
98
+ - lib/rgcm/config.rb
99
+ - lib/rgcm/message.rb
100
+ - lib/rgcm/railtie.rb
101
+ - lib/rgcm/request_builder.rb
102
+ - lib/rgcm/response.rb
103
+ - lib/rgcm/version.rb
104
+ - rgcm.gemspec
105
+ - spec/fixtures/responses/failure.json
106
+ - spec/fixtures/responses/multi.json
107
+ - spec/fixtures/responses/successfully.json
108
+ - spec/rgcm/message_spec.rb
109
+ - spec/rgcm/request_builder_spec.rb
110
+ - spec/rgcm/response_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/support/json_helper.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Google Cloud Messaging
137
+ test_files:
138
+ - spec/fixtures/responses/failure.json
139
+ - spec/fixtures/responses/multi.json
140
+ - spec/fixtures/responses/successfully.json
141
+ - spec/rgcm/message_spec.rb
142
+ - spec/rgcm/request_builder_spec.rb
143
+ - spec/rgcm/response_spec.rb
144
+ - spec/spec_helper.rb
145
+ - spec/support/json_helper.rb