Class: SocialPlus::WebApi::User

Inherits:
Object
  • Object
show all
Defined in:
lib/social_plus/web_api/user.rb

Overview

A class which represents a user of Social Plus

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ User

Returns a new instance of User.

Parameters:

  • params (hash)

    User information obtained from Social Plus Web API

Options Hash (params):

  • "user" (Hash)

    User

  • "profile" (Hash)

    User’s profile

  • "email" (Array)

    User’s email addresses

  • "follow" (Hash)

    User’s counts of following/followed people

  • "providers" (Hash)

    Providers which the user has logged in

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/social_plus/web_api/user.rb', line 32

def initialize(params)
  raise ArgumentError, %q|missing 'user'| unless params.key?('user')
  user = params['user']
  raise ArgumentError, %q|missing 'user/identifier'| unless user.key?('identifier')
  @identifier = user['identifier']
  last_logged_in_provider = user['last_logged_in_provider'] || ''
  @last_logged_in_provider = last_logged_in_provider.inquiry.freeze
  @profile = SocialPlus::WebApi::Profile.new(params.slice('profile', 'email')).freeze
  @followers = params.key?('follow') && params['follow'].key?('followed_by') ? params['follow']['followed_by'] : 0
  @providers = params['providers']
end

Instance Attribute Details

#followersInteger (readonly)

Returns The number of user’s followers(reaches).

Returns:

  • (Integer)

    The number of user’s followers(reaches)



53
54
55
# File 'lib/social_plus/web_api/user.rb', line 53

def followers
  @followers
end

#identifierString (readonly)

Returns The user’s Social Plus ID.

Returns:

  • (String)

    The user’s Social Plus ID



47
48
49
# File 'lib/social_plus/web_api/user.rb', line 47

def identifier
  @identifier
end

#last_logged_in_providerActiveSupport::StringInquirer (readonly)

Returns The provider which the user has logged in most recently.

Returns:

  • (ActiveSupport::StringInquirer)

    The provider which the user has logged in most recently



49
50
51
# File 'lib/social_plus/web_api/user.rb', line 49

def last_logged_in_provider
  @last_logged_in_provider
end

#profileProfile (readonly)

Returns The user’s profile.

Returns:

  • (Profile)

    The user’s profile



51
52
53
# File 'lib/social_plus/web_api/user.rb', line 51

def profile
  @profile
end

#providersArray (readonly)

Returns The Providers which the user has logged in.

Returns:

  • (Array)

    The Providers which the user has logged in



55
56
57
# File 'lib/social_plus/web_api/user.rb', line 55

def providers
  @providers
end

Class Method Details

.authenticate(api_client, token) ⇒ User

Fetch information of a user using a given one time token

Parameters:

  • api_client (Client)

    an API client

  • token (String)

    the token returned from Social Plus login

Returns:

  • (User)

    User object



16
17
18
19
20
# File 'lib/social_plus/web_api/user.rb', line 16

def authenticate(api_client, token)
  result = api_client.execute('authenticated_user', token: token, add_profile: true)
  result['providers'] = api_client.execute('providers_of_user', identifier: result['user']['identifier'])['providers']
  new(result)
end