Class: Moriarty

Inherits:
Object
  • Object
show all
Defined in:
lib/moriarty.rb,
lib/moriarty/cli.rb

Overview

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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '', site = 'github.com', type = :https) ⇒ Moriarty

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



31
32
33
34
# File 'lib/moriarty.rb', line 31

def initialize( name = '', site = 'github.com', type = :https )
  @user = name.to_s
  @url  = make_url site, type
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



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

def html
  @html
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.find!(username, web = 'github.com', type = :search) ⇒ Object

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


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object

#hunt! is alias for #find! with :hunt option Check is user ‘stupiduser’ registered on instagram

-> Moriarty.hunt! 'stupiduser', 'instagram.com'


51
52
53
# File 'lib/moriarty/cli.rb', line 51

def self.hunt!( name, site = 'github.com', type = :hunt)
  find! name, site, type
end

.p1(title, color1 = :'light_green', color2 = :cyan, type = :bold) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object



88
89
90
91
92
# 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'


75
76
77
78
79
80
# 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'


59
60
61
62
63
64
65
66
67
68
69
# 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

Instance Method Details

#go(opt = {}) ⇒ Object Also known as: search

execute request (args are optional)



42
43
44
45
46
47
48
49
50
51
# 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

#make_url(link, prot = :https) ⇒ Object

create URL from site name, add ‘https’ if needed



59
60
61
62
63
64
# 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?Boolean

Check does request succeeded or not @jim.success?

=> true/false

Returns:

  • (Boolean)


86
87
88
# File 'lib/moriarty.rb', line 86

def success?
  @success == true
end