Class: Passage::UserAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/passageidentity/user_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_id, api_key) ⇒ UserAPI

This class will require an API key



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/passageidentity/user_api.rb', line 6

def initialize(app_id, api_key)
  @app_id = app_id
  @api_key = api_key
  @user_client = OpenapiClient::UsersApi.new
  @user_device_client = OpenapiClient::UserDevicesApi.new

  header_params = { "Passage-Version" => "passage-ruby #{Passage::VERSION}"}
  header_params["Authorization"] = "Bearer #{@api_key}" if @api_key != ""
  
  @req_opts = {}
  @req_opts[:header_params] = header_params
  @req_opts[:debug_auth_names] = ["header"]
  
end

Instance Method Details

#activate(user_id:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/passageidentity/user_api.rb', line 79

def activate(user_id:)
  user_exists?(user_id)

  begin
    response = @user_client.activate_user(@app_id, user_id, @req_opts)
    return response.user
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              message: "Passage User with ID \"#{user_id}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              message: "failed to activate Passage User.",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end

#create(email: "", phone: "", user_metadata: {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/passageidentity/user_api.rb', line 152

def create(email: "", phone: "", user_metadata: {})
  create = {}
  create["email"] = email unless email.empty?
  create["phone"] = phone unless phone.empty?
  create["user_metadata"] =  unless .empty?
  begin
    response = @user_client.create_user(@app_id, create, @req_opts)
    return response.user
  rescue Faraday::Error => e
    raise PassageError.new(
            "failed to create Passage User",
            status_code: e.response[:status],
            body: e.response[:body]
          )
  end
end

#deactivate(user_id:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/passageidentity/user_api.rb', line 102

def deactivate(user_id:)
  user_exists?(user_id)

  begin
    response = @user_client.deactivate_user(@app_id, user_id, @req_opts)
    return response.user
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              message: "Passage User with ID \"#{user_id}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              message: "failed to deactivate Passage User.",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end

#delete(user_id:) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/passageidentity/user_api.rb', line 169

def delete(user_id:)
  user_exists?(user_id)

  begin
    response = @user_client.delete_user(@app_id, user_id, @req_opts)
    return true
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              "passage User with ID \"#{user_id}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              "failed to delete Passage User",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end

#delete_device(user_id:, device_id:) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/passageidentity/user_api.rb', line 192

def delete_device(user_id:, device_id:)
  user_exists?(user_id)
  device_exists?(device_id)

  begin
    response = @user_device_client.delete_user_devices(@app_id, user_id, device_id, @req_opts)
    return true
  rescue Faraday::Error => e
    raise PassageError.new(
            "failed to delete Passage User Device",
            status_code: e.response[:status],
            body: e.response[:body]
          )
  end
end

#get(user_id:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/passageidentity/user_api.rb', line 21

def get(user_id:)
  user_exists?(user_id)

  begin
    response = @user_client.get_user(@app_id, user_id, @req_opts)
    user = response.user
    return user
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              message: "Passage User with ID \"#{user_id}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              message: "failed to get Passage User.",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end

#get_by_identifier(user_identifier:) ⇒ Object



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
70
71
72
73
74
75
76
77
# File 'lib/passageidentity/user_api.rb', line 45

def get_by_identifier(user_identifier:)
  identifier_exists?(user_identifier)

  begin
    @req_opts[:limit] = 1
    @req_opts[:identifier] = user_identifier.downcase
    response = @user_client.list_paginated_users(@app_id, @req_opts)
    users = response.users

    if users.length() == 0
      raise PassageError.new(
              message: "Passage User with identifer \"#{user_identifier}\" does not exist",
              status_code: 404,
              body: "user_not_found"
            )
    end
    return get(user_id: users.first().id)
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              message: "Passage User with identifer \"#{user_identifier}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              message: "failed to get Passage User.",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end

#list_devices(user_id:) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/passageidentity/user_api.rb', line 208

def list_devices(user_id:)
  user_exists?(user_id)

  begin
    response = @user_device_client.list_user_devices(@app_id, user_id, @req_opts)
    return response.devices
  rescue Faraday::Error => e
    raise PassageError.new(
            "failed to delete Passage User Device",
            status_code: e.response[:status],
            body: e.response[:body]
          )
  end
end

#signout(user_id:) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/passageidentity/user_api.rb', line 223

def signout(user_id:)
  warn "[DEPRECATION] `user.signout()` is deprecated.  Please use `auth.revoke_user_refresh_tokens()` instead."
  user_exists?(user_id)
  begin
    tokens_client = OpenapiClient::TokensApi.new
    response = tokens_client.revoke_user_refresh_tokens(@app_id, user_id, @req_opts)
    return true
  rescue Faraday::Error => e
    raise PassageError.new(
            "failed to revoke user's refresh tokens",
            status_code: e.response[:status],
            body: e.response[:body]
          )
  end
end

#update(user_id:, email: "", phone: "", user_metadata: {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/passageidentity/user_api.rb', line 125

def update(user_id:, email: "", phone: "", user_metadata: {})
  user_exists?(user_id)

  updates = {}
  updates["email"] = email unless email.empty?
  updates["phone"] = phone unless phone.empty?
  updates["user_metadata"] =  unless .empty?
  begin
    response = @user_client.update_user(@app_id, user_id, updates, @req_opts)
    return response.user
  rescue Faraday::Error => e
    if e.is_a? Faraday::ResourceNotFound
      raise PassageError.new(
              message: "Passage User with ID \"#{user_id}\" does not exist",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    else
      raise PassageError.new(
              "failed to update Passage User",
              status_code: e.response[:status],
              body: e.response[:body]
            )
    end
  end
end