Class: Net::Facebook::Models::User

Inherits:
Object
  • Object
show all
Defined in:
lib/net/facebook/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ User

Returns a new instance of User.



10
11
12
13
14
15
16
17
# File 'lib/net/facebook/models/user.rb', line 10

def initialize(attrs = {})
  @id = attrs['id']
  @email = attrs['email']
  @gender = attrs['gender']
  @first_name = attrs['first_name']
  @last_name = attrs['last_name']
  @access_token = attrs['access_token']
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def access_token
  @access_token
end

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def first_name
  @first_name
end

#genderObject (readonly)

Returns the value of attribute gender.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def gender
  @gender
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



8
9
10
# File 'lib/net/facebook/models/user.rb', line 8

def last_name
  @last_name
end

Class Method Details

.find_by(params = {}) ⇒ Net::Facebook::Models::User

Returns the existing Facebook user matching the provided attributes or nil when the user is not found.

@ return [nil] when the user cannot be found.

Parameters:

  • params (Hash) (defaults to: {})

    the attributes to find a user by.

Options Hash (params):

  • :username (String)

    The Facebook user’s username (case-insensitive).

  • :access_token (String)

    The Facebook user’s access_token (case-insensitive).

Returns:



35
36
37
38
39
# File 'lib/net/facebook/models/user.rb', line 35

def self.find_by(params = {})
  find_by! params
rescue Errors::UnknownUser
  nil
end

.find_by!(params = {}) ⇒ Net::Facebook::Models::User

Returns the existing Facebook user matching the provided attributes or raises an error when the user is not found.

Parameters:

  • params (Hash) (defaults to: {})

    the attributes to find a user by.

Options Hash (params):

  • :username (String)

    The Facebook user’s username (case-insensitive).

  • :access_token (String)

    The Facebook user’s access_token (case-insensitive).

Returns:

Raises:

  • (Net::Errors::UnknownUser)

    if the user cannot be found.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/net/facebook/models/user.rb', line 51

def self.find_by!(params = {})
  request = Api::Request.new params
  if params[:access_token]
    new request.run.merge!({"access_token" => params[:access_token]})
  else
    new request.run
  end
rescue Errors::ResponseError => error
  case error.response
    when Net::HTTPNotFound then raise Errors::UnknownUser
  end
end

Instance Method Details

#pagesObject



19
20
21
22
23
# File 'lib/net/facebook/models/user.rb', line 19

def pages
  request = Api::Request.new access_token: @access_token, path: "/v2.3/#{@id}/accounts"
  page_json = request.run
  page_json['data'].map { |h| h.slice("name", "id") } if page_json['data'].any?
end