android-services 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.1.5"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "simplecov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.8)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ git (1.2.5)
8
+ i18n (0.6.0)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.7.4)
15
+ multi_json (1.3.6)
16
+ rake (0.9.2.2)
17
+ rdoc (3.12)
18
+ json (~> 1.4)
19
+ shoulda (3.1.1)
20
+ shoulda-context (~> 1.0)
21
+ shoulda-matchers (~> 1.2)
22
+ shoulda-context (1.0.0)
23
+ shoulda-matchers (1.2.0)
24
+ activesupport (>= 3.0.0)
25
+ simplecov (0.6.4)
26
+ multi_json (~> 1.0)
27
+ simplecov-html (~> 0.5.3)
28
+ simplecov-html (0.5.3)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.1.5)
35
+ jeweler (~> 1.8.4)
36
+ rdoc (~> 3.12)
37
+ shoulda
38
+ simplecov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 krishna rokhale
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ ## android-services
2
+
3
+ Android services gem is a ruby wrapper over the google services for android.
4
+
5
+ ### QuickStart
6
+ You can install the gem using
7
+ gem install android-services
8
+ You will need a google API key for communicating with the google services, which you can get from here
9
+ http://developer.android.com/guide/google/gcm/gs.html
10
+
11
+ After getting the API key, you can initialize it in the rails app using an initializer file.
12
+
13
+ AndroidServices.configure do |config|
14
+ config.api_key = '<API KEY>'
15
+ config.secure = true
16
+ end
17
+
18
+ ### Sending a simple message
19
+
20
+ Sending a message to an Android device is easy! The simplest message you can send is:
21
+
22
+ CloudMessage.new(:registration_ids => [])
23
+
24
+ Other Options you might want to use:
25
+
26
+ + collapse_key: An arbitrary string (such as "Updates Available") that is used to collapse a group of like messages when the device is offline, so that only the last message gets sent to the client. This is intended to avoid sending too many messages to the phone when it comes back online.
27
+
28
+ + data: A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name.
29
+
30
+ + delay_while_idle: If included, indicates that the message should not be sent immediately if the device is idle. The server will wait for the device to become active, and then only the last message for each collapse_key value will be sent.
31
+
32
+ + time_to_live: How long (in seconds) the message should be kept on GCM storage if the device is offline.
33
+
34
+ ### Contributing to android-services
35
+
36
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
37
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
38
+ * Fork the project.
39
+ * Start a feature/bugfix branch.
40
+ * Commit and push until you are happy with your contribution.
41
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
42
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
43
+
44
+ ### Copyright
45
+
46
+ Copyright (c) 2012 krishna rokhale. See LICENSE.txt for
47
+ further details.
48
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "android-services"
18
+ gem.homepage = "http://github.com/krokhale/android-services"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Ruby wrapper for the google android services.}
21
+ gem.description = %Q{The gem aims to provide an API for google services including google play which interact with Android devices.}
22
+ gem.email = "krishna.rokhale@gmail.com"
23
+ gem.authors = ["krishna rokhale"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ # require 'require/rcovtask'
36
+ # Rcov::RcovTask.new do |test|
37
+ # test.libs << 'test'
38
+ # test.pattern = 'test/**/test_*.rb'
39
+ # test.verbose = true
40
+ # test.rcov_opts << '--exclude "gems/*"'
41
+ # end
42
+
43
+ task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "android-services #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "android-services"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["krishna rokhale"]
12
+ s.date = "2012-08-20"
13
+ s.description = "The gem aims to provide an API for google services including google play which interact with Android devices."
14
+ s.email = "krishna.rokhale@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "android-services.gemspec",
28
+ "lib/android_services.rb",
29
+ "lib/android_services/cloud_message/cloud_message.rb",
30
+ "lib/android_services/cloud_message/request.rb",
31
+ "lib/android_services/cloud_message/response.rb",
32
+ "lib/android_services/configuration.rb",
33
+ "lib/android_services/exceptions.rb",
34
+ "test/helper.rb",
35
+ "test/test_android_services.rb"
36
+ ]
37
+ s.homepage = "http://github.com/krokhale/android-services"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.24"
41
+ s.summary = "Ruby wrapper for the google android services."
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.5"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
51
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.1.5"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
57
+ s.add_dependency(%q<simplecov>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<shoulda>, [">= 0"])
61
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
62
+ s.add_dependency(%q<bundler>, ["~> 1.1.5"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
64
+ s.add_dependency(%q<simplecov>, [">= 0"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,59 @@
1
+ module AndroidServices
2
+ module GoogleCloudMessaging
3
+ class CloudMessage
4
+ attr_accessor :options
5
+
6
+
7
+ def initialize(options)
8
+ validating options
9
+ [ :registration_ids,
10
+ :dry_run,
11
+ :collapse_key,
12
+ :data,
13
+ :delay_while_idle,
14
+ :time_to_live].each do |option|
15
+ instance_variable_set("@#{option}", options[option])
16
+ end
17
+ end
18
+
19
+ def send!
20
+ build_request
21
+ post_request!
22
+ end
23
+
24
+
25
+ private
26
+
27
+ def validating options
28
+ raise AndroidServices::Exceptions::RegistrationIdBlank.new("Atleast one registration id required to send message.") if options[:registration_ids].nil?
29
+ raise AndroidServices::Exceptions::RegistrationIdLimit.new("You cannot define more than 1000 registration ids.") if options[:registration_ids].size > 1000
30
+ end
31
+
32
+ def build_request
33
+ instance_variable_set("@request", AndroidServices::GoogleCloudMessaging::Request.new(payload))
34
+ self.class.instance_eval do
35
+ define_method(:request) { @request }
36
+ end
37
+ end
38
+
39
+ def payload
40
+ params = instance_variables_hash.select {|k,v| ["registration_ids", "dry_run", "collapse_key", "data", "delay_while_idle", "time_to_live"].include?(k) && !v.nil?}
41
+ params.to_json
42
+ end
43
+
44
+ def post_request!
45
+ response_object = @request.send :post!
46
+ instance_variable_set("@response", response_object)
47
+ self.class.instance_eval do
48
+ define_method(:response) { @request }
49
+ end
50
+ @response
51
+ end
52
+
53
+ def instance_variables_hash
54
+ Hash[instance_variables.map { |name| [name.to_s.gsub("@",""), instance_variable_get(name)] } ]
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ module AndroidServices
2
+ module GoogleCloudMessaging
3
+ class Request
4
+
5
+ def initialize payload
6
+ @payload ||= payload
7
+ end
8
+
9
+ private
10
+
11
+ def post!
12
+ handle post_to uri
13
+ end
14
+
15
+ def handle response
16
+ AndroidServices::GoogleCloudMessaging::Response.new(response)
17
+ end
18
+
19
+ def uri
20
+ URI(AndroidServices.configuration.messaging_endpoint)
21
+ end
22
+
23
+ def post_to uri
24
+ Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
25
+ req = Net::HTTP::Post.new(uri.path)
26
+ req.body = @payload
27
+ req['Content-Type'] = AndroidServices.configuration.content_type
28
+ req["Authorization"] = "key=#{AndroidServices.configuration.api_key}"
29
+ http.request(req)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ module AndroidServices
2
+ module GoogleCloudMessaging
3
+ class Response
4
+
5
+ def initialize response
6
+ unless response.body.nil?
7
+ @body ||= response.body
8
+ instantiate_attributes
9
+ end
10
+ @status ||= response.code
11
+ eval_response
12
+ end
13
+
14
+ private
15
+
16
+ def instantiate_attributes
17
+ JSON.parse(@body).each do |attr, value|
18
+ instance_variable_set("@#{attr}", value)
19
+ end
20
+ var_hash = instance_variables_hash
21
+ self.class.instance_eval do
22
+ var_hash.each do |var, value|
23
+ define_method(var) { value }
24
+ end
25
+ end
26
+ end
27
+
28
+ def instance_variables_hash
29
+ Hash[instance_variables.map { |name| [name.to_s.gsub("@",""), instance_variable_get(name)] } ]
30
+ end
31
+
32
+ def eval_response
33
+ if @status.eql? 200
34
+ true
35
+ elsif @status.eql? 400
36
+ "STATUS:400 Request could not be parsed as JSON."
37
+ elsif @status.eql? 401
38
+ "STATUS:401 There was an error authenticating the sender account."
39
+ elsif @status.eql? 500
40
+ "STATUS:500 There was an internal error in the GCM server while trying to process the request."
41
+ elsif @status.eql? 500
42
+ "STATUS:503 The GCM server took too long to process the request."
43
+ # TODO Implement exponential backoff algorithm to retry sending.
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ module AndroidServices
2
+ class Configuration
3
+ OPTIONS = [:api_key, :messaging_endpoint, :secure, :port, :protocol].freeze
4
+ attr_accessor :api_key
5
+
6
+ attr_accessor :messaging_endpoint
7
+
8
+ attr_accessor :secure
9
+
10
+ attr_accessor :port
11
+
12
+ attr_accessor :content_type
13
+
14
+ alias_method :secure?, :secure
15
+
16
+ def initialize
17
+ @secure = true
18
+ @messaging_endpoint = "#{protocol}://android.googleapis.com/gcm/send"
19
+ @content_type = 'application/json'
20
+ end
21
+
22
+ def port
23
+ @port || default_port
24
+ end
25
+
26
+ def protocol
27
+ if secure?
28
+ 'https'
29
+ else
30
+ 'http'
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def default_port
37
+ if secure?
38
+ 443
39
+ else
40
+ 80
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ module AndroidServices
2
+ module Exceptions
3
+ class RegistrationIdBlank < StandardError; end
4
+ class RegistrationIdLimit < StandardError; end
5
+ end
6
+ end
@@ -0,0 +1,34 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ require 'android_services/cloud_message/cloud_message'
7
+ require 'android_services/cloud_message/request'
8
+ require 'android_services/cloud_message/response'
9
+
10
+ require 'android_services/configuration'
11
+ require 'android_services/exceptions'
12
+
13
+
14
+ module AndroidServices
15
+
16
+ class << self
17
+ attr_writer :configuration
18
+
19
+ # @example
20
+ # AndroidServices.configure do |config|
21
+ # config.api_key = '1232fsd23asd'
22
+ # config.secure = true
23
+ # end
24
+ def configure
25
+ yield(configuration)
26
+ end
27
+
28
+ def configuration
29
+ @configuration ||= Configuration.new
30
+ end
31
+
32
+ end
33
+
34
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'android-services'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAndroidServices < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: android-services
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - krishna rokhale
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.1.5
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.5
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: The gem aims to provide an API for google services including google play
95
+ which interact with Android devices.
96
+ email: krishna.rokhale@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.md
102
+ files:
103
+ - .document
104
+ - Gemfile
105
+ - Gemfile.lock
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - android-services.gemspec
111
+ - lib/android_services.rb
112
+ - lib/android_services/cloud_message/cloud_message.rb
113
+ - lib/android_services/cloud_message/request.rb
114
+ - lib/android_services/cloud_message/response.rb
115
+ - lib/android_services/configuration.rb
116
+ - lib/android_services/exceptions.rb
117
+ - test/helper.rb
118
+ - test/test_android_services.rb
119
+ homepage: http://github.com/krokhale/android-services
120
+ licenses:
121
+ - MIT
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ segments:
133
+ - 0
134
+ hash: 4052475533325376823
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.24
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: Ruby wrapper for the google android services.
147
+ test_files: []