Class: Ikran::Reader
- Inherits:
-
Object
- Object
- Ikran::Reader
- Defined in:
- lib/ikran/reader.rb
Constant Summary collapse
- COMMANDS =
["exit", "server", "ping", "head", "verbose", "truncate"]
- STATUS_REGEXP =
/#<Net::HTTP[a-zA-Z]+ (.+) readbody=(?:true|false)>/
- HELP =
""" command doesn't exist head, h - HEAD request ping, p - try to ping the remote server verbose, v - turn on/off verbose mod remote, r - set remote server truncate, t - set maximum size of response to display """
Instance Attribute Summary collapse
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #exec(command) ⇒ Object
- #exit ⇒ Object
- #get_url(url) ⇒ Object
- #head ⇒ Object
- #http_ping(url) ⇒ Object
- #parse(command) ⇒ Object
- #ping ⇒ Object
- #run ⇒ Object
- #server(val = nil) ⇒ Object
- #truncate(size = nil) ⇒ Object
Instance Attribute Details
#verbose ⇒ Object
Returns the value of attribute verbose.
8 9 10 |
# File 'lib/ikran/reader.rb', line 8 def verbose @verbose end |
Class Method Details
.aliases(opts) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/ikran/reader.rb', line 112 def self.aliases(opts) opts.each do |original, aliased| [*aliased].each do |a| alias_method a, original COMMANDS << a.to_s end end end |
Instance Method Details
#exec(command) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/ikran/reader.rb', line 94 def exec(command) cmd = parse(command) if cmd instance_eval(cmd) else HELP end end |
#exit ⇒ Object
30 31 32 33 |
# File 'lib/ikran/reader.rb', line 30 def exit @running = false "exiting ..." end |
#get_url(url) ⇒ Object
62 63 64 |
# File 'lib/ikran/reader.rb', line 62 def get_url(url) Net::HTTP.get_response(URI.parse(url)).inspect =~ STATUS_REGEXP end |
#head ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ikran/reader.rb', line 66 def head return "remote must be set before executing head" unless @server res = Net::HTTP.get_response(URI.parse(@server)) if res.inspect =~ STATUS_REGEXP if @verbose (@size.to_i > 0) ? res.body[0, @size] : res.body else $1 end else "invalid response #{res.inspect}" end end |
#http_ping(url) ⇒ Object
58 59 60 |
# File 'lib/ikran/reader.rb', line 58 def http_ping(url) get_url(url).to_i < 400 end |
#parse(command) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ikran/reader.rb', line 23 def parse(command) cmd = command.split(' ') if COMMANDS.include? cmd.first cmd.length > 1 ? ("#{cmd[0]} '#{cmd[1]}'") : cmd.first end end |
#ping ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/ikran/reader.rb', line 48 def ping if not @server "remote must be set before executing ping without parameters" elsif http_ping(@server) "#{@server} is alive" else "#{@server} is unreachable" end end |
#run ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/ikran/reader.rb', line 103 def run @running = true while @running print ">> " line = gets.chomp! puts exec(line) end end |
#server(val = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ikran/reader.rb', line 35 def server(val = nil) if val begin @server = Addressable::URI.heuristic_parse(val).to_s "remote set to #{@server}" rescue Addressable::URI::InvalidURIError => e "invalid url" end else @server ? "current remote is #{@server}" : "remote is not set" end end |
#truncate(size = nil) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/ikran/reader.rb', line 85 def truncate(size = nil) if size @size = (size.to_i == 0 ? nil : size.to_i) @size ? "maximum size set to #{@size}" : "maximum size set to infinite" else @size ? "maximum size is #{@size}" : "maximum size is infinite" end end |