epns_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.project +12 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/epns_client.gemspec +26 -0
- data/example/client.rb +27 -0
- data/lib/epns_client.rb +99 -0
- data/lib/epns_client/version.rb +3 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bec014fc61af1d80e16b28984d424c223c4ff23a
|
4
|
+
data.tar.gz: 2e016b2507e8aa2dd154ff1037e733b7b692223f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc28945f4dfcfb7f47e64e179ee59f29c0c7c3ec3c74903dd181db73a46797388409961b334660f18eb0cd407c30b7bf83282de32e9bc2935fe9a7337ed2b3ee
|
7
|
+
data.tar.gz: ef135f7ae7af4aaaa837c43e8b503593d1bb92bc16cd06297d94a96e429a503e2519da5b7bce12dd8d518b95bdfc607fc9b7794d16f65f4027488298a4e06222
|
data/.gitignore
ADDED
data/.project
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<projectDescription>
|
3
|
+
<name>epns_client</name>
|
4
|
+
<comment></comment>
|
5
|
+
<projects>
|
6
|
+
</projects>
|
7
|
+
<buildSpec>
|
8
|
+
</buildSpec>
|
9
|
+
<natures>
|
10
|
+
<nature>com.aptana.ruby.core.rubynature</nature>
|
11
|
+
</natures>
|
12
|
+
</projectDescription>
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
EPNs-client
|
2
|
+
===========
|
3
|
+
|
4
|
+
##What is it?
|
5
|
+
|
6
|
+
EPNs用のrubyクライアントです。詳細は[EPNs](http://epns.flum.pw)
|
7
|
+
|
8
|
+
動作確認はruby 2.0.0p481及びruby 2.1.0ですがruby 1.9.3でも動くと思います。
|
9
|
+
|
10
|
+
##How to Use
|
11
|
+
Rubyのインストールは省きます。
|
12
|
+
準備として
|
13
|
+
```
|
14
|
+
gem install epns_client
|
15
|
+
```
|
16
|
+
を実行してください
|
17
|
+
|
18
|
+
まず、[EPNsのトップページ](http://epns.flum.pw/)でユーザー登録をし、api_keyを取得してください。
|
19
|
+
必要なデータをクライアントに指定すれば接続できます。
|
20
|
+
example/にデモ用のclient.rbと説明が書いてるので参考にしてください。
|
21
|
+
|
22
|
+
|
23
|
+
質問等ありましたらTwitter:[@flum_](https://twitter.com/flum_)までお願いします。
|
24
|
+
|
25
|
+
##License
|
26
|
+
|
27
|
+
The MIT License
|
28
|
+
|
29
|
+
-------
|
30
|
+
(c) @2015 flum_
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "epns_client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/epns_client.gemspec
ADDED
@@ -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 'epns_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "epns_client"
|
8
|
+
spec.version = EpnsClient::VERSION
|
9
|
+
spec.authors = ["flum1025"]
|
10
|
+
spec.email = ["flum.1025@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{EPNs API Library}
|
13
|
+
spec.description = %q{This Library connects to EPNs Server and provides websocket connection.}
|
14
|
+
spec.homepage = "https://github.com/flum1025/EPNs-client"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
|
24
|
+
spec.add_dependency 'json'
|
25
|
+
spec.add_dependency 'websocket-client-simple'
|
26
|
+
end
|
data/example/client.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'epns_client'
|
2
|
+
|
3
|
+
#$host = "http://localhost:3000"
|
4
|
+
|
5
|
+
#api_keyの指定
|
6
|
+
api_key = "3WqjXzfcw8yUea02BNrKA154YvItsn6LipCJSukDTxoHM9hEF" #ダミー
|
7
|
+
|
8
|
+
#registration_idの指定 EpnsClient.sendする場合は必須
|
9
|
+
#registration_id = "HkKTJrZCLlGFYMcAqtR7fg2u5dUnz1wBIO6vQN4xm9i0bey38PaDXESVsopjhW" #ダミー
|
10
|
+
|
11
|
+
#新しいregistration_idの取得
|
12
|
+
registration_id = EpnsClient.register(api_key)
|
13
|
+
|
14
|
+
#サーバー側から新しいpushがあった場合にブロック内が実行される
|
15
|
+
EpnsClient.connect(api_key, registration_id) do |object|
|
16
|
+
puts object
|
17
|
+
end
|
18
|
+
|
19
|
+
#クライアントにデータをpushする
|
20
|
+
sleep(3) #connect直後だとsendしてもfailureになる
|
21
|
+
p EpnsClient.send([registration_id], api_key, {:test=>123})
|
22
|
+
|
23
|
+
#Threadで動いているので終わらない処理を書かないと終了する
|
24
|
+
loop do
|
25
|
+
STDIN.gets.strip
|
26
|
+
end
|
27
|
+
|
data/lib/epns_client.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require "epns_client/version"
|
2
|
+
|
3
|
+
require 'websocket-client-simple'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'json'
|
6
|
+
require 'net/http'
|
7
|
+
|
8
|
+
$host = "http://epns.flum.pw"
|
9
|
+
$ssl = false
|
10
|
+
|
11
|
+
module EpnsClient
|
12
|
+
class EpnsClientError < StandardError; end
|
13
|
+
|
14
|
+
def self.connect(api_key, registration_id, &blk)
|
15
|
+
::EpnsClient::Client.new(Connection.builder(api_key, registration_id), blk)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.register(api_key)
|
19
|
+
::EpnsClient::Connection.register(api_key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.send(ids, api_key, options={})
|
23
|
+
::EpnsClient::Connection.send_notification(ids, api_key, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
class Connection
|
27
|
+
def self.builder(api_key, reg_id)
|
28
|
+
params = Hash.new
|
29
|
+
params.store('api_key', api_key)
|
30
|
+
params.store('registration_id', reg_id)
|
31
|
+
json = post(params, File.join($host, 'weset/websocket/connect'))
|
32
|
+
error?(json)
|
33
|
+
return json
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.register(api_key)
|
37
|
+
params = Hash.new
|
38
|
+
params.store('api_key', api_key)
|
39
|
+
json = post(params, File.join($host, 'weset/websocket/regist'))
|
40
|
+
error?(json)
|
41
|
+
return json['data']['registration_id']
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.send_notification(ids, api_key, options)
|
45
|
+
params = Hash.new
|
46
|
+
params.store('api_key', api_key)
|
47
|
+
params.store('registration_ids', ids)
|
48
|
+
params.store('options', options.to_json)
|
49
|
+
json = post(params, File.join($host, 'weset/websocket/send_notification'))
|
50
|
+
error?(json)
|
51
|
+
return json
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.error?(json)
|
55
|
+
return if json['error'].nil?
|
56
|
+
raise EpnsClientError, "#{json['error']['message']}:#{json['error']['code']}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.post(params, url)
|
60
|
+
uri = URI.parse(url)
|
61
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
62
|
+
http.use_ssl = $ssl
|
63
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
64
|
+
request.set_form_data(params)
|
65
|
+
response = nil
|
66
|
+
http.start do |h|
|
67
|
+
response = h.request(request).body
|
68
|
+
end
|
69
|
+
json = JSON.parse(response)
|
70
|
+
return json
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class Client
|
75
|
+
def initialize(opt, blk)
|
76
|
+
connect("#{opt['data']['url']}:#{opt['data']['port']}", opt['data']['req_body'], blk)
|
77
|
+
end
|
78
|
+
|
79
|
+
def connect(url, req_body, blk)
|
80
|
+
ws = WebSocket::Client::Simple.connect url
|
81
|
+
|
82
|
+
ws.on :message do |msg|
|
83
|
+
blk.call(msg.data)
|
84
|
+
end
|
85
|
+
|
86
|
+
ws.on :open do
|
87
|
+
ws.send req_body.to_json
|
88
|
+
end
|
89
|
+
|
90
|
+
ws.on :close do |e|
|
91
|
+
puts e
|
92
|
+
end
|
93
|
+
|
94
|
+
ws.on :error do |e|
|
95
|
+
puts e
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: epns_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- flum1025
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-11 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
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: websocket-client-simple
|
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: This Library connects to EPNs Server and provides websocket connection.
|
70
|
+
email:
|
71
|
+
- flum.1025@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .project
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- epns_client.gemspec
|
85
|
+
- example/client.rb
|
86
|
+
- lib/epns_client.rb
|
87
|
+
- lib/epns_client/version.rb
|
88
|
+
homepage: https://github.com/flum1025/EPNs-client
|
89
|
+
licenses: []
|
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.5.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: EPNs API Library
|
111
|
+
test_files: []
|