adm 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a49c485c23dff5ac192e05b0157327ca3b682ace
4
+ data.tar.gz: efa59a5cb73a2eaa9c546c2a70a7364601791237
5
+ SHA512:
6
+ metadata.gz: 102440a744a83c79a450ec4c5058b4f50126dedc48e35e00b788b7d5f8c5a3515b634ae621eafe36edd96d0872f14aa8121f146015a5e74da3e8023c10d3369b
7
+ data.tar.gz: 2d58eae638a01e5763a862afc8ef80a30cc687bad59966bd61d2fafcde4685eed80cf8a42103ddda185430180515458ed60f4aca294d8c7dd92d40c57f369e36
@@ -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/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in adm.gemspec
4
+ gemspec
5
+ gem 'oj'
6
+ gem 'looksee'
7
+ gem 'wirb'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tal Atlas
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.
@@ -0,0 +1,29 @@
1
+ # Adm
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'adm'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install adm
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'adm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "adm"
8
+ spec.version = ADM::VERSION
9
+ spec.authors = ["Tal Atlas"]
10
+ spec.email = ["me@tal.by"]
11
+ spec.description = %q{Amazon Device Messaging Gem}
12
+ spec.summary = %q{Send messages via the Amazon Device Messaging (ADM) service.}
13
+ spec.homepage = "https://github.com/tal/adm"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3.5"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'typhoeus'
25
+ spec.add_dependency 'multi_json'
26
+ end
@@ -0,0 +1,28 @@
1
+ require 'yaml'
2
+ require 'typhoeus'
3
+ require 'multi_json'
4
+ require "adm/version"
5
+ require "adm/access_token"
6
+ require "adm/message"
7
+ require "adm/notification"
8
+
9
+ module ADM
10
+ extend self
11
+ attr_writer :message_url, :client_id, :client_secret
12
+
13
+ def config
14
+ if File.exists?('./config/adm.yml')
15
+ YAML.load_file('./config/adm.yml')
16
+ else
17
+ {}
18
+ end
19
+ end
20
+
21
+ def client_id
22
+ @client_id ||= config['client_id']
23
+ end
24
+
25
+ def client_secret
26
+ @client_secret ||= config['client_secret']
27
+ end
28
+ end
@@ -0,0 +1,62 @@
1
+ class ADM::AccessToken
2
+ attr_reader :client_id, :client_secret
3
+ attr_reader :token
4
+
5
+ def initialize opts
6
+ @token = opts['access_token']
7
+ @expires_at = Time.now+opts['expires_in'].to_i
8
+ @type = opts['token_type']
9
+ @scope = opts['scope']
10
+ end
11
+
12
+ def to_s
13
+ token
14
+ end
15
+
16
+ def valid?
17
+ @expires_at < Time.now
18
+ end
19
+
20
+ def self.fetch client_id=nil, client_secret=nil
21
+ client_id ||= ADM.client_id
22
+ client_secret ||= ADM.client_secret
23
+ data = request_data client_id, client_secret
24
+
25
+ if data['error']
26
+ raise TokenGetError, response.body
27
+ end
28
+
29
+ new(data)
30
+ end
31
+
32
+ def self.request_data client_id, client_secret
33
+ request = Typhoeus::Request.new(
34
+ 'http://api.amazon.com/auth/O2/token',
35
+ method: :post,
36
+ body: {
37
+ grant_type: :'client_credentials',
38
+ scope: :'messaging:push',
39
+ client_id: client_id,
40
+ client_secret: client_secret
41
+ },
42
+ headers: {
43
+ :Accept => :'application/json'
44
+ }
45
+ )
46
+ response = request.run
47
+
48
+ MultiJson.load(response.body)
49
+ end
50
+
51
+ def self.get_token
52
+ if @token && @token.valid?
53
+ return @token.token
54
+ end
55
+
56
+ @token = fetch
57
+
58
+ @token.token
59
+ end
60
+
61
+ class TokenGetError < StandardError; end
62
+ end
@@ -0,0 +1,31 @@
1
+ class ADM::Message
2
+ attr_reader :compiled,:data
3
+ attr_writer :hydra
4
+
5
+ def initialize data={}
6
+ @data = data
7
+ end
8
+
9
+ def send_to *tokens
10
+ if tokens.first.is_a?(Array)
11
+ tokens = tokens.first
12
+ end
13
+
14
+ tokens.each do |token|
15
+ ADM::Notification.new(token,self).queue(hydra)
16
+ end
17
+
18
+ hydra.run
19
+ end
20
+
21
+ def hydra
22
+ @hydra ||= Typhoeus::Hydra.hydra
23
+ end
24
+
25
+ def compile
26
+ @json ||= begin
27
+ @compiled = true
28
+ MultiJson.dump(data)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ class ADM::Notification
2
+ attr_reader :registration_id, :message
3
+
4
+ def initialize registration_id, message
5
+ if message.is_a?(Message)
6
+ @message = message
7
+ else
8
+ @message = Message.new(message)
9
+ end
10
+
11
+ @registration_id = registration_id
12
+ end
13
+
14
+ def token
15
+ ADM::AccessToken.get_token
16
+ end
17
+
18
+ def request
19
+ @request ||= Typhoeus::Request.new(
20
+ "https://api.amazon.com/messaging/registrations/#{registration_id}/messages",
21
+ method: :post,
22
+ body: message.compile,
23
+ headers: {
24
+ :Accept => :'application/json',
25
+ :'Content-Type' => :'application/json',
26
+ :'X-Amzn-Type-Version' => :'com.amazon.device.messaging.ADMMessage@1.0',
27
+ :'X-Amzn-Accept-Type' => :'com.amazon.device.messaging.ADMSendResult@1.0',
28
+ :Authorization => "Bearer #{token}"
29
+ }
30
+ )
31
+ end
32
+
33
+ def send
34
+ request.run
35
+ end
36
+
37
+ def queue hydra
38
+ hydra.queue(self)
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ADM
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tal Atlas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-09 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.3.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.3.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: typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: multi_json
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
+ description: Amazon Device Messaging Gem
70
+ email:
71
+ - me@tal.by
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - adm.gemspec
82
+ - lib/adm.rb
83
+ - lib/adm/access_token.rb
84
+ - lib/adm/message.rb
85
+ - lib/adm/notification.rb
86
+ - lib/adm/version.rb
87
+ homepage: https://github.com/tal/adm
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Send messages via the Amazon Device Messaging (ADM) service.
111
+ test_files: []