realpush 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +21 -0
- data/Gemfile +4 -0
- data/Guardfile +77 -0
- data/LICENSE +21 -0
- data/README.md +24 -0
- data/Rakefile +2 -0
- data/lib/realpush.rb +55 -0
- data/lib/realpush/api/app.rb +11 -0
- data/lib/realpush/api/base.rb +155 -0
- data/lib/realpush/api/base_create.rb +21 -0
- data/lib/realpush/api/base_destroy.rb +22 -0
- data/lib/realpush/api/base_list.rb +22 -0
- data/lib/realpush/api/base_update.rb +21 -0
- data/lib/realpush/client.rb +188 -0
- data/lib/realpush/request.rb +89 -0
- data/lib/realpush/resource.rb +36 -0
- data/lib/realpush/version.rb +6 -0
- data/realpush.gemspec +39 -0
- data/spec/realpush/api/app_spec.rb +182 -0
- data/spec/realpush/client_spec.rb +146 -0
- data/spec/realpush/request_spec.rb +52 -0
- data/spec/spec_helper.rb +27 -0
- metadata +288 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 07b50b05342cbf396b21078cc541a86da0e1a687
|
4
|
+
data.tar.gz: 321537b3c17e1374f98bbdcf7241c1b581f5adbc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a9e10ec0da01ba0d3400cafae4a9d6e677e00c3a9f0cfc73d9df8db9a3c4950d94efd737458fbe0e4f10de3c20fab5651738ee099cb472cfd7841f6d6f0bb9e8
|
7
|
+
data.tar.gz: 716862c51d40e618b65cc6ceee994a8675e8542c2e8284aa5f6f86d35eab268287b26d1833743b06555ae0d5aecf6e7f42d67780973221b93ebad60074f3935e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
before_install:
|
4
|
+
- gem install bundler
|
5
|
+
|
6
|
+
cache: bundler
|
7
|
+
|
8
|
+
sudo: false
|
9
|
+
|
10
|
+
script:
|
11
|
+
- bundle exec rspec
|
12
|
+
|
13
|
+
rvm:
|
14
|
+
- 2.0.0
|
15
|
+
- 2.1.2
|
16
|
+
- 2.1.5
|
17
|
+
- 2.2
|
18
|
+
|
19
|
+
addons:
|
20
|
+
code_climate:
|
21
|
+
repo_token: 868adb2fbaf42e5f24e170dee9e89a4385a181dc0de2c9f61b288eb4c40de307
|
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features)
|
6
|
+
|
7
|
+
## Uncomment to clear the screen before every task
|
8
|
+
# clearing :on
|
9
|
+
|
10
|
+
## Guard internally checks for changes in the Guardfile and exits.
|
11
|
+
## If you want Guard to automatically start up again, run guard in a
|
12
|
+
## shell loop, e.g.:
|
13
|
+
##
|
14
|
+
## $ while bundle exec guard; do echo "Restarting Guard..."; done
|
15
|
+
##
|
16
|
+
## Note: if you are using the `directories` clause above and you are not
|
17
|
+
## watching the project directory ('.'), then you will want to move
|
18
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
19
|
+
#
|
20
|
+
# $ mkdir config
|
21
|
+
# $ mv Guardfile config/
|
22
|
+
# $ ln -s config/Guardfile .
|
23
|
+
#
|
24
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
25
|
+
|
26
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
27
|
+
# rspec may be run, below are examples of the most common uses.
|
28
|
+
# * bundler: 'bundle exec rspec'
|
29
|
+
# * bundler binstubs: 'bin/rspec'
|
30
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
31
|
+
# installed the spring binstubs per the docs)
|
32
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
33
|
+
# * 'just' rspec: 'rspec'
|
34
|
+
|
35
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
36
|
+
require "guard/rspec/dsl"
|
37
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
38
|
+
|
39
|
+
# Feel free to open issues for suggestions and improvements
|
40
|
+
|
41
|
+
# RSpec files
|
42
|
+
rspec = dsl.rspec
|
43
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
44
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
45
|
+
watch(rspec.spec_files)
|
46
|
+
|
47
|
+
# Ruby files
|
48
|
+
ruby = dsl.ruby
|
49
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
50
|
+
|
51
|
+
# Rails files
|
52
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
53
|
+
dsl.watch_spec_files_for(rails.app_files)
|
54
|
+
dsl.watch_spec_files_for(rails.views)
|
55
|
+
|
56
|
+
watch(rails.controllers) do |m|
|
57
|
+
[
|
58
|
+
rspec.spec.("routing/#{m[1]}_routing"),
|
59
|
+
rspec.spec.("controllers/#{m[1]}_controller"),
|
60
|
+
rspec.spec.("acceptance/#{m[1]}")
|
61
|
+
]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Rails config changes
|
65
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
66
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
67
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
68
|
+
|
69
|
+
# Capybara features specs
|
70
|
+
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
|
71
|
+
|
72
|
+
# Turnip features and steps
|
73
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
74
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
75
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
76
|
+
end
|
77
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Zaez Inovação Digital
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
RealPush [![Build Status](https://travis-ci.org/zaeznet/realpush-ruby.svg?branch=master)](https://travis-ci.org/zaeznet/realpush-ruby) [![Dependency Status](https://gemnasium.com/zaeznet/realpush-ruby.svg)](https://gemnasium.com/zaeznet/realpush-ruby) [![Code Climate](https://codeclimate.com/github/zaeznet/realpush-ruby/badges/gpa.svg)](https://codeclimate.com/github/zaeznet/realpush-ruby) [![Test Coverage](https://codeclimate.com/github/zaeznet/realpush-ruby/badges/coverage.svg)](https://codeclimate.com/github/zaeznet/realpush-ruby) [![Inline docs](http://inch-ci.org/github/zaeznet/realpush-ruby.svg?branch=master)](http://inch-ci.org/github/zaeznet/realpush-ruby)
|
2
|
+
========
|
3
|
+
**RealPush Library for Ruby**
|
4
|
+
|
5
|
+
## Installation & Configuration
|
6
|
+
|
7
|
+
Add realpush to your Gemfile, and then run bundle install
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'realpush'
|
11
|
+
```
|
12
|
+
|
13
|
+
or install via gem
|
14
|
+
|
15
|
+
```bash
|
16
|
+
gem install realpush
|
17
|
+
```
|
18
|
+
|
19
|
+
After registering at http://realpush.cc/ configure your app with the security credentials.
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
data/Rakefile
ADDED
data/lib/realpush.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
autoload 'Logger', 'logger'
|
2
|
+
require 'active_support/all'
|
3
|
+
|
4
|
+
module RealPush
|
5
|
+
# All errors descend from this class so they can be easily rescued
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# begin
|
9
|
+
# RealPush.trigger('channel_name', 'event_name, {:some => 'data'})
|
10
|
+
# rescue RealPush::Error => e
|
11
|
+
# # Do something on error
|
12
|
+
# end
|
13
|
+
class Error < RuntimeError; end
|
14
|
+
class AuthenticationError < Error; end
|
15
|
+
class ConfigurationError < Error; end
|
16
|
+
class HTTPError < Error; end
|
17
|
+
|
18
|
+
API_VERSION_BE = 'v1'
|
19
|
+
API_VERSION_APP = 'v1'
|
20
|
+
|
21
|
+
autoload :Client, 'realpush/client'
|
22
|
+
autoload :Request, 'realpush/request'
|
23
|
+
autoload :Resource, 'realpush/resource'
|
24
|
+
|
25
|
+
class << self
|
26
|
+
attr_writer :logger
|
27
|
+
|
28
|
+
def logger
|
29
|
+
@logger ||= begin
|
30
|
+
log = Logger.new($stdout)
|
31
|
+
log.level = Logger::INFO
|
32
|
+
log
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def default_client
|
37
|
+
@default_client ||= Client.new
|
38
|
+
end
|
39
|
+
|
40
|
+
%w(trigger trigger_async post post_async get get_async).each do |method|
|
41
|
+
delegate method, to: 'default_client'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module API
|
46
|
+
autoload :App, 'realpush/api/app'
|
47
|
+
autoload :Base, 'realpush/api/base'
|
48
|
+
autoload :BaseCreate, 'realpush/api/base_create'
|
49
|
+
autoload :BaseDestroy, 'realpush/api/base_destroy'
|
50
|
+
autoload :BaseList, 'realpush/api/base_list'
|
51
|
+
autoload :BaseUpdate, 'realpush/api/base_update'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
require 'realpush/version'
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module RealPush
|
4
|
+
module API
|
5
|
+
module Base
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.class_eval do
|
10
|
+
|
11
|
+
attr_reader :token
|
12
|
+
|
13
|
+
attr_accessor :connect_timeout, :send_timeout, :receive_timeout, :keep_alive_timeout
|
14
|
+
|
15
|
+
def initialize(token)
|
16
|
+
raise ConfigurationError, "Invalid token format: #{token}" if /^[-=\w]{43}={0,2}$/.match(token).nil?
|
17
|
+
@token = token
|
18
|
+
@params_accept = []
|
19
|
+
|
20
|
+
# Default timeouts
|
21
|
+
@connect_timeout = 5
|
22
|
+
@send_timeout = 5
|
23
|
+
@receive_timeout = 5
|
24
|
+
@keep_alive_timeout = 30
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
# Sends a request to the specified URL.
|
30
|
+
#
|
31
|
+
# @param method [String | Symbol]
|
32
|
+
# HTTP method to be sent. method.to_s.upcase is used.
|
33
|
+
#
|
34
|
+
# @param uri [String | URI]
|
35
|
+
# HTTP method to be sent. method.to_s.upcase is used.
|
36
|
+
#
|
37
|
+
# @param query [Hash]
|
38
|
+
# a Hash of query part of URL.
|
39
|
+
# e.g. { "a" => "b" } => 'http://host/part?a=b'
|
40
|
+
#
|
41
|
+
# @param body [Hash]
|
42
|
+
# a Hash of body part. e.g.
|
43
|
+
# { "a" => "b" } => 'a=b'
|
44
|
+
#
|
45
|
+
# @param header [Hash]
|
46
|
+
# a Hash of extra headers. e.g.
|
47
|
+
# { 'Accept' => 'text/html' }.
|
48
|
+
#
|
49
|
+
# @return [ HTTP::Message ]
|
50
|
+
def execute(method, uri, query={}, body={}, header={})
|
51
|
+
begin
|
52
|
+
response = httpclient.request(method, uri, query, body, header)
|
53
|
+
rescue HTTPClient::BadResponseError,
|
54
|
+
HTTPClient::TimeoutError,
|
55
|
+
SocketError,
|
56
|
+
Errno::ECONNREFUSED => e
|
57
|
+
raise RealPush::HTTPError, "#{e.message} (#{e.class})"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# TODO: nodoc
|
62
|
+
# @return [HTTPClient]
|
63
|
+
def httpclient
|
64
|
+
@client ||= begin
|
65
|
+
require 'httpclient'
|
66
|
+
|
67
|
+
HTTPClient.new(default_header: {'X-RealPush-Token' => token}).tap do |c|
|
68
|
+
c.connect_timeout = connect_timeout
|
69
|
+
c.send_timeout = send_timeout
|
70
|
+
c.receive_timeout = receive_timeout
|
71
|
+
c.keep_alive_timeout = keep_alive_timeout
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Capture the contents of the request and makes the JSON parse inside the BODY content.
|
77
|
+
#
|
78
|
+
# @param content [ HTTP::Message ]
|
79
|
+
# Returns of self.httpclient
|
80
|
+
#
|
81
|
+
# @return [ Hash ]
|
82
|
+
def parse_content(content)
|
83
|
+
MultiJson.decode(content.body)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Prepare the URL to the request, it contains the url pattern for API Version 1.0.
|
87
|
+
#
|
88
|
+
# :example:
|
89
|
+
# url() //=> https://app.realpush.cc/api/v1/
|
90
|
+
# url('apps/123.json') //=> https://app.realpush.cc/api/v1/apps/123.json
|
91
|
+
#
|
92
|
+
# @param path [String]
|
93
|
+
# Added in path of URL
|
94
|
+
#
|
95
|
+
# @return [URI]
|
96
|
+
def url(path='')
|
97
|
+
path = "/#{path}" unless path.start_with? '/'
|
98
|
+
URI::Generic.build({
|
99
|
+
:scheme => 'https',
|
100
|
+
:host => 'app.realpush.cc',
|
101
|
+
:port => 443,
|
102
|
+
:path => "/api/#{RealPush::API_VERSION_BE}#{path}"
|
103
|
+
})
|
104
|
+
end
|
105
|
+
|
106
|
+
# Validate a params accepted
|
107
|
+
#
|
108
|
+
# @param params [Hash]
|
109
|
+
#
|
110
|
+
# @return [TrueClass]
|
111
|
+
def valid_params?(params)
|
112
|
+
params.keys.each do |key|
|
113
|
+
unless RealPush::API::App.params_accept.include? key.to_sym
|
114
|
+
raise ConfigurationError, "Invalid parameter! ( #{RealPush::API::App.params_accept.join(', ')} )"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
true
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
module ClassMethods
|
124
|
+
attr_reader :params_accept, :params
|
125
|
+
|
126
|
+
def accept_params(*args)
|
127
|
+
@params_accept ||= []
|
128
|
+
args.each do |field|
|
129
|
+
@params_accept << field
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def configure(params={})
|
134
|
+
@params = {
|
135
|
+
base_path: nil,
|
136
|
+
modules: [
|
137
|
+
:list,
|
138
|
+
:create,
|
139
|
+
:update,
|
140
|
+
:destroy
|
141
|
+
]
|
142
|
+
}.deep_merge(params.symbolize_keys)
|
143
|
+
raise ConfigurationError, 'Invalid parameters, you need a "base_path"' unless params[:base_path]
|
144
|
+
@params[:modules].each do |a|
|
145
|
+
a = a.to_s.downcase
|
146
|
+
send(:include, Object.const_get("RealPush::API::Base#{a[0].upcase}#{a[1..-1]}"))
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RealPush
|
2
|
+
module API
|
3
|
+
module BaseCreate
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
def create(data)
|
9
|
+
valid_params? data
|
10
|
+
content = execute :post, url("#{self.class.params[:base_path]}.json"), {}, data
|
11
|
+
parse_content content
|
12
|
+
rescue RealPush::HTTPError,
|
13
|
+
RealPush::ConfigurationError => e
|
14
|
+
raise RealPush::ConfigurationError, e.message
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RealPush
|
2
|
+
module API
|
3
|
+
module BaseDestroy
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
def destroy(id)
|
9
|
+
content = execute :delete, url("#{self.class.params[:base_path]}/#{id}.json")
|
10
|
+
content.status == 204
|
11
|
+
rescue RealPush::HTTPError => e
|
12
|
+
{
|
13
|
+
status: :ERROR,
|
14
|
+
message: e.message
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|