Module: UsernameCheck

Defined in:
lib/username_check.rb

Constant Summary collapse

SITES =
{
  :twitter => 'twitter.com/',
  :github => 'github.com/',
  :flickr => 'flickr.com/photos/',
  :lastfm => 'last.fm/user/',
  :blogspot => '%s.blogspot.com',
  '%s.com' => ['%s.com', :whois],
  '%s.net' => ['%s.net', :whois],
  '%s.org' => ['%s.org', :whois],
  :delicious => 'delicious.com/',
  :friendfeed => 'friendfeed.com/',
  "friendfeed room" => 'friendfeed.com/rooms/',
  :hulu => 'hulu.com/profiles/',
  :disqus => 'disqus.com/people/',
  :linkedin => ['www.linkedin.com/in/', :match, 'contact settings'],
  :stumbleupon => ['stumbleupon.com/stumbler/', :not_match, 'no such username'],
  :soundcloud => 'soundcloud.com/',
  :hulu => 'hulu.com/profiles/',
  :slashdot => ['slashdot.org/~', :not_match, 'does not exist'],
  :plurk => ['plurk.com/', :not_match, 'register this nick name'],
  :myopenid => '%s.myopenid.com',
  :tumblr => ['%s.tumblr.com', :not_match, "We couldn't find the page you were looking for"],
  :vimeo => 'vimeo.com/',
  :youtube => 'youtube.com/',
  :deviantart => '%s.deviantart.com',
  :reddit => 'reddit.com/user/',
  :livejournal => 'community.livejournal.com/',
  :ustream => 'ustream.tv/',
}

Class Method Summary collapse

Class Method Details

.check_all(name) ⇒ Object



34
35
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/username_check.rb', line 34

def self.check_all(name)
  memo_mutex = Mutex.new
  memo = {}
  threads = []
  SITES.each do |site_and_options|
    threads << Thread.new do
      site, address, condition, *args = site_and_options.flatten
      condition ||= :is_200
      rendered_address = 
        if address.include?('%s')
          address % name
        else
          address + name
        end
    
    
      service = site.to_s % name
      in_use = send(condition, rendered_address, *args)
      memo_mutex.synchronize do
        yield(service, in_use) if block_given?
        memo[service] = in_use
      end
    end
  end
  threads.each { |t| t.join }
  memo
end

.get(address) ⇒ Object



62
63
64
# File 'lib/username_check.rb', line 62

def self.get(address)
  HTTParty.get("http://#{address}")
end

.is_200(address) ⇒ Object



66
67
68
69
70
71
# File 'lib/username_check.rb', line 66

def self.is_200(address)
  begin
    get(address).code.to_i == 200
  rescue
  end
end

.match(address, regexp) ⇒ Object



73
74
75
# File 'lib/username_check.rb', line 73

def self.match(address, regexp)
  !!(get(address).body =~ regexify(regexp))
end

.not_match(address, regexp) ⇒ Object



77
78
79
# File 'lib/username_check.rb', line 77

def self.not_match(address, regexp)
  !(get(address).body =~ regexify(regexp))
end

.regexify(regexp) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/username_check.rb', line 81

def self.regexify(regexp)
  if regexp.is_a?(Regexp)
    regexp
  else
    Regexp.new(regexp, true)
  end
end

.whois(domain) ⇒ Object



89
90
91
92
# File 'lib/username_check.rb', line 89

def self.whois(domain)
  # FIXME: seems to be broken on .org
  `whois #{domain.inspect} | head -n 10 | grep "No match for" || echo 1`.chomp == '1'
end