-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_commands.rb
167 lines (131 loc) · 2.81 KB
/
server_commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'open-uri'
require 'nokogiri'
require 'securerandom'
require 'digest'
class BaseCommand
def self.run(args)
raise NotImplementedError.new ('Not Implemented')
end
def self.get_name
raise NotImplementedError.new ('Not Implemented')
end
def self.get_description
raise NotImplementedError.new ('Not Implemented')
end
end
class RandomCommand < BaseCommand
def self.run(args='')
rand(0...1000000)
end
def self.get_name
'random'
end
def self.get_description
'Return Random Value'
end
end
class TimeNowCommand < BaseCommand
def self.run(args='')
Time.now.ctime
end
def self.get_name
'timenow'
end
def self.get_description
'Return Current Time'
end
end
class ColorCommand < BaseCommand
def self.run(args='')
"#%06X" % (rand * 0xffffff)
end
def self.get_name
'color'
end
def self.get_description
'Return random color in hex'
end
end
class UUidCommand < BaseCommand
def self.run(args='')
SecureRandom.uuid
end
def self.get_name
'uuid'
end
def self.get_description
'Return uuid'
end
end
class MD5Command < BaseCommand
def self.run(args='')
Digest::MD5.hexdigest(args)
end
def self.get_name
'md5'
end
def self.get_description
'Return md5'
end
end
class SHA256Command < BaseCommand
def self.run(args='')
Digest::SHA256.hexdigest(args)
end
def self.get_name
'sha256'
end
def self.get_description
'Return sha256'
end
end
class ValuteCommand < BaseCommand
def self.run(args='')
get_currency_exchange(args)
end
def self.get_name
'valute'
end
def self.get_description
'Return Valute'
end
private_class_method def self.get_currency_exchange(currency)
url = "http://www.cbr.ru/scripts/XML_daily.asp"
doc = Nokogiri::XML(URI.open(url))
doc.css('Valute').each do |node|
ccy = node.css('CharCode').text
value = node.css('Value').text
return value.sub(',', '.').to_f.round(2).to_s if ccy == currency
end
return "Not found currency #{currency}"
end
end
class ValuteUsdCommand < ValuteCommand
def self.run(args='')
super('USD')
end
def self.get_name
'valute_USD'
end
def self.get_description
'Return Valute USD'
end
end
ALL_COMMANDS = [
RandomCommand, TimeNowCommand, ColorCommand, UUidCommand, MD5Command,
SHA256Command, ValuteCommand, ValuteUsdCommand
]
def process_command(command_name, args='')
ALL_COMMANDS.each do |command|
return command.run(args) if command.get_name == command_name
end
return "Unrecognized command '#{command_name}'"
end
if __FILE__ == $0 then
puts RandomCommand.run
puts process_command('valute_USD')
puts process_command('valute', args='USD')
puts process_command('valute_XYU')
puts process_command('random')
puts process_command('color')
end