Class: Capcoauth::Notifications

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/capcoauth/notifications.rb

Constant Summary collapse

@@bearer_token =
nil

Class Method Summary collapse

Class Method Details

.add_device_token(user_id, device_token, device_type, environment = 'production') ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capcoauth/notifications.rb', line 39

def add_device_token(user_id, device_token, device_type, environment = 'production')
  body = {
    data: {
      type: 'user_devices',
      attributes: {
        device_token: device_token,
        device_type: device_type,
      },
      relationships: {
        user: {
          data: {
            type: 'users',
            id: user_id
          }
        }
      }
    }
  }
  body[:data][:attributes][:environment] = environment if device_type == 'ios'
  res = self.post(
    "#{Capcoauth.configuration.capcoauth_url}/api/v1/user_devices",
    {
      body: body.to_json,
      headers: default_headers
    }
  )
  @@bearer_token = nil if res.code == 401
  return true if res.created?
  return true if res.body.include? 'has already been registered'
  false
end

.bearer_tokenObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/capcoauth/notifications.rb', line 11

def bearer_token
  return @@bearer_token if @@bearer_token.present?

  res = self.post(
    "#{Capcoauth.configuration.capcoauth_url}/oauth/token",
    {
      body: {
        grant_type: 'client_credentials',
      },
      basic_auth: {
        username: Capcoauth.configuration.client_id,
        password: Capcoauth.configuration.client_secret
      }
    }
  )
  if res.ok? and res.parsed_response['access_token']
    @@bearer_token = res.parsed_response['access_token']
  end
  @@bearer_token
end

.default_headersObject



32
33
34
35
36
37
# File 'lib/capcoauth/notifications.rb', line 32

def default_headers
  {
    'Authorization' => "Bearer #{bearer_token}",
    'Content-Type' => 'application/vnd.api+json'
  }
end

.notify(user_id, alert = nil, badge = nil, data = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/capcoauth/notifications.rb', line 77

def notify(user_id, alert=nil, badge=nil, data=nil)
  res = self.post(
    "#{Capcoauth.configuration.capcoauth_url}/api/v1/user_notifications",
    {
      body: {
        data: {
          type: 'user_notifications',
          attributes: {
            alert: alert,
            badge: badge,
            data: data
          },
          relationships: {
            user: {
              data: {
                type: 'users',
                id: user_id
              }
            }
          }
        }
      }.to_json,
      headers: default_headers
    }
  )
  @@bearer_token = nil if res.code == 401
  return true if res.created?
  false
end

.remove_device_token(device_token) ⇒ Object



71
72
73
74
75
# File 'lib/capcoauth/notifications.rb', line 71

def remove_device_token(device_token)
  res = self.delete("#{Capcoauth.configuration.capcoauth_url}/api/v1/user_devices/#{device_token}", headers: default_headers)
  @@bearer_token = nil if res.code == 401
  res.code == 204
end