jeapie 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +24 -0
- data/README.md +52 -0
- data/Rakefile +1 -0
- data/jeapie.gemspec +23 -0
- data/lib/jeapie.rb +92 -0
- data/lib/jeapie/version.rb +3 -0
- data/spec/lib/jeapie_spec.rb +82 -0
- data/spec/spec_helper.rb +27 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
All software in this package is covered by the MIT license
|
2
|
+
|
3
|
+
Copyright (c) 2013 Lutsko Dmitriy
|
4
|
+
|
5
|
+
MIT License
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
a copy of this software and associated documentation files (the
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Jeapie
|
2
|
+
|
3
|
+
This is wrapper for RESTFull API http://jeapie.com
|
4
|
+
It allow send push notification to your Android and Apple devices
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
$ gem 'jeapie'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install jeapie
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
```ruby
|
21
|
+
require 'jeapie'
|
22
|
+
```
|
23
|
+
|
24
|
+
To send with the very minimum amount of information.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Jeapie.notify(message: 'message', title: 'title', user: 'USER_TOKEN', token: 'APP_TOKEN')
|
28
|
+
```
|
29
|
+
|
30
|
+
Optional you can place in /config/application.rb
|
31
|
+
```ruby
|
32
|
+
Jeapie.configure do |config|
|
33
|
+
config.user='USER_TOKEN' # you can take from http://dashboard.jeapie.com
|
34
|
+
config.token='APP_TOKEN'
|
35
|
+
config.device='Nexus7' #optional
|
36
|
+
config.priority=0 #or 1(high) or -1(low, not sound when receive). By default is 0
|
37
|
+
end
|
38
|
+
|
39
|
+
Jeapie.notify(message: 'message', title: 'title')
|
40
|
+
#or just
|
41
|
+
Jeapie.notify(message: 'message')
|
42
|
+
```
|
43
|
+
Method `notify` return true or false. If it return false you can check `Jeapie.errors` for error message.
|
44
|
+
Or you can use method `notify!`, it raise exception if something going wrong.
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/jeapie.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jeapie/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'jeapie'
|
8
|
+
gem.version = Jeapie::VERSION
|
9
|
+
gem.authors = ['Lutsko Dmitriy']
|
10
|
+
gem.email = %w(regrahc@gmail.com)
|
11
|
+
gem.date = Time.now.strftime('%Y-%m-%d')
|
12
|
+
gem.description = 'Wrapper for Jeapie push service http://jeapie.com'
|
13
|
+
gem.summary = 'Allow send push notification to your Android and Apple devices'
|
14
|
+
gem.homepage = 'https://github.com/charger/jeapie'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = %w(lib)
|
20
|
+
|
21
|
+
gem.add_dependency('activesupport')
|
22
|
+
gem.add_development_dependency 'fakeweb', ['~> 1.3']
|
23
|
+
end
|
data/lib/jeapie.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'jeapie/version'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
require 'active_support/json'
|
5
|
+
|
6
|
+
module Jeapie
|
7
|
+
class DeliverException < StandardError ; end
|
8
|
+
extend self
|
9
|
+
# app_token and user_key, you can get it from dashboard.jeapie.com
|
10
|
+
attr_accessor :token, :user
|
11
|
+
attr_accessor :message, :title, :device, :priority
|
12
|
+
attr_reader :result
|
13
|
+
|
14
|
+
API_URL= 'https://api.jeapie.com/v1/send/message.json'
|
15
|
+
PRIORITY_LOW=-1
|
16
|
+
PRIORITY_MED=0
|
17
|
+
PRIORITY_HIGH=1
|
18
|
+
MESSAGE_MAX_LEN= 2000
|
19
|
+
|
20
|
+
def configure
|
21
|
+
yield self
|
22
|
+
parameters
|
23
|
+
end
|
24
|
+
|
25
|
+
# available parameters and his values
|
26
|
+
def parameters
|
27
|
+
h = {}
|
28
|
+
keys.each { |k| h[k.to_sym] = Jeapie.instance_variable_get("@#{k}") }
|
29
|
+
h
|
30
|
+
end
|
31
|
+
alias_method :params, :parameters
|
32
|
+
|
33
|
+
def keys
|
34
|
+
@keys||= [:token, :user, :message, :title, :device, :priority]
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear
|
38
|
+
keys.each do |k|
|
39
|
+
Jeapie.instance_variable_set("@#{k}", nil)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def errors
|
44
|
+
return "Params not valid: #{@params_errors}" unless @params_errors && @params_errors.empty?
|
45
|
+
return false if result.nil?
|
46
|
+
begin
|
47
|
+
arr= JSON::parse(result.body)
|
48
|
+
rescue JSON::ParserError
|
49
|
+
return "Can't parse response: #{result.body}"
|
50
|
+
end
|
51
|
+
|
52
|
+
return false if arr['success']
|
53
|
+
"code: #{result.code}, errors: #{arr['errors']}"
|
54
|
+
end
|
55
|
+
|
56
|
+
# push a message to Jeapie
|
57
|
+
# example: notify message:'Backup complete'
|
58
|
+
# or: notify title:'Backup complete', message:'Time elapsed 50s, size: 500Mb', priority:-1, device:'Nexus7', user:'', token:''
|
59
|
+
# @return [String] the response from jeapie.com, in json.
|
60
|
+
def notify(opts={message:''})
|
61
|
+
data = params.merge(opts).select { |_, v| v != nil }
|
62
|
+
return false unless params_errors(data).empty?
|
63
|
+
url = URI.parse(API_URL)
|
64
|
+
req = Net::HTTP::Post.new(url.path, {'User-Agent' => "Ruby jeapie gem: #{Jeapie::VERSION}"})
|
65
|
+
req.set_form_data(data)
|
66
|
+
|
67
|
+
res = Net::HTTP.new(url.host, url.port)
|
68
|
+
res.use_ssl = true
|
69
|
+
res.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
70
|
+
@result= res.start {|http| http.request(req) }
|
71
|
+
errors ? false : true
|
72
|
+
end
|
73
|
+
|
74
|
+
def notify!(opts={message:''})
|
75
|
+
raise DeliverException, errors unless notify(opts)
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
def params_errors(params)
|
81
|
+
@params_errors=[]
|
82
|
+
@params_errors<<'Token cannot be blank' if params[:token].blank?
|
83
|
+
@params_errors<<'Token must be 32 symbols' if params[:token].size != 32
|
84
|
+
@params_errors<<'User cannot be blank' if params[:user].blank?
|
85
|
+
@params_errors<<'User must be 32 symbols' if params[:user].size != 32
|
86
|
+
@params_errors<<'Message cannot be blank' if params[:message].blank?
|
87
|
+
@params_errors<<"Message too long, max: #{MESSAGE_MAX_LEN}" if params[:message] && params[:message].size > MESSAGE_MAX_LEN
|
88
|
+
@params_errors
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Jeapie do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
Jeapie.clear
|
7
|
+
Jeapie.configure do |c|
|
8
|
+
c.user='qwqwqwqwqwqwqwqwqwqwqwqwqwqwqwqw'
|
9
|
+
c.token='asasasasasasasasasasasasasasasas'
|
10
|
+
end
|
11
|
+
FakeWeb.clean_registry
|
12
|
+
end
|
13
|
+
let(:api_url){Jeapie::API_URL}
|
14
|
+
let(:api_ok_answer){'{"success":true,"message":"Message was sent successfully"}'}
|
15
|
+
let(:api_fail_answer){'{"success":false,"errors":{"device":["Some error"]}}'}
|
16
|
+
|
17
|
+
describe '#configure' do
|
18
|
+
Jeapie.keys.each do |key|
|
19
|
+
it "#{key} should be configured via .configure" do
|
20
|
+
r = 'qwerty'
|
21
|
+
Jeapie.configure do |c|
|
22
|
+
c.instance_variable_set "@#{key}", r
|
23
|
+
end
|
24
|
+
Jeapie.send(key).should eq r
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#parameters' do
|
30
|
+
it 'should return all params' do
|
31
|
+
p={token:'1', user:'2', message:'3', title:'4', device:'5', priority:'6'}
|
32
|
+
p.each do |k,v|
|
33
|
+
Jeapie.send "#{k}=", v
|
34
|
+
end
|
35
|
+
Jeapie.parameters.should == p
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#notify' do
|
40
|
+
subject{ Jeapie.notify(params) }
|
41
|
+
|
42
|
+
|
43
|
+
context 'when all params OK' do
|
44
|
+
let(:params){ {message:'Text'} }
|
45
|
+
context 'and when server return OK' do
|
46
|
+
before(:each){FakeWeb.register_uri(:post, api_url, :body => api_ok_answer) }
|
47
|
+
it 'should return "true"' do
|
48
|
+
subject.should be_true
|
49
|
+
FakeWeb.should have_requested(:post, api_url)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context 'and when server return error' do
|
53
|
+
before(:each){FakeWeb.register_uri(:post, api_url, :body => api_fail_answer) }
|
54
|
+
it 'should return "false" and "errors" must contain info about error' do
|
55
|
+
subject.should be_false
|
56
|
+
Jeapie.errors.should match /code:/
|
57
|
+
FakeWeb.should have_requested(:post, api_url)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'when "message" missing' do
|
64
|
+
let(:params){ {message:''} }
|
65
|
+
it 'should return "false" and "errors" must contain info about error' do
|
66
|
+
subject.should be_false
|
67
|
+
Jeapie.errors.should match /Params not valid:/
|
68
|
+
FakeWeb.should_not have_requested(:post, api_url)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when message too long' do
|
73
|
+
let(:params){ {message:'q'*2001} }
|
74
|
+
it 'should return "false" and "errors" must contain "Message too long"' do
|
75
|
+
subject.should be_false
|
76
|
+
Jeapie.errors.should match /Message too long/
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
if ENV['COVERAGE'] == 'true'
|
2
|
+
require 'simplecov'
|
3
|
+
require 'simplecov-rcov'
|
4
|
+
|
5
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
+
SimpleCov::Formatter::HTMLFormatter,
|
7
|
+
SimpleCov::Formatter::RcovFormatter
|
8
|
+
]
|
9
|
+
SimpleCov.start do
|
10
|
+
add_filter '/spec/'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'fake_web'
|
15
|
+
require 'fakeweb_matcher'
|
16
|
+
require 'jeapie'
|
17
|
+
|
18
|
+
include Jeapie
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
22
|
+
config.run_all_when_everything_filtered = true
|
23
|
+
config.filter_run :focus
|
24
|
+
config.order = 'random'
|
25
|
+
end
|
26
|
+
|
27
|
+
FakeWeb.allow_net_connect = false
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jeapie
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lutsko Dmitriy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
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: fakeweb
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
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: '1.3'
|
46
|
+
description: Wrapper for Jeapie push service http://jeapie.com
|
47
|
+
email:
|
48
|
+
- regrahc@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- jeapie.gemspec
|
59
|
+
- lib/jeapie.rb
|
60
|
+
- lib/jeapie/version.rb
|
61
|
+
- spec/lib/jeapie_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://github.com/charger/jeapie
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Allow send push notification to your Android and Apple devices
|
87
|
+
test_files:
|
88
|
+
- spec/lib/jeapie_spec.rb
|
89
|
+
- spec/spec_helper.rb
|