Class: SocketServ::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/socket_serv/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
# File 'lib/socket_serv/client.rb', line 19

def initialize(client_id, client_secret)
  @client_id = client_id
  # The long-lived key.
  @client_secret = client_secret

  # Refresh the short-lived key.
  refresh_token!
end

Class Method Details

.shared_client(client_id = nil, client_secret = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/socket_serv/client.rb', line 6

def self.shared_client(client_id=nil, client_secret=nil)
  client_secret ||= ENV["SOCKETSERV_APP_SECRET"]
  client_id     ||= ENV["SOCKETSERV_APP_ID"]
  Thread.current[:socketserv_api] ||= self.new(client_id, client_secret)
  Thread.current[:socketserv_api].refresh_token!
  Thread.current[:socketserv_api]
end

.test_mode!Object



14
15
16
# File 'lib/socket_serv/client.rb', line 14

def self.test_mode!
  @@test_mode = true
end

Instance Method Details

#message_to_session(session_id, message_payload) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/socket_serv/client.rb', line 75

def message_to_session(session_id, message_payload)
  return true if @@test_mode

  resp = self.class.post "/api/v1/sessions/#{session_id}/push", authenticated_params({
    headers: {
      'Content-Type' => 'application/json'
    },
    body: JSON.generate({
      payload: message_payload
    })
  })

  resp.code == 200
end

#message_to_sessions(session_ids, message_payload) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/socket_serv/client.rb', line 90

def message_to_sessions(session_ids, message_payload)
  return true if @@test_mode

  resp = self.class.post "/api/v1/multi_push", authenticated_params({
    headers: {
      'Content-Type' => 'application/json'
    },
    body: JSON.generate({
      session_ids: session_ids,
      payload: message_payload
    })
  })

  resp.code == 200
end

#new_session(name = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/socket_serv/client.rb', line 48

def new_session(name=nil)
  return {
    "name" => name,
    "id" => "bc5e7d36-76c9-4d8d-9535-6a49d96fea4b"
  } if @@test_mode

  resp = self.class.post "/api/v1/sessions", authenticated_params({
    body: {
      session: {
        name: name
      }
    }
  })

  if resp.code == 200
    resp.parsed_response
  end
end

#refresh_token!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/socket_serv/client.rb', line 28

def refresh_token!
  return @token = JWT.encode({"exp" => (Time.now + 86400).to_i}, "secret") if @@test_mode
  return unless requires_refresh?

  resp = self.class.post "/oauth/token", {
    body: {
      grant_type: "client_credentials",
      client_id: @client_id,
      client_secret: @client_secret
    }
  }

  if resp.code == 200
    @token = resp.parsed_response["access_token"]
  else
    Rails.logger.fatal "[SocketServ] Token refresh failed (#{resp.code})"
    @token = nil
  end
end

#verify_session(session_id) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/socket_serv/client.rb', line 67

def verify_session(session_id)
  return true if @@test_mode

  resp = self.class.get "/api/v1/sessions/#{session_id}", authenticated_params

  resp.code == 200
end