capcoauth 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/capcoauth/notifications.rb +108 -0
- data/lib/capcoauth/version.rb +1 -1
- data/lib/capcoauth.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22c6096e8562d514f12f137427193caee7c93f29
|
4
|
+
data.tar.gz: dd518c0fe730235d44f6a89105f63f5af29a4741
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 713ff96769caf90b4e512234f27de29ef03506ebc24b85c020b5dbef5cfe389f2401c5d789cef0e2c496d1218a86d6cfcad82eddd7598ea19a1708a2063cc906
|
7
|
+
data.tar.gz: 681415b738d9e4b78f4f2dd2ef0232061283c00030f67c867b0308fc5e6ba3224d70e067efde78e88eaf2b0d8d37d85a563151786e4d08581eaf6df3f386641e
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module Capcoauth
|
4
|
+
class Notifications
|
5
|
+
include HTTParty
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
@@bearer_token = nil
|
10
|
+
|
11
|
+
def bearer_token
|
12
|
+
return @@bearer_token if @@bearer_token.present?
|
13
|
+
|
14
|
+
res = self.post(
|
15
|
+
'/oauth/token',
|
16
|
+
{
|
17
|
+
body: {
|
18
|
+
grant_type: 'client_credentials',
|
19
|
+
},
|
20
|
+
basic_auth: {
|
21
|
+
username: Capcoauth.configuration.client_id,
|
22
|
+
password: Capcoauth.configuration.client_secret
|
23
|
+
}
|
24
|
+
}
|
25
|
+
)
|
26
|
+
if res.ok? and res.parsed_response['access_token']
|
27
|
+
@@bearer_token = res.parsed_response['access_token']
|
28
|
+
end
|
29
|
+
@@bearer_token
|
30
|
+
end
|
31
|
+
|
32
|
+
def default_headers
|
33
|
+
{
|
34
|
+
'Authorization' => "Bearer #{bearer_token}",
|
35
|
+
'Content-Type' => 'application/vnd.api+json'
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_device_token(user_id, device_token, device_type, environment = 'production')
|
40
|
+
body = {
|
41
|
+
data: {
|
42
|
+
type: 'user_devices',
|
43
|
+
attributes: {
|
44
|
+
device_token: device_token,
|
45
|
+
device_type: device_type,
|
46
|
+
},
|
47
|
+
relationships: {
|
48
|
+
user: {
|
49
|
+
data: {
|
50
|
+
type: 'users',
|
51
|
+
id: user_id
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
body[:data][:attributes][:environment] = environment if device_type == 'ios'
|
58
|
+
res = self.post(
|
59
|
+
'/api/v1/user_devices',
|
60
|
+
{
|
61
|
+
body: body.to_json,
|
62
|
+
headers: default_headers
|
63
|
+
}
|
64
|
+
)
|
65
|
+
@@bearer_token = nil if res.code == 401
|
66
|
+
return true if res.created?
|
67
|
+
return true if res.body.include? 'has already been registered'
|
68
|
+
false
|
69
|
+
end
|
70
|
+
|
71
|
+
def remove_device_token(device_token)
|
72
|
+
res = self.delete("/api/v1/user_devices/#{device_token}", headers: default_headers)
|
73
|
+
@@bearer_token = nil if res.code == 401
|
74
|
+
res.code == 204
|
75
|
+
end
|
76
|
+
|
77
|
+
def notify(user_id, alert=nil, badge=nil, data=nil)
|
78
|
+
res = self.post(
|
79
|
+
'/api/v1/user_notifications',
|
80
|
+
{
|
81
|
+
body: {
|
82
|
+
data: {
|
83
|
+
type: 'user_notifications',
|
84
|
+
attributes: {
|
85
|
+
alert: alert,
|
86
|
+
badge: badge,
|
87
|
+
data: data
|
88
|
+
},
|
89
|
+
relationships: {
|
90
|
+
user: {
|
91
|
+
data: {
|
92
|
+
type: 'users',
|
93
|
+
id: user_id
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}.to_json,
|
99
|
+
headers: default_headers
|
100
|
+
}
|
101
|
+
)
|
102
|
+
@@bearer_token = nil if res.code == 401
|
103
|
+
return true if res.created?
|
104
|
+
false
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/capcoauth/version.rb
CHANGED
data/lib/capcoauth.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capcoauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Robertson
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- lib/capcoauth/config.rb
|
94
94
|
- lib/capcoauth/engine.rb
|
95
95
|
- lib/capcoauth/helpers/controller.rb
|
96
|
+
- lib/capcoauth/notifications.rb
|
96
97
|
- lib/capcoauth/oauth/access_token.rb
|
97
98
|
- lib/capcoauth/oauth/token_verifier.rb
|
98
99
|
- lib/capcoauth/oauth/ttl_cache.rb
|