Class: SocialUsernameChecker
- Inherits:
-
Object
- Object
- SocialUsernameChecker
- Defined in:
- lib/social_username_checker.rb
Overview
SocialUsernameChecker checks username availability across multiple social platforms.
Constant Summary collapse
- PLATFORMS =
Supported platforms and their URL patterns for usernames
{ github: 'https://github.com/%<username>s', twitter: 'https://twitter.com/%<username>s', instagram: 'https://www.instagram.com/%<username>s', facebook: 'https://www.facebook.com/%<username>s', youtube: 'https://www.youtube.com/@%<username>s', tiktok: 'https://www.tiktok.com/@%<username>s', pinterest: 'https://www.pinterest.com/%<username>s', linkedin: 'https://www.linkedin.com/in/%<username>s', reddit: 'https://www.reddit.com/user/%<username>s', threads: 'https://www.threads.net/@%<username>s' }.freeze
Instance Method Summary collapse
-
#supported_platforms ⇒ Object
Returns an array of supported platform names as strings.
-
#username_available?(username, platform) ⇒ Boolean
Check if a username is available on a given platform.
Instance Method Details
#supported_platforms ⇒ Object
Returns an array of supported platform names as strings
Returns Array<String>
26 27 28 |
# File 'lib/social_username_checker.rb', line 26 def supported_platforms PLATFORMS.keys.map(&:to_s) end |
#username_available?(username, platform) ⇒ Boolean
Check if a username is available on a given platform.
username - String username to check platform - Symbol or String platform key (e.g., :github or “github”)
Returns Boolean true if available, false if taken or on error.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/social_username_checker.rb', line 36 def username_available?(username, platform) platform_key = platform.to_sym unless PLATFORMS.key?(platform_key) warn "Unsupported platform: #{platform}" return false end url = format(PLATFORMS[platform_key], username: username) uri = URI.parse(url) response = fetch_response(uri) # If HTTP status is not 200, assume username is available (profile not found) return true unless response.code == '200' # Check response body for username presence (basic heuristic) body = response.body.force_encoding('UTF-8') # If the username appears in the page content, it's likely taken !body.include?(username) rescue StandardError => e warn "⚠️ Error checking #{platform}: #{e.message}".colorize(:yellow) false end |