|
3 | 3 | require 'securerandom'
|
4 | 4 | require 'digest'
|
5 | 5 |
|
6 |
| -# class Server_commands |
7 |
| -# end |
| 6 | +class BaseCommand |
| 7 | + def run(args) |
| 8 | + raise NotImplementedError.new ('Not Implemented') |
| 9 | + end |
| 10 | + |
| 11 | + def get_name |
| 12 | + raise NotImplementedError.new ('Not Implemented') |
| 13 | + end |
| 14 | + |
| 15 | + def get_description |
| 16 | + raise NotImplementedError.new ('Not Implemented') |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +class RandomCommand < BaseCommand |
| 23 | + def run(args='') |
| 24 | + rand(0...1000000) |
| 25 | + end |
| 26 | + |
| 27 | + def get_name |
| 28 | + 'random' |
| 29 | + end |
| 30 | + |
| 31 | + def get_description |
| 32 | + 'Return Random Value' |
| 33 | + end |
| 34 | +end |
8 | 35 |
|
9 |
| -def get_currency_exchange(currency) |
10 |
| - url = "http://www.cbr.ru/scripts/XML_daily.asp" |
| 36 | +class TimeNowCommand < BaseCommand |
| 37 | + def run(args='') |
| 38 | + Time.now.ctime |
| 39 | + end |
11 | 40 |
|
12 |
| - doc = Nokogiri::XML(URI.open(url)) |
| 41 | + def get_name |
| 42 | + 'timenow' |
| 43 | + end |
13 | 44 |
|
14 |
| - doc.css('Valute').each do |node| |
15 |
| - ccy = node.css('CharCode').text |
16 |
| - value = node.css('Value').text |
17 |
| - return value.sub(',', '.').to_f.round(2).to_s if ccy == currency |
| 45 | + def get_description |
| 46 | + 'Return Current Time' |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +class ColorCommand < BaseCommand |
| 51 | + def run(args='') |
| 52 | + "#%06X" % (rand * 0xffffff) |
| 53 | + end |
| 54 | + |
| 55 | + def get_name |
| 56 | + 'color' |
| 57 | + end |
| 58 | + |
| 59 | + def get_description |
| 60 | + 'Return random color in hex' |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +class UUidCommand < BaseCommand |
| 65 | + def run(args='') |
| 66 | + SecureRandom.uuid |
| 67 | + end |
| 68 | + |
| 69 | + def get_name |
| 70 | + 'uuid' |
| 71 | + end |
| 72 | + |
| 73 | + def get_description |
| 74 | + 'Return uuid' |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +class MD5Command < BaseCommand |
| 79 | + def run(args='') |
| 80 | + Digest::MD5.hexdigest(args) |
| 81 | + end |
| 82 | + |
| 83 | + def get_name |
| 84 | + 'md5' |
| 85 | + end |
| 86 | + |
| 87 | + def get_description |
| 88 | + 'Return md5' |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +class SHA254Command < BaseCommand |
| 93 | + def run(args='') |
| 94 | + Digest::SHA256.hexdigest(args) |
| 95 | + end |
| 96 | + |
| 97 | + def get_name |
| 98 | + 'sha256' |
| 99 | + end |
| 100 | + |
| 101 | + def get_description |
| 102 | + 'Return sha256' |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +class ValuteCommand < BaseCommand |
| 107 | + def run(args='') |
| 108 | + get_currency_exchange(args) |
| 109 | + end |
| 110 | + |
| 111 | + def get_name |
| 112 | + 'valute' |
| 113 | + end |
| 114 | + |
| 115 | + def get_description |
| 116 | + 'Return Valute' |
| 117 | + end |
| 118 | + |
| 119 | + private def get_currency_exchange(currency) |
| 120 | + url = "http://www.cbr.ru/scripts/XML_daily.asp" |
| 121 | + |
| 122 | + doc = Nokogiri::XML(URI.open(url)) |
| 123 | + |
| 124 | + doc.css('Valute').each do |node| |
| 125 | + ccy = node.css('CharCode').text |
| 126 | + value = node.css('Value').text |
| 127 | + return value.sub(',', '.').to_f.round(2).to_s if ccy == currency |
| 128 | + end |
| 129 | + return "Not found currency #{currency}" |
18 | 130 | end
|
19 |
| - return "Not found currency #{currency}" |
20 | 131 | end
|
21 | 132 |
|
22 | 133 |
|
23 | 134 | def process_command(command, args)
|
24 | 135 | rs = ''
|
25 | 136 | case command
|
26 | 137 | when 'random' then
|
27 |
| - rs = rand(0...1000000) |
| 138 | + rs = RandomCommand.new.run(0...1000000) |
28 | 139 | when 'timenow' then
|
29 |
| - rs = Time.now.ctime |
| 140 | + rs = TimeNowCommand.new.run |
30 | 141 | when 'color' then
|
31 |
| - rs = "#%06X" % (rand * 0xffffff) |
| 142 | + rs = ColorCommand.new.run |
32 | 143 | when 'uuid' then
|
33 |
| - rs = SecureRandom.uuid |
| 144 | + rs = UUidCommand.new.run |
34 | 145 | when 'md5' then
|
35 |
| - rs = Digest::MD5.hexdigest(args) |
| 146 | + rs = MD5Command.new.run(args) |
36 | 147 | when 'sha256' then
|
37 |
| - rs = Digest::SHA256.hexdigest(args) |
| 148 | + rs = SHA254Command.new.run(args) |
38 | 149 | when 'valute' then
|
39 |
| - rs = get_currency_exchange(args) |
| 150 | + rs = ValuteCommand.new.run(args) |
40 | 151 | when 'valute_USD' then
|
41 |
| - rs = get_currency_exchange('USD') |
| 152 | + rs = ValuteCommand.new.run('USD') |
42 | 153 | else
|
43 | 154 | rs = "Unrecognized command '#{command}'"
|
44 | 155 | end
|
45 | 156 | return rs
|
46 | 157 | end
|
| 158 | + |
| 159 | + |
| 160 | +if __FILE__ == $0 then |
| 161 | + command = RandomCommand.new |
| 162 | + puts command.run |
| 163 | +end |
0 commit comments