capcoauth 0.2.1 → 0.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88d88d5d24669f7c09057d039ed04bcf8f7e4215
4
- data.tar.gz: e3126ba3d76375574f63dd3b7f65177e66f1a781
3
+ metadata.gz: 22c6096e8562d514f12f137427193caee7c93f29
4
+ data.tar.gz: dd518c0fe730235d44f6a89105f63f5af29a4741
5
5
  SHA512:
6
- metadata.gz: c7ed882f4e5786775fa4f858c994398f6bd01720f27ea2da660eec2dd2ae2e3f1d98029c50a9ce49b2c45a2292d0bfd54430d5a87deb13e467739a842022fbc3
7
- data.tar.gz: c41136838fe6f2e529b2f290a1b27e7860983c756e089e1751bb3bece86c61bf9d54598c38741a077357fc161ba2f3c2222224a24f8d8104f8c0f48fdced2b84
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
@@ -1,3 +1,3 @@
1
1
  module Capcoauth
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/capcoauth.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'capcoauth/version'
2
2
  require 'capcoauth/engine'
3
3
  require 'capcoauth/config'
4
+ require 'capcoauth/notifications'
4
5
 
5
6
  require 'capcoauth/oauth/access_token'
6
7
  require 'capcoauth/oauth/token_verifier'
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.1
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