Class: SocialNet::Byte::Models::User

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



17
18
19
# File 'lib/social_net/byte/models/user.rb', line 17

def initialize(attrs = {})
  attrs.each{|k, v| instance_variable_set("@#{k}", v) unless v.nil?}
end

Instance Attribute Details

#avatar_urlObject (readonly)

Returns the value of attribute avatar_url.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def avatar_url
  @avatar_url
end

#bioObject (readonly)

Returns the value of attribute bio.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def bio
  @bio
end

#display_nameObject (readonly)

Returns the value of attribute display_name.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def display_name
  @display_name
end

#follower_countObject (readonly)

Returns the value of attribute follower_count.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def follower_count
  @follower_count
end

#following_countObject (readonly)

Returns the value of attribute following_count.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def following_count
  @following_count
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def id
  @id
end

#registration_dateObject (readonly)

Returns the value of attribute registration_date.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def registration_date
  @registration_date
end

#usernameObject (readonly)

Returns the value of attribute username.



8
9
10
# File 'lib/social_net/byte/models/user.rb', line 8

def username
  @username
end

Class Method Details

.find_by(params = {}) ⇒ SocialNet::Byte::Models::User?

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

Parameters:

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

    the attributes to find a user by.

Options Hash (params):

  • :username (String)

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

  • :id (String)

    The Byte user’s id (case-insensitive).

Returns:



51
52
53
54
55
# File 'lib/social_net/byte/models/user.rb', line 51

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

.find_by!(params = {}) ⇒ SocialNet::Byte::Models::User

Returns the existing Byte user matching the provided attributes or nil when the user is not found, and raises an error when the user account is private.

Parameters:

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

    the attributes to find a user by.

Options Hash (params):

  • :username (String)

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

  • :id (String)

    The Byte user’s id (case-insensitive).

Returns:

Raises:

  • (SocialNet::Errors::UnknownUser)

    if the user account is unknown.



67
68
69
70
71
72
73
# File 'lib/social_net/byte/models/user.rb', line 67

def self.find_by!(params = {})
  if params[:username]
    find_by_username! params[:username]
  elsif params[:id]
    find_by_id! params[:id]
  end
end

Instance Method Details

#posts(opts = {}) ⇒ SocialNet::Byte::Models::Post

Returns the existing Byte user’s most recent posts

@ param [Hash] params the attributes to find paginated posts by.

Parameters:

  • params (Hash)

    a customizable set of options

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/social_net/byte/models/user.rb', line 26

def posts(opts={})
  params = {endpoint: "/account/id/#{@id}/posts"}.merge! opts
  request = Api::Request.new params
  posts_data = request.run
  {}.tap do |k,v|
    k[:posts] = posts_data['data']['posts'].map{|post| Post.new post.deep_transform_keys { |key| key.underscore.to_sym }}
    k[:next_page] = posts_data['data']['cursor']
  end
rescue Errors::ResponseError => error
  case error.response
    when Net::HTTPBadRequest then raise Errors::UnknownUser
    when Net::HTTPNotFound then raise Errors::UnknownUser
  end
end