Class: FakeFriends::FakeFriend
- Inherits:
-
Object
- Object
- FakeFriends::FakeFriend
- Defined in:
- lib/fake_friends.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.all ⇒ Object
Public: FakeFriend.all.
-
.find_by(options) ⇒ Object
Public: FakeFriend.find_by(options) Returns a FakeFriend object for a specific user in the user listing.
-
.gather(n) ⇒ Object
Public: FakeFriend.gather(n) Returns n FakeFriend objects.
Instance Method Summary collapse
-
#avatar_url(size) ⇒ Object
Public: returns a user’s uiFaces url in the closest available size.
-
#initialize(username) ⇒ FakeFriend
constructor
Public: FakeFriend.new(username) Creates a FakeFriend object with attributes fetched from the user listing defined in users.yml and accesses via FakeFriend.all.
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.all
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.all
73 74 75 76 77 78 79 80 |
# File 'lib/fake_friends.rb', line 73 def initialize(username) @username = username @name = FakeFriend.all[username][:name] @location = FakeFriend.all[username][:location] @description = FakeFriend.all[username][:description] @url = FakeFriend.all[username][:url] @posts = FakeFriend.all[username][:posts] end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def description @description end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def name @name end |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def posts @posts end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def url @url end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
10 11 12 |
# File 'lib/fake_friends.rb', line 10 def username @username end |
Class Method Details
.all ⇒ Object
Public: FakeFriend.all
Returns a class instance Hash variable holding the
user list defined in users.yml
16 17 18 |
# File 'lib/fake_friends.rb', line 16 def self.all @friends_list end |
.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 100
:username - String - User's Twitter username
Examples
FakeFriend.find_by(id: 101)
# => #<FakeFriend:0x007ff0f286e2d8 ...>
Returns the requested FakeFriend object if found, else raises ArgumentError
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fake_friends.rb', line 50 def self.find_by() if [:id] && [:id].between?(1, FakeFriend.all.count) username = FakeFriend.all.keys[[:id]-1] FakeFriend.new(username) elsif [:username] && FakeFriend.all.keys.include?([:username]) FakeFriend.new([: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 100.
Examples
FakeFriend.gather(2)
# => [#<FakeFriend:0x00..>, #<FakeFriend:0x00..>]
Returns an array of n FakeFriend objects
31 32 33 34 35 |
# File 'lib/fake_friends.rb', line 31 def self.gather(n) raise ArgumentError, "Can only gather 1 to 100 FakeFriends" unless n.between?(1, 100) users = FakeFriend.all.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.
87 88 89 90 91 |
# File 'lib/fake_friends.rb', line 87 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 |