Class: Waylon::RSpec::TestUser
- Inherits:
-
Object
- Object
- Waylon::RSpec::TestUser
- Includes:
- User
- Defined in:
- lib/waylon/rspec/test_user.rb
Overview
Extras for RSpec to facilitate testing Waylon (by creating fake Users)
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.email_from_name(name) ⇒ String
private
Creates an email address based on the name provided.
-
.find_by_email(email) ⇒ User?
Looks up a User by their email.
-
.find_by_handle(handle) ⇒ User?
Looks up a User by their IM handle.
-
.find_by_name(name) ⇒ User?
Looks up a User by their full name.
-
.find_or_create(name: nil, email: nil, handle: nil) ⇒ User, Boolean
Looks up existing or creates a new User based on their full name, email, or handle rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity.
-
.handle_from_name(name) ⇒ String
private
Creates a handle from a name.
-
.random_name ⇒ String
Provides a random human-sounding full name for test users.
-
.whoami ⇒ TestUser
Gives back the TestUser for the bot.
Instance Method Summary collapse
-
#details ⇒ Hash
private
Lazily provides the details for a TestUser.
-
#display_name ⇒ String
The User’s full name (:user from the details Hash).
-
#email ⇒ String
The User’s email address.
-
#handle ⇒ String
The User’s handle.
-
#initialize(user_id, details = {}) ⇒ TestUser
constructor
A new instance of TestUser.
-
#private_message(content) ⇒ TestMessage
Sends a direct TestMessage to a User.
-
#status ⇒ Symbol
The User’s current status.
-
#valid? ⇒ Boolean
Is the User valid?.
Methods included from User
Constructor Details
#initialize(user_id, details = {}) ⇒ TestUser
Returns a new instance of TestUser.
88 89 90 91 |
# File 'lib/waylon/rspec/test_user.rb', line 88 def initialize(user_id, details = {}) @id = user_id.to_i @details = details end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/waylon/rspec/test_user.rb', line 9 def id @id end |
Class Method Details
.email_from_name(name) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an email address based on the name provided
149 150 151 152 153 154 155 |
# File 'lib/waylon/rspec/test_user.rb', line 149 def self.email_from_name(name) if ENV.fetch("USER_EMAIL", nil) && name == "homer.simpson" ENV.fetch("USER_EMAIL", nil) else "#{name.downcase.gsub(/[\s_-]/, ".")}@example.com" end end |
.find_by_email(email) ⇒ User?
Looks up a User by their email
14 15 16 |
# File 'lib/waylon/rspec/test_user.rb', line 14 def self.find_by_email(email) TestSense.user_list.find { |user| user.email == email } end |
.find_by_handle(handle) ⇒ User?
Looks up a User by their IM handle
21 22 23 |
# File 'lib/waylon/rspec/test_user.rb', line 21 def self.find_by_handle(handle) TestSense.user_list.find { |user| user.handle == handle } end |
.find_by_name(name) ⇒ User?
Looks up a User by their full name
28 29 30 |
# File 'lib/waylon/rspec/test_user.rb', line 28 def self.find_by_name(name) TestSense.user_list.find { |user| user.display_name == name } end |
.find_or_create(name: nil, email: nil, handle: nil) ⇒ User, Boolean
Looks up existing or creates a new User based on their full name, email, or handle rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/waylon/rspec/test_user.rb', line 38 def self.find_or_create(name: nil, email: nil, handle: nil) return false unless name || email || handle # have to provide _something_ existing_user = find_by_email(email) || find_by_handle(handle) || find_by_name(name) if existing_user existing_user else this_name = name || random_name # if no name was provided, make one up details = { email: email || email_from_name(this_name), handle: handle || handle_from_name(this_name), name: this_name, status: :online } # Need to give up if we've generated a duplicate if find_by_email(details[:email]) || find_by_handle(details[:handle]) || find_by_name(details[:name]) return false end TestSense.add_user_from_details(details) end end |
.handle_from_name(name) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a handle from a name
160 161 162 |
# File 'lib/waylon/rspec/test_user.rb', line 160 def self.handle_from_name(name) name.downcase.split(/[\s_-]/).first end |
.random_name ⇒ String
Provides a random human-sounding full name for test users
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/waylon/rspec/test_user.rb', line 64 def self.random_name first_names = %w[ Abraham Al Alex Barbara Barry Bob Brenda Chloe Chuck Daniel Dave Eliza Felicia Frank Francis Glen Graham Greg Hal Jackie Jacob Jessica Jonathan Julie Maria Marcia Nikhil Olivia Patrick Paul Reggie Robby Roger Sam Saul Sean Tim Todd Tristan Xavier Zack ] last_names = %w[ Adams Andrews Bailey Brooks Brown Bush Cervantes Chen Collins Crooks Dean Franz Harris Jackson Jimenez Jones Jordan Laflor Lopez Gonzalez McDowell Miller Ng Odinson Reed Roberts Rodriguez Sanders Schmidt Scott Smith Stewart Taylor Tesla Torres Turner Walker Ward Warner White Williams Wilson Wong Young Zeta Zimmerman ] "#{first_names.sample} #{last_names.sample}" end |
.whoami ⇒ TestUser
Gives back the TestUser for the bot
82 83 84 |
# File 'lib/waylon/rspec/test_user.rb', line 82 def self.whoami find_by_email("[email protected]") end |
Instance Method Details
#details ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Lazily provides the details for a TestUser
141 142 143 144 |
# File 'lib/waylon/rspec/test_user.rb', line 141 def details @details = TestSense.user_list[id].details if @details.empty? @details.dup end |
#display_name ⇒ String
The User’s full name (:user from the details Hash)
95 96 97 |
# File 'lib/waylon/rspec/test_user.rb', line 95 def display_name details[:name] end |
#email ⇒ String
The User’s email address
101 102 103 |
# File 'lib/waylon/rspec/test_user.rb', line 101 def email details[:email] end |
#handle ⇒ String
The User’s handle
122 123 124 |
# File 'lib/waylon/rspec/test_user.rb', line 122 def handle details[:handle] end |
#private_message(content) ⇒ TestMessage
Sends a direct TestMessage to a User
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/waylon/rspec/test_user.rb', line 108 def (content) msg = { user_id: self.class.whoami.id, receiver_id: id, text: content, type: :private, created_at: Time.now } TestSense. << msg TestMessage.new(TestSense..size - 1) end |
#status ⇒ Symbol
The User’s current status
128 129 130 |
# File 'lib/waylon/rspec/test_user.rb', line 128 def status details[:status] end |
#valid? ⇒ Boolean
Is the User valid?
134 135 136 |
# File 'lib/waylon/rspec/test_user.rb', line 134 def valid? true end |