jeapie 0.1.1 → 0.2.0
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.
- data/README.md +14 -6
- data/jeapie.gemspec +3 -1
- data/lib/jeapie.rb +69 -20
- data/lib/jeapie/version.rb +1 -1
- data/spec/lib/jeapie_spec.rb +42 -4
- data/whatsnew.md +4 -0
- metadata +2 -4
data/README.md
CHANGED
@@ -6,7 +6,7 @@ It allow send push notification to your Android and Apple devices
|
|
6
6
|
## Installation
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
$ gem 'jeapie'
|
9
|
+
$ gem 'jeapie', '~> 0.2'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -17,6 +17,7 @@ Or install it yourself as:
|
|
17
17
|
$ gem install jeapie
|
18
18
|
|
19
19
|
## Usage
|
20
|
+
optional:
|
20
21
|
```ruby
|
21
22
|
require 'jeapie'
|
22
23
|
```
|
@@ -24,25 +25,32 @@ require 'jeapie'
|
|
24
25
|
To send with the very minimum amount of information.
|
25
26
|
|
26
27
|
```ruby
|
27
|
-
|
28
|
+
# sending to myself
|
29
|
+
Jeapie.notify(message: 'message', title: 'title', token: 'APP_TOKEN')
|
30
|
+
#sending to all subscribers of your application (providers)
|
31
|
+
Jeapie.notify_all(message: 'message', title: 'title', token: 'APP_TOKEN')
|
32
|
+
#sending to some subscribers of your application
|
33
|
+
Jeapie.notify_multiple(message: 'message', title: 'title', token: 'APP_TOKEN', emails:'user1@mail.com, user2@mail.com')
|
28
34
|
```
|
29
35
|
|
30
36
|
Optional you can place in /config/application.rb
|
31
37
|
```ruby
|
32
38
|
Jeapie.configure do |config|
|
33
|
-
config.
|
34
|
-
config.token='APP_TOKEN'
|
39
|
+
config.token='APP_TOKEN' # you can take from http://dashboard.jeapie.com on section "Providers"
|
35
40
|
config.device='Nexus7' #optional
|
36
41
|
config.priority=0 #or 1(high) or -1(low, not sound when receive). By default is 0
|
37
42
|
end
|
38
43
|
|
39
|
-
Jeapie.notify(message: 'message', title: 'title')
|
40
|
-
#or just
|
41
44
|
Jeapie.notify(message: 'message')
|
45
|
+
#or
|
46
|
+
Jeapie.notify(message: 'message', title: 'title', priority:Jeapie::PRIORITY_LOW)
|
42
47
|
```
|
43
48
|
Method `notify` return true or false. If it return false you can check `Jeapie.errors` for error message.
|
44
49
|
Or you can use method `notify!`, it raise exception if something going wrong.
|
45
50
|
|
51
|
+
## Migrating from v0.1 to 0.2
|
52
|
+
Just delete ``config.token`` from ``/config/application.rb``
|
53
|
+
|
46
54
|
## Contributing
|
47
55
|
|
48
56
|
1. Fork it
|
data/jeapie.gemspec
CHANGED
@@ -22,8 +22,10 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.add_development_dependency 'fakeweb', ['~> 1.3']
|
23
23
|
|
24
24
|
# documentation
|
25
|
+
#yardoc --line-numbers --inline-source --title Jeapie --main README.md --encoding=UTF-8
|
26
|
+
#--no-private --protected
|
25
27
|
gem.extra_rdoc_files = Dir.glob('*.md')
|
26
|
-
gem.rdoc_options = %w(--line-numbers --inline-source --
|
28
|
+
gem.rdoc_options = %w(--line-numbers --inline-source --title Jeapie --main README.md --encoding=UTF-8)
|
27
29
|
gem.has_rdoc = 'yard'
|
28
30
|
|
29
31
|
end
|
data/lib/jeapie.rb
CHANGED
@@ -8,12 +8,14 @@ module Jeapie
|
|
8
8
|
class DeliverException < StandardError ; end
|
9
9
|
extend self
|
10
10
|
# app_token and user_key, you can get it from dashboard.jeapie.com
|
11
|
-
attr_accessor :token
|
12
|
-
attr_accessor :message, :title, :device, :priority
|
11
|
+
attr_accessor :token
|
12
|
+
attr_accessor :message, :title, :device, :priority, :emails
|
13
13
|
# After call notify it contain result as {Net::HTTPCreated} object, for check result better see {#errors}
|
14
14
|
attr_reader :result
|
15
15
|
|
16
|
-
|
16
|
+
NOTIFY_API_URL= 'https://api.jeapie.com/v2/personal/send/message.json'
|
17
|
+
NOTIFY_ALL_API_URL= 'https://api.jeapie.com/v2/broadcast/send/message.json'
|
18
|
+
NOTIFY_MULTIPLE_API_URL= 'https://api.jeapie.com/v2/users/send/message.json'
|
17
19
|
PRIORITY_LOW=-1
|
18
20
|
PRIORITY_MED=0
|
19
21
|
PRIORITY_HIGH=1
|
@@ -24,7 +26,7 @@ module Jeapie
|
|
24
26
|
parameters
|
25
27
|
end
|
26
28
|
|
27
|
-
# available parameters and
|
29
|
+
# available parameters and its values
|
28
30
|
def parameters
|
29
31
|
h = {}
|
30
32
|
keys.each { |k| h[k.to_sym] = Jeapie.instance_variable_get("@#{k}") }
|
@@ -32,10 +34,15 @@ module Jeapie
|
|
32
34
|
end
|
33
35
|
alias_method :params, :parameters
|
34
36
|
|
37
|
+
# for backward compatibility
|
38
|
+
def user=(val)
|
39
|
+
puts 'DEPRECATED: parameter "Jeapie.user" is obsolete. You can delete it from your config.'
|
40
|
+
end
|
41
|
+
|
35
42
|
# List of available fields, useful for unit test.
|
36
43
|
# @return Hash
|
37
44
|
def keys
|
38
|
-
@keys||= [:token, :
|
45
|
+
@keys||= [:token, :message, :title, :device, :priority]
|
39
46
|
end
|
40
47
|
|
41
48
|
# Clear stored params, useful for unit tests.
|
@@ -59,45 +66,87 @@ module Jeapie
|
|
59
66
|
"code: #{result.code}, errors: #{arr['errors']}"
|
60
67
|
end
|
61
68
|
|
62
|
-
# Send message to Jeapie.
|
69
|
+
# Send message to one user through Jeapie.
|
63
70
|
# example:
|
64
71
|
# notify message:'Backup complete'
|
65
72
|
# or:
|
66
|
-
# notify title:'Backup complete', message:'Time elapsed 50s, size: 500Mb', priority:-1, device:'Nexus7',
|
73
|
+
# notify title:'Backup complete', message:'Time elapsed 50s, size: 500Mb', priority:-1, device:'Nexus7', token:''
|
67
74
|
#
|
68
75
|
# If this method return +false+, you can check {#errors} for text, or you can use {#notify!} for raise +DeliverException+ if any problem
|
69
76
|
# @return [true, false]
|
70
77
|
def notify(opts={message:''})
|
71
|
-
|
72
|
-
|
73
|
-
url = URI.parse(API_URL)
|
74
|
-
req = Net::HTTP::Post.new(url.path, {'User-Agent' => "Ruby jeapie gem: #{Jeapie::VERSION}"})
|
75
|
-
req.set_form_data(data)
|
78
|
+
send_notify(NOTIFY_API_URL, opts)
|
79
|
+
end
|
76
80
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
81
|
+
# Send message to all subscribers of application.
|
82
|
+
# example:
|
83
|
+
# notify_all message:'Backup complete'
|
84
|
+
# or:
|
85
|
+
# notify_all title:'Backup complete', message:'Time elapsed 50s, size: 500Mb', priority:-1, device:'Nexus7', token:''
|
86
|
+
#
|
87
|
+
# If this method return +false+, you can check {#errors} for text, or you can use {#notify_all!} for raise +DeliverException+ if any problem
|
88
|
+
# @return [true, false]
|
89
|
+
def notify_all(opts={message:''})
|
90
|
+
send_notify(NOTIFY_ALL_API_URL, opts)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Send message to some subscribers of application.
|
94
|
+
# example:
|
95
|
+
# notify_all message:'Backup complete', emails:['user1@mail.com', 'user3@mail.com']
|
96
|
+
# notify_all message:'Backup complete', emails:'user1@mail.com, user3@mail.com'
|
97
|
+
#
|
98
|
+
# If this method return +false+, you can check {#errors} for text, or you can use {#notify_multiple!} for raise +DeliverException+ if any problem
|
99
|
+
# @return [true, false]
|
100
|
+
def notify_multiple(opts={message:'', emails:[]})
|
101
|
+
send_notify(NOTIFY_MULTIPLE_API_URL, opts)
|
82
102
|
end
|
83
103
|
|
104
|
+
# Similar to {#notify}, but raise +DeliverException+ if any problem
|
84
105
|
# @return [true]
|
85
106
|
def notify!(opts={message:''})
|
86
107
|
raise DeliverException, errors unless notify(opts)
|
87
108
|
true
|
88
109
|
end
|
89
110
|
|
111
|
+
# Similar to {#notify_all}, but raise +DeliverException+ if any problem
|
112
|
+
# @return [true]
|
113
|
+
def notify_all!(opts={message:''})
|
114
|
+
raise DeliverException, errors unless notify_all(opts)
|
115
|
+
true
|
116
|
+
end
|
117
|
+
|
118
|
+
# Similar to {#notify_multiple}, but raise +DeliverException+ if any problem
|
119
|
+
# @return [true]
|
120
|
+
def notify_multiple!(opts={message:'', emails:[]})
|
121
|
+
raise DeliverException, errors unless notify_multiple(opts)
|
122
|
+
true
|
123
|
+
end
|
124
|
+
|
90
125
|
protected
|
91
|
-
def params_errors(params)
|
126
|
+
def params_errors(params, api_url)
|
92
127
|
@params_errors=[]
|
93
128
|
@params_errors<<'Token cannot be blank' if params[:token].blank?
|
94
129
|
@params_errors<<'Token must be 32 symbols' if params[:token].size != 32
|
95
|
-
@params_errors<<'User cannot be blank' if params[:user].blank?
|
96
|
-
@params_errors<<'User must be 32 symbols' if params[:user].size != 32
|
97
130
|
@params_errors<<'Message cannot be blank' if params[:message].blank?
|
131
|
+
@params_errors<<'Emails cannot be blank' if params[:emails].blank? && api_url==NOTIFY_MULTIPLE_API_URL
|
98
132
|
@params_errors<<"Message too long, max: #{MESSAGE_MAX_LEN}" if params[:message] && params[:message].size > MESSAGE_MAX_LEN
|
99
133
|
@params_errors
|
100
134
|
end
|
101
135
|
|
136
|
+
def send_notify(api_url, options={})
|
137
|
+
data = params.merge(options).select { |_, v| v != nil }
|
138
|
+
data[:emails]=data[:emails].join(',') if !data[:emails].blank? && data[:emails].is_a?(Array)
|
139
|
+
return false unless params_errors(data, api_url).empty?
|
140
|
+
url = URI.parse(api_url)
|
141
|
+
req = Net::HTTP::Post.new(url.path, {'User-Agent' => "Ruby jeapie gem: #{Jeapie::VERSION}"})
|
142
|
+
req.set_form_data(data)
|
143
|
+
|
144
|
+
res = Net::HTTP.new(url.host, url.port)
|
145
|
+
res.use_ssl = true
|
146
|
+
res.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
147
|
+
@result= res.start {|http| http.request(req) }
|
148
|
+
errors ? false : true
|
149
|
+
end
|
150
|
+
|
102
151
|
|
103
152
|
end
|
data/lib/jeapie/version.rb
CHANGED
data/spec/lib/jeapie_spec.rb
CHANGED
@@ -5,12 +5,10 @@ describe Jeapie do
|
|
5
5
|
before(:each) do
|
6
6
|
Jeapie.clear
|
7
7
|
Jeapie.configure do |c|
|
8
|
-
c.user='qwqwqwqwqwqwqwqwqwqwqwqwqwqwqwqw'
|
9
8
|
c.token='asasasasasasasasasasasasasasasas'
|
10
9
|
end
|
11
10
|
FakeWeb.clean_registry
|
12
11
|
end
|
13
|
-
let(:api_url){Jeapie::API_URL}
|
14
12
|
let(:api_ok_answer){'{"success":true,"message":"Message was sent successfully"}'}
|
15
13
|
let(:api_fail_answer){'{"success":false,"errors":{"device":["Some error"]}}'}
|
16
14
|
|
@@ -28,7 +26,7 @@ describe Jeapie do
|
|
28
26
|
|
29
27
|
describe '#parameters' do
|
30
28
|
it 'should return all params' do
|
31
|
-
p={token:'1',
|
29
|
+
p={token:'1', message:'3', title:'4', device:'5', priority:'6'}
|
32
30
|
p.each do |k,v|
|
33
31
|
Jeapie.send "#{k}=", v
|
34
32
|
end
|
@@ -38,7 +36,7 @@ describe Jeapie do
|
|
38
36
|
|
39
37
|
describe '#notify' do
|
40
38
|
subject{ Jeapie.notify(params) }
|
41
|
-
|
39
|
+
let(:api_url){Jeapie::NOTIFY_API_URL}
|
42
40
|
|
43
41
|
context 'when all params OK' do
|
44
42
|
let(:params){ {message:'Text'} }
|
@@ -78,5 +76,45 @@ describe Jeapie do
|
|
78
76
|
end
|
79
77
|
end
|
80
78
|
|
79
|
+
describe '#notify_multiple' do
|
80
|
+
subject{ Jeapie.notify_multiple(params) }
|
81
|
+
let(:api_url){Jeapie::NOTIFY_MULTIPLE_API_URL}
|
82
|
+
before(:each){FakeWeb.register_uri(:post, api_url, :body => api_ok_answer) }
|
83
|
+
context 'when "emails" as array' do
|
84
|
+
let(:params){ {message:'Test message', emails: %w(user1@mail.com user2@mail.com)} }
|
85
|
+
it 'should send "emails" as string' do
|
86
|
+
subject.should be_true
|
87
|
+
FakeWeb.last_request.body.should include CGI.escape('user1@mail.com,user2@mail.com')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
context 'when "emails" as string with spaces' do
|
91
|
+
let(:params){ {message:'Test message', emails: 'user1@mail.com , user2@mail.com'} }
|
92
|
+
it 'should send "emails" as is as' do
|
93
|
+
subject.should be_true
|
94
|
+
FakeWeb.last_request.body.should include CGI.escape('user1@mail.com , user2@mail.com')
|
95
|
+
end
|
96
|
+
end
|
97
|
+
context 'when "emails" is empty' do
|
98
|
+
let(:params){ {message:'Test message', emails: ''} }
|
99
|
+
it 'should return "false" and "errors" must contain info about error' do
|
100
|
+
subject.should be_false
|
101
|
+
Jeapie.errors.should match /Params not valid:/
|
102
|
+
FakeWeb.should_not have_requested(:post, api_url)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#notify_all' do
|
108
|
+
context 'when all params "OK" and when server return "OK"' do
|
109
|
+
subject{ Jeapie.notify_all(params) }
|
110
|
+
let(:api_url){Jeapie::NOTIFY_ALL_API_URL}
|
111
|
+
let(:params){ {message:'Text'} }
|
112
|
+
before(:each){FakeWeb.register_uri(:post, api_url, :body => api_ok_answer) }
|
113
|
+
it 'should return "true"' do
|
114
|
+
subject.should be_true
|
115
|
+
FakeWeb.should have_requested(:post, api_url)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
81
119
|
|
82
120
|
end
|
data/whatsnew.md
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jeapie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -69,8 +69,6 @@ post_install_message:
|
|
69
69
|
rdoc_options:
|
70
70
|
- --line-numbers
|
71
71
|
- --inline-source
|
72
|
-
- --no-private
|
73
|
-
- --protected
|
74
72
|
- --title
|
75
73
|
- Jeapie
|
76
74
|
- --main
|