class Moriarty

Main class to get data and execute request

Methods: [ new, go, success?, make_url, url=, user= ]

Attributes:

:user     = :moriarty     => 'moriarty'
:url      = 'example.com' => 'https://example.com/'
:response => [.code, .headers, .body]  -> restclient#get
:html     => scrapped HTML if success? -> nokogiri#html

For CLI use, return colorized output Accepted args: ( name, site, type )

name is required ~ (string or :symbol) site is optional ~ (default 'github.com')

Attributes

html[R]
response[R]
url[R]
user[R]

Public Class Methods

find!( username, web = 'github.com', type = :search ) click to toggle source

Moriarty.find! 'smartuser'

=> [FOUND!] if username 'smartuser' is free on github

Moriarty.find! :stupiduser, 'facebook.com', :hunt

=> [FREE!] if user 'stupiduser' is registered on facebook
# File lib/moriarty/cli.rb, line 15
def self.find!( username, web = 'github.com', type = :search )

  @jim = Moriarty.new username.to_s, web.to_s
  @jim.go

  name = print_name(username).to_s
  site = print_url(web).to_s

  case    
  when type.to_sym == :hunt && @jim.success?
    p1('+')
    p1('FOUND!')
    p2(" #{name}", :cyan, :bold)
    p2(" found on >> ") 
    puts p2(site, :cyan, :bold)
  when type.to_sym == :hunt && !@jim.success?
    p1('-', :red, :red)
    p2(" #{name} fail on ", :red)
    puts p2(site, :red)
 when @jim.success?
    p1('-', :red, :red)
    p2(" #{name} is taken on ", :red)
    puts p2(site, :red)
  else
    p1('+')
    p1('FREE!')
    p2(" #{name}", :cyan, :bold)
    p2(" is free on >> ")
    puts p2(site, :cyan, :bold)
  end
end
hunt!( name, site = 'github.com', type = :hunt) click to toggle source

hunt! is alias for find! with :hunt option Check is user 'stupiduser' registered on instagram

-> Moriarty.hunt! 'stupiduser', 'instagram.com'
# File lib/moriarty/cli.rb, line 51
def self.hunt!( name, site = 'github.com', type = :hunt)
  find! name, site, type
end
new( name = '', site = 'github.com', type = :https ) click to toggle source

Set username and site for search request exclude 'https', make_url will add it for you To use different protocol, set it as third parameter

@jim = Moriarty.new( 'moriarty', 'example.com', :http )

=> @jim.user == 'moriarty'
=> @jim.url  == 'http://example.com/'
# File lib/moriarty.rb, line 31
def initialize( name = '', site = 'github.com', type = :https )
  @user = name.to_s
  @url  = make_url site, type
end
p1( title, color1 = :'light_green', color2 = :cyan, type = :bold ) click to toggle source
# File lib/moriarty/cli.rb, line 82
def self.p1( title, color1 = :'light_green', color2 = :cyan, type = :bold )
  str = ' ['.colorize(color2) + title.to_s.colorize(color1) + ']'.colorize(color2)
  str = str.bold if type == :bold
  print str
end
p2( title, color = :cyan, type = :regular) click to toggle source
# File lib/moriarty/cli.rb, line 88
def self.p2( title, color = :cyan, type = :regular)
  str = title.colorize(color)
  str = str.bold if type == :bold
  print str
end
print_name( name ) click to toggle source

Remove extensions from username Moriarty.print_name('@moriarty')

=> 'moriarty'
print_url( site ) click to toggle source

Remove extensions from domain name Moriarty.print_url('www.github.com')

=> 'github'

Public Instance Methods

go( opt = {} ) click to toggle source

execute request (args are optional) @jim.go site: 'github.com', user: 'mynickname'

=> true/false
-> @jim.response (.code, .headers, .body)
-> @jim.html (page HTML if request success)
# File lib/moriarty.rb, line 42
def go( opt = {} )
  opt[:user] ||= @user
  url = opt[:site].nil? ? @url : make_url(opt[:site])
  uri = url + opt[:user]
  @response = RestClient.get uri
  @html     = Nokogiri::HTML @response
  return @success = true
  rescue
  return @success = false
end
Also aliased as: search
make_url( link, prot = :https ) click to toggle source

create URL from site name, add 'https' if needed @jim.make_url 'instagram.com'

=> 'https://instagram.com/'
# File lib/moriarty.rb, line 59
def make_url( link, prot = :https )
  prot = nil if link.to_s.start_with? prot.to_s
  url  = prot.nil? ? link.to_s : prot.to_s + '://' + link.to_s
  url += '/' unless url.end_with?('/')
  return url
end
success?() click to toggle source

Check does request succeeded or not @jim.success?

=> true/false
# File lib/moriarty.rb, line 86
def success?
  @success == true
end
url=( link, start = :https ) click to toggle source

Set URL from site name and protocol(optional) @jim.url = 'github.com'

=> @jim.url == 'https://github.com'
# File lib/moriarty.rb, line 70
def url=( link, start = :https )
  @url = make_url link, start
end
user=( name ) click to toggle source

Set username from string or :symbol @jim.user = :moriarty

=> @jim.user == 'moriarty'
# File lib/moriarty.rb, line 78
def user=( name )
  @user = name.to_s
end