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

Public: FakeFriend.new(username) Creates a FakeFriend object with attributes fetched from the user listing defined in users.yml and accesses via FakeFriend.list

username - String - a Twitter username found in the user listing

Examples

FakeFriend.new('idiot')
# => #<FakeFriend:0x00000101348a80 @username="idiot"...>

Returns a FakeFriend object with attributes populated from FakeFriend.list



65
66
67
68
69
70
71
72
# File 'lib/fake_friends.rb', line 65

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

Public: FakeFriend.find_by(options) Returns a FakeFriend object for a specific user in the user listing

options - The Hash of options (default: {}):

:id - Integer - User's position in the user listing, 1 to 101
:username - String - User's Twitter username

Examples

FakeFriend.find_by(id: 101)
# => #<FakeFriend:0x007ff0f286e2d8 ...>

Returns the requested FakeFriend object if found, else raises ArgumentError



42
43
44
45
46
47
48
49
50
51
# File 'lib/fake_friends.rb', line 42

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

Public: FakeFriend.gather(n) Returns n FakeFriend objects

n - An Integer from 1 to 101.

Examples

FakeFriend.gather(2)
# => [#<FakeFriend:0x00..>, #<FakeFriend:0x00..>]

Returns an array of n FakeFriend objects

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/fake_friends.rb', line 23

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

Public: returns a user’s uiFaces url in the closest available size

size - Integer - the requested image size (length = width), in pixels

Returns a string with the appropriate url.



79
80
81
82
83
# File 'lib/fake_friends.rb', line 79

def avatar_url(size)
  valid_sizes = [128, 73, 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