Class: FakeFriends::FakeFriend

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_friends.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username) ⇒ FakeFriend

FakeFriend.new(username)

username <string>

twitter username

returns user object



52
53
54
55
56
57
58
59
# File 'lib/fake_friends.rb', line 52

def initialize(username)
  @username    = username
  @name        = FakeFriend.list[username][:name]
  @location    = FakeFriend.list[username][:location]
  @description = FakeFriend.list[username][:description]
  @url         = FakeFriend.list[username][:url]
  @posts       = FakeFriend.list[username][:posts]
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/fake_friends.rb', line 10

def description
  @description
end

#locationObject (readonly)

Returns the value of attribute location.



10
11
12
# File 'lib/fake_friends.rb', line 10

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/fake_friends.rb', line 10

def name
  @name
end

#postsObject (readonly)

Returns the value of attribute posts.



10
11
12
# File 'lib/fake_friends.rb', line 10

def posts
  @posts
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/fake_friends.rb', line 10

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/fake_friends.rb', line 10

def username
  @username
end

Class Method Details

.find_by(options) ⇒ Object

FakeFriend.find_by(options)

options <hash> id: n <int>

position in the users list, 1-101

username: str <string>

twitter username

Example: FakeFriend.find_by(id: 101)

> #<User:0x007ff0f286e2d8 …>

returns the requested user object



34
35
36
37
38
39
40
41
42
43
# File 'lib/fake_friends.rb', line 34

def self.find_by(options)
  if options[:id] && options[:id].between?(1, FakeFriend.list.count)
    username = FakeFriend.list.keys[options[:id]-1]
    FakeFriend.new(username)
  elsif options[:username] && FakeFriend.list.keys.include?(options[:username])
    FakeFriend.new(options[:username])
  else
    raise ArgumentError, "Requested user not found in library."
  end
end

.gather(n) ⇒ Object

Friend.gather(n) returns an array of n user objects

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'lib/fake_friends.rb', line 14

def self.gather(n)
  raise ArgumentError, "Can only gather 1 to 101 FakeFriends" unless n.between?(1, 101)
  users = FakeFriend.list.keys.sample(n)
  users.map{ |username| FakeFriend.new(username) }
end

Instance Method Details

#avatar_url(size) ⇒ Object

avatar_url(size) returns the user’s uiFaces url in the closest available size



63
64
65
66
67
# File 'lib/fake_friends.rb', line 63

def avatar_url(size)
  valid_sizes = [128, 64, 48, 24]
  size = valid_sizes.min { |a,b| (size-a).abs <=> (size-b).abs }
  "https://s3.amazonaws.com/uifaces/faces/twitter/#{username}/#{size}.jpg"
end