Module: TwitterUsernameExtractor

Defined in:
lib/twitter_username_extractor.rb,
lib/twitter_username_extractor/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.extract(t) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/twitter_username_extractor.rb', line 6

def self.extract(t)
  return if t.to_s.empty?
  return Regexp.last_match(1) if t.match(/^\@(\w+)$/)
  return Regexp.last_match(1) if t.match(/^(\w+)$/)
  return Regexp.last_match(1) if t.match(%r{(?:www.)?twitter.com/@?(\w+)$}i)

  # Odd special cases
  return Regexp.last_match(1) if t.match(%r{twitter.com/search\?q=%23(\w+)}i)
  return Regexp.last_match(1) if t.match(%r{twitter.com/#!/https://twitter.com/(\w+)}i)
  return Regexp.last_match(1) if t.match(%r{(?:www.)?twitter.com/#!/(\w+)[/\?]?}i)
  return Regexp.last_match(1) if t.match(%r{(?:www.)?twitter.com/@?(\w+)[\/]?}i)
  fail Error, "Unknown twitter handle: #{t}"
end