Class: SkypeCheck::Username

Inherits:
Object
  • Object
show all
Defined in:
lib/skype_check/username.rb

Class Method Summary collapse

Class Method Details

.is_available?(username) ⇒ Boolean

when it comes to web services it’s better to be explicit with semantic meanings of http return codes, it’s better than calling !is_taken?(username) here.

Returns:

  • (Boolean)


26
27
28
# File 'lib/skype_check/username.rb', line 26

def is_available?(username)
  query(username)["status"] == 200
end

.is_taken?(username) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/skype_check/username.rb', line 21

def is_taken?(username)
  query(username)["status"] == 406 
end

.query(username) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/skype_check/username.rb', line 11

def query(username)
  raise SkypeCheck::UsernameValidationError, "Input username is nil!" if username.nil?
  raise SkypeCheck::UsernameValidationError, "Input username is too long!" if username.size > 32
  raise SkypeCheck::UsernameValidationError, "Input username is too short!" if username.size < 6
  raise SkypeCheck::UsernameValidationError, "Input username should not start with a number!" if username.match(/\A\d/)

  resp_body = SkypeCheck::HTTPService.http_query(username).body
  JSON.parse(resp_body)
end