Class: SBF::Client::UserEndpoint

Inherits:
EntityEndpoint show all
Defined in:
lib/stbaldricks/endpoints/user.rb

Direct Known Subclasses

Facebook::UserEndpoint

Instance Attribute Summary

Attributes inherited from EntityEndpoint

#orig_target_class

Instance Method Summary collapse

Methods inherited from EntityEndpoint

#aggregate, #create, #delete, #find, #find_first, #get, #initialize, #save, #update

Constructor Details

This class inherits a constructor from SBF::Client::EntityEndpoint

Instance Method Details

#change_password(profile_id, password) ⇒ Object

Changes the password of the user associated with the given profile_id

Raises:

  • (SBFClientError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stbaldricks/endpoints/user.rb', line 61

def change_password(profile_id, password)
  raise SBF::Client::Error, 'User not logged in' if SBF::Client::Configuration.user_token.nil?

  response = SBF::Client::Api::Request.post_request(
    "/#{SBF::Client::Api::VERSION}/security/change_password",
    profile_id: profile_id,
    password: password
  )

  unless ok?(response)
    parsed_response = JSON.parse(response.body).symbolize!
    error = SBF::Client::ErrorEntity.new(parsed_response)
  end
  SBF::Client::Configuration.user_token = nil

  SBF::Client::LOG.info { "SBF::Client - Change Password for user with profile_id: #{profile_id}" }
  SBF::Client::Api::Response.new(http_code: response.code, data: nil, error: error)
end

#disconnect_facebook(profile_id) ⇒ Object

Removes the Facebook user of the given profile_id

Raises:

  • (SBFClientError)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stbaldricks/endpoints/user.rb', line 83

def disconnect_facebook(profile_id)
  raise SBF::Client::Error, 'User not logged in' if SBF::Client::Configuration.user_token.nil?

  response = SBF::Client::Api::Request.post_request(
    "/#{SBF::Client::Api::VERSION}/user/disconnect_facebook",
    profile_id: profile_id
  )

  SBF::Client::Configuration.user_token = nil

  unless ok?(response)
    parsed_response = JSON.parse(response.body).symbolize!
    error = SBF::Client::ErrorEntity.new(parsed_response)
  end

  SBF::Client::LOG.info { "SBF::Client - Disconnecting Facebook user with profile_id: #{profile_id}" }
  SBF::Client::Api::Response.new(http_code: response.code, data: nil, error: error)
end

#list_entities_user_has_permission(permission_id, event_years) ⇒ Object

Returns array of entity hash info for the entities logged in user has access to for requested permission.

Raises:

  • (SBFClientError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/stbaldricks/endpoints/user.rb', line 36

def list_entities_user_has_permission(permission_id, event_years)
  raise SBF::Client::Error, 'User not logged in' if  SBF::Client::Configuration.user_token.nil?

  event_years = [event_years] if event_years.is_a? Integer

  response = SBF::Client::Api::Request.post_request(
    "/#{SBF::Client::Api::VERSION}/security/list_entities_user_has_permission",
    permission_id: permission_id,
    event_years: event_years
  )
  parsed_response = JSON.parse(response.body).symbolize!

  if ok?(response)
    data = parsed_response
  else
    error = SBF::Client::ErrorEntity.new(parsed_response)
  end

  SBF::Client::LOG.info { "SBF::Client - User List Entities User Has Permission: #{permission_id}, #{event_years}, Entities: #{data}" }
  SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error)
end

#login!(username, password) ⇒ string

Logs in a user and configures the client library to use user credentials for subsequent requests. Returns the access token for the user that should be held onto to configure the client library in future requests that are outside of this scope.

Parameters:

  • username (string)
  • password (string)

Returns:

  • (string)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stbaldricks/endpoints/user.rb', line 13

def login!(username, password)
  response = SBF::Client::Api::Request.post_request(
    "/#{SBF::Client::Api::VERSION}/security/login",
    username: username,
    password: password
  )
  parsed_response = JSON.parse(response.body).symbolize!

  if ok?(response)
    SBF::Client::Configuration.user_token = parsed_response[:token]
    data = parsed_response.merge(user: target_class.new(parsed_response[:user]))
  else
    SBF::Client::Configuration.user_token = nil
    error = SBF::Client::ErrorEntity.new(parsed_response)
  end

  SBF::Client::LOG.info { "SBF::Client - User Login: #{username}, Token: #{parsed_response[:token]}" }
  SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error)
end