noti 1.0.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/lib/noti/notification.rb +22 -0
- data/lib/noti/request.rb +79 -0
- data/lib/noti/token.rb +20 -0
- data/lib/noti/version.rb +3 -0
- data/lib/noti.rb +27 -0
- metadata +60 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
module Noti
|
2
|
+
class Notification
|
3
|
+
|
4
|
+
attr_accessor :title, :sub_title, :text, :sound, :url
|
5
|
+
|
6
|
+
def deliver_to(user)
|
7
|
+
data = {
|
8
|
+
:app => Noti.app,
|
9
|
+
:user => user,
|
10
|
+
:notification => {
|
11
|
+
:title => self.title,
|
12
|
+
:sub_title => self.sub_title,
|
13
|
+
:text => self.text,
|
14
|
+
:url => self.url,
|
15
|
+
:sound => self.sound
|
16
|
+
}
|
17
|
+
}
|
18
|
+
Noti::Request.request('add', data) && true
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
data/lib/noti/request.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
module Noti
|
2
|
+
class Request
|
3
|
+
|
4
|
+
def self.request(path, data = {})
|
5
|
+
req = self.new(path, :post)
|
6
|
+
req.data = data
|
7
|
+
req.make && req.success? ? req.output : false
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :path, :method, :client
|
11
|
+
attr_accessor :data
|
12
|
+
|
13
|
+
def initialize(path, method = :get)
|
14
|
+
@path = path
|
15
|
+
@method = method
|
16
|
+
end
|
17
|
+
|
18
|
+
def success?
|
19
|
+
@success || false
|
20
|
+
end
|
21
|
+
|
22
|
+
def output
|
23
|
+
@output || nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def make
|
27
|
+
uri = URI.parse(["http://notiapp.com", "api/v1", @path].join('/'))
|
28
|
+
http_request = http_class.new(uri.request_uri)
|
29
|
+
http_request.initialize_http_header({"User-Agent" => "NotiRubyClient/#{Noti::VERSION}"})
|
30
|
+
http_request.content_type = 'application/json'
|
31
|
+
|
32
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
33
|
+
|
34
|
+
if uri.scheme == 'https'
|
35
|
+
http.use_ssl = true
|
36
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
37
|
+
end
|
38
|
+
|
39
|
+
http_result = http.request(http_request, @data.to_json)
|
40
|
+
if http_result.body == 'true'
|
41
|
+
@output = true
|
42
|
+
elsif http_result.body == 'false'
|
43
|
+
@output = false
|
44
|
+
else
|
45
|
+
@output = JSON.parse(http_result.body)
|
46
|
+
end
|
47
|
+
@success = case http_result
|
48
|
+
when Net::HTTPSuccess
|
49
|
+
true
|
50
|
+
when Net::HTTPServiceUnavailable
|
51
|
+
raise Noti::Errors::ServiceUnavailable
|
52
|
+
when Net::HTTPForbidden, Net::HTTPUnauthorized
|
53
|
+
raise Noti::Errors::AccessDenied, "Access Denied for '#{Noti.application}'"
|
54
|
+
when Net::HTTPNotFound
|
55
|
+
json = JSON.parse(http_result.body)
|
56
|
+
raise Noti::Errors::NotFound, json['error']
|
57
|
+
when Net::HTTPClientError
|
58
|
+
json = JSON.parse(http_result.body)
|
59
|
+
raise Noti::Errors::ValidationError, json.to_s
|
60
|
+
else
|
61
|
+
raise Noti::Errors::CommunicationError, http_result.body
|
62
|
+
end
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def http_class
|
69
|
+
case @method
|
70
|
+
when :post then Net::HTTP::Post
|
71
|
+
when :put then Net::HTTP::Put
|
72
|
+
when :delete then Net::HTTP::Delete
|
73
|
+
else
|
74
|
+
Net::HTTP::Get
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
data/lib/noti/token.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Noti
|
2
|
+
class Token
|
3
|
+
|
4
|
+
attr_accessor :request_token, :redirect_url
|
5
|
+
|
6
|
+
def self.create_request_token(redirect_url)
|
7
|
+
request = Noti::Request.request("request_access", :app => Noti.app, :redirect_url => redirect_url)
|
8
|
+
token = self.new
|
9
|
+
token.request_token = request['request_token']
|
10
|
+
token.redirect_url = request['redirect_url']
|
11
|
+
token
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.get_access_token(request_token)
|
15
|
+
request = Noti::Request.request("get_access_token", :app => Noti.app, :request_token => request_token)
|
16
|
+
request['access_token']
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/noti/version.rb
ADDED
data/lib/noti.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/https'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'noti/version'
|
6
|
+
require 'noti/request'
|
7
|
+
require 'noti/token'
|
8
|
+
require 'noti/notification'
|
9
|
+
|
10
|
+
module Noti
|
11
|
+
class << self
|
12
|
+
|
13
|
+
# Stores the application key for the application
|
14
|
+
attr_accessor :app
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class Error < StandardError; end
|
19
|
+
module Errors
|
20
|
+
class ServiceUnavailable < Error; end
|
21
|
+
class AccessDenied < Error; end
|
22
|
+
class NotFound < Error; end
|
23
|
+
class CommunicationError < Error; end
|
24
|
+
class ValidationError < Error; end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: noti
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Cooke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: &70229269592780 !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: *70229269592780
|
25
|
+
description:
|
26
|
+
email: adam@atechmedia.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/noti.rb
|
32
|
+
- lib/noti/notification.rb
|
33
|
+
- lib/noti/request.rb
|
34
|
+
- lib/noti/token.rb
|
35
|
+
- lib/noti/version.rb
|
36
|
+
homepage: http://notiapp.com
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Easy to use client library for Noti
|
60
|
+
test_files: []
|