Class: SocialNet::Facebook::Models::User

Inherits:
Object
  • Object
show all
Defined in:
lib/social_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.



12
13
14
15
16
17
18
19
20
# File 'lib/social_net/facebook/models/user.rb', line 12

def initialize(attrs = {})
  @id = attrs['id']
  @email = attrs['email']
  @gender = attrs['gender']
  @user_name = attrs[:user_name]
  @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.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def access_token
  @access_token
end

#emailObject (readonly)

Returns the value of attribute email.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def email
  @email
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def first_name
  @first_name
end

#genderObject (readonly)

Returns the value of attribute gender.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def gender
  @gender
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def last_name
  @last_name
end

#user_nameObject (readonly)

Returns the value of attribute user_name.



10
11
12
# File 'lib/social_net/facebook/models/user.rb', line 10

def user_name
  @user_name
end

Class Method Details

.find_by(params = {}) ⇒ SocialNet::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:



49
50
51
52
53
# File 'lib/social_net/facebook/models/user.rb', line 49

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

.find_by!(params = {}) ⇒ SocialNet::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:

  • (SocialNet::Errors::UnknownUser)

    if the user cannot be found.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/social_net/facebook/models/user.rb', line 65

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

#find_video(id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/social_net/facebook/models/user.rb', line 28

def find_video(id)
  request = Api::ScrapeRequest.new video_id: id, username: @user_name
  video = request.run
  Models::Video.new video
rescue Errors::ResponseError => error
  case error.response
  when Net::HTTPBadRequest then raise Errors::UnknownVideo
  when Net::HTTPNotFound then raise Errors::UnknownVideo
  end
end

#pagesObject



22
23
24
25
26
# File 'lib/social_net/facebook/models/user.rb', line 22

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