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
-
.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.list.
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
#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
.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() if [:id] && [:id].between?(1, FakeFriend.list.count) username = FakeFriend.list.keys[[:id]-1] FakeFriend.new(username) elsif [:username] && FakeFriend.list.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 101.
Examples
FakeFriend.gather(2)
# => [#<FakeFriend:0x00..>, #<FakeFriend:0x00..>]
Returns an array of n FakeFriend objects
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 |