knjrbfw 0.0.42 → 0.0.43
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/knjrbfw.gemspec +3 -2
- data/lib/knj/arrayext.rb +16 -0
- data/lib/knj/datet.rb +133 -5
- data/lib/knj/http2.rb +4 -0
- data/lib/knj/os.rb +1 -0
- data/lib/knj/unix_proc.rb +2 -2
- data/lib/knj/web.rb +12 -14
- data/spec/arrayext_spec.rb +14 -0
- data/spec/datet_spec.rb +25 -0
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.43
|
data/knjrbfw.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{knjrbfw}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.43"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kasper Johansen"]
|
12
|
-
s.date = %q{2012-06-
|
12
|
+
s.date = %q{2012-06-10}
|
13
13
|
s.description = %q{Including stuff for HTTP, SSH and much more.}
|
14
14
|
s.email = %q{k@spernj.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -255,6 +255,7 @@ Gem::Specification.new do |s|
|
|
255
255
|
"lib/knj/youtube.rb",
|
256
256
|
"lib/knjrbfw.rb",
|
257
257
|
"spec/amixer_spec.rb",
|
258
|
+
"spec/arrayext_spec.rb",
|
258
259
|
"spec/cmd_parser_spec.rb",
|
259
260
|
"spec/datet_spec.rb",
|
260
261
|
"spec/db_spec.rb",
|
data/lib/knj/arrayext.rb
CHANGED
@@ -230,4 +230,20 @@ module Knj::ArrayExt
|
|
230
230
|
|
231
231
|
return hash
|
232
232
|
end
|
233
|
+
|
234
|
+
#Forces an array to have a certain amount of columns.
|
235
|
+
#===Examples
|
236
|
+
# arr = [1, 2, 3, 4, 5]
|
237
|
+
# Knj::ArrayExt.force_no_cols(:arr => arr, :no => 4) #=> [1, 2, 3, 4]
|
238
|
+
def self.force_no_cols(args)
|
239
|
+
while args[:arr].length > args[:no]
|
240
|
+
args[:arr].slice!(-1)
|
241
|
+
end
|
242
|
+
|
243
|
+
while args[:arr].length < args[:no]
|
244
|
+
args[:arr] << args[:empty]
|
245
|
+
end
|
246
|
+
|
247
|
+
return nil
|
248
|
+
end
|
233
249
|
end
|
data/lib/knj/datet.rb
CHANGED
@@ -9,8 +9,70 @@ require "time"
|
|
9
9
|
class Knj::Datet
|
10
10
|
attr_accessor :time
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
#Initializes the object. Default is the current time. A time-object can be given.
|
13
|
+
def initialize(time = Time.now, *args)
|
14
|
+
if time.is_a?(Time)
|
15
|
+
@time = time
|
16
|
+
else
|
17
|
+
begin
|
18
|
+
@time = Time.new(*([time] | args))
|
19
|
+
rescue ArgumentError => e
|
20
|
+
days_left = 0
|
21
|
+
months_left = 0
|
22
|
+
hours_left = 0
|
23
|
+
mins_left = 0
|
24
|
+
secs_left = 0
|
25
|
+
usecs_left = 0
|
26
|
+
|
27
|
+
#Check larger month the allowed.
|
28
|
+
if args[0] and args[0] > 12
|
29
|
+
months_left = args[0] - 12
|
30
|
+
args[0] = 12
|
31
|
+
end
|
32
|
+
|
33
|
+
#Check larger date than allowed.
|
34
|
+
datet = Knj::Datet.new(time, args[0], 1)
|
35
|
+
dim = datet.days_in_month
|
36
|
+
|
37
|
+
if args[1] and args[1] > dim
|
38
|
+
days_left = args[1] - dim
|
39
|
+
args[1] = dim if days_left > 0
|
40
|
+
end
|
41
|
+
|
42
|
+
#Check larger hour than allowed.
|
43
|
+
if args[2] and args[2] >= 24
|
44
|
+
hours_left = args[2] + 1
|
45
|
+
args[2] = 0
|
46
|
+
end
|
47
|
+
|
48
|
+
#Check larger minute than allowed.
|
49
|
+
if args[3] and args[3] >= 60
|
50
|
+
mins_left = args[3] + 1
|
51
|
+
args[3] = 0
|
52
|
+
end
|
53
|
+
|
54
|
+
#Check larger secs than allowed.
|
55
|
+
if args[4] and args[4] >= 60
|
56
|
+
secs_left = args[4] + 1
|
57
|
+
args[4] = 0
|
58
|
+
end
|
59
|
+
|
60
|
+
#Check larger usecs than allowed.
|
61
|
+
if args[5] and args[5] >= 60
|
62
|
+
usecs_left = args[5] + 1
|
63
|
+
args[5] = 0
|
64
|
+
end
|
65
|
+
|
66
|
+
#Generate new stamp.
|
67
|
+
@time = Time.new(*([time] | args))
|
68
|
+
self.mins + mins_left if mins_left > 0
|
69
|
+
self.hours + hours_left if hours_left > 0
|
70
|
+
self.days + days_left if days_left > 0
|
71
|
+
self.months + months_left if months_left > 0
|
72
|
+
self.secs + secs_left if secs_left > 0
|
73
|
+
self.usecs + usecs_left if usecs_left > 0
|
74
|
+
end
|
75
|
+
end
|
14
76
|
end
|
15
77
|
|
16
78
|
#Goes forward day-by-day and stops at a date matching the criteria given.
|
@@ -45,6 +107,48 @@ class Knj::Datet
|
|
45
107
|
end
|
46
108
|
end
|
47
109
|
|
110
|
+
#Add a given amount of seconds to the object.
|
111
|
+
def add_usecs(usecs = 1)
|
112
|
+
usecs = usecs.to_i
|
113
|
+
cur_usecs = @time.usec
|
114
|
+
next_usec = cur_usecs + usecs
|
115
|
+
|
116
|
+
if next_usec >= 60
|
117
|
+
@time = self.add_secs(1).stamp(:datet => false, :usec => 0)
|
118
|
+
usecs_left = (usecs - 1) - (60 - cur_usecs)
|
119
|
+
return self.add_usecs(usecs_left) if usecs_left > 0
|
120
|
+
elsif next_usec < 0
|
121
|
+
@time = self.add_secs(-1).stamp(:datet => false, :usec => 59)
|
122
|
+
usecs_left = usecs + cur_usecs + 1
|
123
|
+
self.add_usecs(usecs_left) if usecs_left > 0
|
124
|
+
else
|
125
|
+
@time = self.stamp(:datet => false, :usec => next_usec)
|
126
|
+
end
|
127
|
+
|
128
|
+
return self
|
129
|
+
end
|
130
|
+
|
131
|
+
#Add a given amount of seconds to the object.
|
132
|
+
def add_secs(secs = 1)
|
133
|
+
secs = secs.to_i
|
134
|
+
cur_secs = @time.sec
|
135
|
+
next_sec = cur_secs + secs
|
136
|
+
|
137
|
+
if next_sec >= 60
|
138
|
+
@time = self.add_mins(1).stamp(:datet => false, :sec => 0)
|
139
|
+
secs_left = (secs - 1) - (60 - cur_secs)
|
140
|
+
return self.add_secs(secs_left) if secs_left > 0
|
141
|
+
elsif next_sec < 0
|
142
|
+
@time = self.add_mins(-1).stamp(:datet => false, :sec => 59)
|
143
|
+
secs_left = secs + cur_secs + 1
|
144
|
+
self.add_secs(secs_left) if secs_left > 0
|
145
|
+
else
|
146
|
+
@time = self.stamp(:datet => false, :sec => next_sec)
|
147
|
+
end
|
148
|
+
|
149
|
+
return self
|
150
|
+
end
|
151
|
+
|
48
152
|
#Add a given amount of minutes to the object.
|
49
153
|
#===Examples
|
50
154
|
# datet = Knj::Datet.new #=> 2012-05-03 17:39:45 +0200
|
@@ -234,6 +338,16 @@ class Knj::Datet
|
|
234
338
|
return @time.min
|
235
339
|
end
|
236
340
|
|
341
|
+
#Returns the seconds as an integer.
|
342
|
+
def sec
|
343
|
+
return @time.sec
|
344
|
+
end
|
345
|
+
|
346
|
+
#Returns the microsecond as an integer.
|
347
|
+
def usec
|
348
|
+
return @time.usec
|
349
|
+
end
|
350
|
+
|
237
351
|
#Changes the year to the given year.
|
238
352
|
# datet = Knj::Datet.now #=> 2014-05-03 17:46:11 +0200
|
239
353
|
# datet.year = 2005
|
@@ -373,6 +487,8 @@ class Knj::Datet
|
|
373
487
|
return self.add_days(val) if @mode == :days
|
374
488
|
return self.add_months(val) if @mode == :months
|
375
489
|
return self.add_mins(val) if @mode == :mins
|
490
|
+
return self.add_secs(val) if @mode == :secs
|
491
|
+
return self.add_usecs(val) if @mode == :usecs
|
376
492
|
raise "No such mode: #{@mode}"
|
377
493
|
end
|
378
494
|
|
@@ -414,6 +530,18 @@ class Knj::Datet
|
|
414
530
|
return self
|
415
531
|
end
|
416
532
|
|
533
|
+
#Sets the mode to seconds and gets ready to plus or minus.
|
534
|
+
def secs
|
535
|
+
@mode = :secs
|
536
|
+
return self
|
537
|
+
end
|
538
|
+
|
539
|
+
#Sets the mode to mili-seconds and gets ready to plus or minus.
|
540
|
+
def usecs
|
541
|
+
@mode = :usecs
|
542
|
+
return self
|
543
|
+
end
|
544
|
+
|
417
545
|
#Sets the mode to days and gets ready to plus or minus.
|
418
546
|
#===Examples
|
419
547
|
# datet.time #=> 2005-05-08 22:51:11 +0200
|
@@ -448,13 +576,13 @@ class Knj::Datet
|
|
448
576
|
#===Examples
|
449
577
|
# time = datet.stamp(:datet => false, :min => 15, :day => 5) #=> 2012-07-05 05:15:20 +0200
|
450
578
|
def stamp(args)
|
451
|
-
vars = {:year => @time.year, :month => @time.month, :day => @time.day, :hour => @time.hour, :min => @time.min, :sec => @time.sec}
|
579
|
+
vars = {:year => @time.year, :month => @time.month, :day => @time.day, :hour => @time.hour, :min => @time.min, :sec => @time.sec, :usec => @time.usec}
|
452
580
|
|
453
581
|
args.each do |key, value|
|
454
582
|
vars[key.to_sym] = value.to_i if key != :datet
|
455
583
|
end
|
456
584
|
|
457
|
-
time = Time.local(vars[:year], vars[:month], vars[:day], vars[:hour], vars[:min], vars[:sec])
|
585
|
+
time = Time.local(vars[:year], vars[:month], vars[:day], vars[:hour], vars[:min], vars[:sec], vars[:usec])
|
458
586
|
|
459
587
|
if !args.key?(:datet) or args[:datet]
|
460
588
|
return Knj::Datet.new(time)
|
@@ -632,7 +760,7 @@ class Knj::Datet
|
|
632
760
|
#Datet.code format
|
633
761
|
return Knj::Datet.new(Time.local(match[1], match[2], match[3], match[4], match[5], match[6], match[7]))
|
634
762
|
elsif match = timestr.to_s.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})(|\s+(\d{2}):(\d{2}):(\d{2})(|\.\d+)\s*)(|\s+(UTC))(|\s+(\+|\-)(\d{2})(\d{2}))$/)
|
635
|
-
#Database date format (with possibility of .0 in the end -
|
763
|
+
#Database date format (with possibility of .0 in the end - microseconds? -knj.
|
636
764
|
|
637
765
|
if match[11] and match[13] and match[14]
|
638
766
|
if match[12] == "+" or match[12] == "-"
|
data/lib/knj/http2.rb
CHANGED
@@ -212,6 +212,10 @@ class Knj::Http2
|
|
212
212
|
#headers["Accept-Encoding"] = "none"
|
213
213
|
end
|
214
214
|
|
215
|
+
if @args[:basic_auth]
|
216
|
+
headers["Authorization"] = "Basic #{Base64.encode64("#{@args[:basic_auth][:user]}:#{@args[:basic_auth][:passwd]}")}"
|
217
|
+
end
|
218
|
+
|
215
219
|
return headers
|
216
220
|
end
|
217
221
|
|
data/lib/knj/os.rb
CHANGED
data/lib/knj/unix_proc.rb
CHANGED
@@ -26,8 +26,8 @@ class Knj::Unix_proc
|
|
26
26
|
grepstr = ""
|
27
27
|
|
28
28
|
if args["grep"]
|
29
|
-
grepstr = "grep #{
|
30
|
-
cmdstr << " | #{
|
29
|
+
grepstr = "grep #{args["grep"]}" #used for ignoring the grep-process later.
|
30
|
+
cmdstr << " | grep #{Knj::Strings.unixsafe(args["grep"])}"
|
31
31
|
end
|
32
32
|
|
33
33
|
MUTEX.synchronize do
|
data/lib/knj/web.rb
CHANGED
@@ -369,11 +369,11 @@ class Knj::Web
|
|
369
369
|
elsif args[:type] == :spacer
|
370
370
|
html << "<tr#{classes_tr_html}><td colspan=\"2\"> </td></tr>"
|
371
371
|
else
|
372
|
-
html << "<tr#{classes_tr_html}>"
|
373
|
-
html << "<td class=\"tdt\">"
|
372
|
+
html << "<tr#{classes_tr_html} id=\"#{Knj::Web.html("#{args[:id]}_tr")}\">"
|
373
|
+
html << "<td class=\"tdt\" id=\"#{Knj::Web.html("#{args[:id]}_label")}\"><div>"
|
374
374
|
html << title_html
|
375
|
-
html << "</td>"
|
376
|
-
html << "<td#{self.style_html(css)} class=\"tdc\">"
|
375
|
+
html << "</div></td>"
|
376
|
+
html << "<td#{self.style_html(css)} class=\"tdc\" id=\"#{Knj::Web.html("#{args[:id]}_content")}\"><div>"
|
377
377
|
|
378
378
|
if args[:type] == :textarea
|
379
379
|
if args.key?(:height)
|
@@ -385,7 +385,6 @@ class Knj::Web
|
|
385
385
|
end
|
386
386
|
|
387
387
|
html << "<textarea#{self.style_html(css)} class=\"input_textarea\" name=\"#{args[:name].html}\" id=\"#{args[:id].html}\">#{value}</textarea>"
|
388
|
-
html << "</td>"
|
389
388
|
elsif args[:type] == :fckeditor
|
390
389
|
args[:height] = 400 if !args[:height]
|
391
390
|
|
@@ -394,8 +393,6 @@ class Knj::Web
|
|
394
393
|
fck.Height = args[:height].to_i
|
395
394
|
fck.Value = value
|
396
395
|
html << fck.CreateHtml
|
397
|
-
|
398
|
-
html << "</td>"
|
399
396
|
elsif args[:type] == :select
|
400
397
|
attr["multiple"] = "multiple" if args[:multiple]
|
401
398
|
attr["size"] = args["size"] if args[:size]
|
@@ -403,7 +400,6 @@ class Knj::Web
|
|
403
400
|
html << "<select#{self.attr_html(attr)}>"
|
404
401
|
html << Knj::Web.opts(args[:opts], value, args[:opts_args])
|
405
402
|
html << "</select>"
|
406
|
-
html << "</td>"
|
407
403
|
elsif args[:type] == :imageupload
|
408
404
|
html << "<table class=\"designtable\"><tr#{classes_tr_html}><td style=\"width: 100%;\">"
|
409
405
|
html << "<input type=\"file\" name=\"#{args[:name].html}\" class=\"input_file\" />"
|
@@ -423,11 +419,10 @@ class Knj::Web
|
|
423
419
|
end
|
424
420
|
|
425
421
|
html << "</td></tr></table>"
|
426
|
-
html << "</td>"
|
427
422
|
elsif args[:type] == :file
|
428
|
-
html << "<input type=\"#{args[:type].to_s}\" class=\"input_#{args[:type].to_s}\" name=\"#{args[:name].html}\"
|
423
|
+
html << "<input type=\"#{args[:type].to_s}\" class=\"input_#{args[:type].to_s}\" name=\"#{args[:name].html}\" />"
|
429
424
|
elsif args[:type] == :textshow or args[:type] == :info
|
430
|
-
html <<
|
425
|
+
html << value.to_s
|
431
426
|
elsif args[:type] == :plain
|
432
427
|
html << "#{Knj::Php.nl2br(Knj::Web.html(value))}"
|
433
428
|
elsif args[:type] == :editarea
|
@@ -450,13 +445,16 @@ class Knj::Web
|
|
450
445
|
html << "editAreaLoader.init(#{Knj::Php.json_encode(jshash)});"
|
451
446
|
html << "}"
|
452
447
|
html << "</script>"
|
448
|
+
elsif args[:type] == :numeric
|
449
|
+
attr[:type] = :text
|
450
|
+
attr[:value] = value
|
451
|
+
html << "<input#{self.attr_html(attr)} />"
|
453
452
|
else
|
454
453
|
attr[:value] = value
|
455
|
-
html << "<input#{self.attr_html(attr)}
|
456
|
-
html << "</td>"
|
454
|
+
html << "<input#{self.attr_html(attr)} />"
|
457
455
|
end
|
458
456
|
|
459
|
-
html << "</tr>"
|
457
|
+
html << "</div></td></tr>"
|
460
458
|
end
|
461
459
|
|
462
460
|
html << "<tr#{classes_tr_html}><td colspan=\"2\" class=\"tdd\">#{args[:descr]}</td></tr>" if args[:descr]
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "ArrayExt" do
|
4
|
+
it "should be able to make ago-strings" do
|
5
|
+
arr = [1, 2]
|
6
|
+
Knj::ArrayExt.force_no_cols(:arr => arr, :no => 1)
|
7
|
+
raise "Expected length of 1 but got: #{arr.length}" if arr.length != 1
|
8
|
+
raise "Expected element to be 1 but it wasnt: #{arr[0]}" if arr[0] != 1
|
9
|
+
|
10
|
+
Knj::ArrayExt.force_no_cols(:arr => arr, :no => 3, :empty => "test")
|
11
|
+
raise "Expected length of 3 but got: #{arr.lengtj}" if arr.length != 3
|
12
|
+
raise "Expected element 2 to be 'test' but it wasnt: #{arr[2]}" if arr[2] != "test"
|
13
|
+
end
|
14
|
+
end
|
data/spec/datet_spec.rb
CHANGED
@@ -70,4 +70,29 @@ describe "Datet" do
|
|
70
70
|
raise "Date1 was wrongly not the same as date3." if date1 != date3
|
71
71
|
raise "Date1 was the same as date2?" if date1 == date2
|
72
72
|
end
|
73
|
+
|
74
|
+
it "various methods should just work" do
|
75
|
+
date = Knj::Datet.new(1985, 6, 17)
|
76
|
+
raise "Invalid days in month: #{date.days_in_month}" if date.days_in_month != 30
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should be able to handle invalid timestamps" do
|
80
|
+
datet = Knj::Datet.new(2012, 3, 40)
|
81
|
+
raise "Expected dbstr to be '2012-04-09' but it wasnt: '#{datet.dbstr(:time => false)}'." if datet.dbstr(:time => false) != "2012-04-09"
|
82
|
+
|
83
|
+
datet = Knj::Datet.new(2012, 14)
|
84
|
+
raise "Expected dbstr to be '2013-02-01' but it wasnt: '#{datet.dbstr(:time => false)}'." if datet.dbstr(:time => false) != "2013-02-01"
|
85
|
+
|
86
|
+
datet = Knj::Datet.new(1985, 6, 17, 28)
|
87
|
+
raise "Expected dbstr to be '1985-06-18 04:00:00' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 04:00:00"
|
88
|
+
|
89
|
+
datet = Knj::Datet.new(1985, 6, 17, 28, 68)
|
90
|
+
raise "Expected dbstr to be '1985-06-18 05:08:00' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:08:00"
|
91
|
+
|
92
|
+
datet = Knj::Datet.new(1985, 6, 17, 28, 68, 68)
|
93
|
+
raise "Expected dbstr to be '1985-06-18 05:09:08' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:08"
|
94
|
+
|
95
|
+
datet = Knj::Datet.new(1985, 6, 17, 28, 68, 68, 68)
|
96
|
+
raise "Expected dbstr to be '1985-06-18 05:09:09' but it wasnt: '#{datet.dbstr}'." if datet.dbstr != "1985-06-18 05:09:09"
|
97
|
+
end
|
73
98
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knjrbfw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.43
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-06-
|
13
|
+
date: 2012-06-10 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -349,6 +349,7 @@ files:
|
|
349
349
|
- lib/knj/youtube.rb
|
350
350
|
- lib/knjrbfw.rb
|
351
351
|
- spec/amixer_spec.rb
|
352
|
+
- spec/arrayext_spec.rb
|
352
353
|
- spec/cmd_parser_spec.rb
|
353
354
|
- spec/datet_spec.rb
|
354
355
|
- spec/db_spec.rb
|
@@ -378,7 +379,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
378
379
|
requirements:
|
379
380
|
- - ">="
|
380
381
|
- !ruby/object:Gem::Version
|
381
|
-
hash: -
|
382
|
+
hash: -2508696839394262150
|
382
383
|
segments:
|
383
384
|
- 0
|
384
385
|
version: "0"
|