Module: Twhois

Defined in:
lib/twhois.rb,
lib/twhois/user.rb,
lib/twhois/version.rb

Defined Under Namespace

Classes: InvalidUsername, User

Constant Summary collapse

LOOKUP_HOST =
"api.twitter.com"
LOOKUP_PATH =
"/1/users/show.json?screen_name="
VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.lookup(username) ⇒ Object

Lookup a Twitter user by their username.

Raises:



18
19
20
21
22
23
24
# File 'lib/twhois.rb', line 18

def self.lookup(username)
  raise InvalidUsername, "Username is invalid" unless valid_username?(username)
  res = Net::HTTP.start(LOOKUP_HOST) { |http| http.get(LOOKUP_PATH + username) }
  if res.code == '200'
    User.new(JSON.parse(res.body))
  end
end

.valid_username?(username) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/twhois.rb', line 26

def self.valid_username?(username)
  return false if username.match(/^[a-zA-Z0-9_]{1,15}$/).nil?
  return true
end