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
Public Class Methods
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! 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
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
# 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
# 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
Remove extensions from username Moriarty.print_name
('@moriarty')
=> 'moriarty'
# File lib/moriarty/cli.rb, line 75 def self.print_name( name ) name.gsub!('@','') if name.to_s.start_with?('@') name.gsub!('#','') if name.to_s.start_with?('#') name.gsub!('/u/','') if name.to_s.start_with?('/u/') return name end
Remove extensions from domain name Moriarty.print_url
('www.github.com')
=> 'github'
# File lib/moriarty/cli.rb, line 59 def self.print_url( site ) site, name = site.to_s, '' if site.start_with?('http') site.gsub!("https://", '') or site.gsub!("http://", '') end site.gsub!("www.", '') if site.start_with?('www.') ext = site.to_s.split('.').last name = site.gsub(".#{ext}", '') name = name.split('/').first if ext.size < 5 return name.capitalize end
Public Instance Methods
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
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
Check does request succeeded or not @jim.success?
=> true/false
# File lib/moriarty.rb, line 86 def success? @success == true end
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
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