Skip to content

Commit a3cd158

Browse files
author
Keith Walsh
committed
Initial commit
0 parents  commit a3cd158

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

app.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'sinatra'
2+
require 'haml'
3+
require './crontab_formatter.rb'
4+
5+
get '/' do
6+
haml :index
7+
end
8+
9+
post '/' do
10+
formatter = CrontabFormatter.new(:minute => params[:minute],
11+
:hour => params[:hour],
12+
:day_of_month => params[:day_of_month],
13+
:month => params[:month],
14+
:day_of_week => params[:day_of_week],
15+
:command => params[:command]
16+
)
17+
18+
@params = params
19+
@crontab = formatter.convert_to_cron_command
20+
21+
haml :index
22+
end

crontab_formatter.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class CrontabFormatter
2+
attr_reader :minute, :hour, :day_of_month, :month, :day_of_week, :command
3+
4+
def initialize(args)
5+
@minute = args[:minute]
6+
@hour = args[:hour]
7+
@day_of_month = args[:day_of_month]
8+
@month = args[:month]
9+
@day_of_week = args[:day_of_week]
10+
@command = args[:command]
11+
end
12+
13+
def convert_to_cron_command
14+
"#{join_vals(@minute)} #{join_vals(@hour)} #{join_vals(@day_of_month)} #{join_vals(@month)} #{join_vals(@day_of_week)} #{@command}"
15+
end
16+
17+
def join_vals(vals)
18+
vals.nil? ? "*" : vals.join(",")
19+
end
20+
end

spec/crontab_formatter_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require './crontab_formatter.rb'
2+
require 'rspec'
3+
require 'spec_helper.rb'
4+
5+
describe CrontabFormatter do
6+
context "formatter" do
7+
it "outputs valid crontab" do
8+
formatter = CrontabFormatter.new(:minute => [30], :hour => [1,5], :day_of_month => nil, :month => [2], :day_of_week => [0], :command => "/task.rb")
9+
expect(formatter.convert_to_cron_command).to eq("30 1,5 * 2 0 /task.rb")
10+
end
11+
end
12+
13+
context "vals" do
14+
it "join with multiple" do
15+
formatter = CrontabFormatter.new(:minute => [30,60])
16+
17+
expect(formatter.join_vals(formatter.minute)).to eq("30,60")
18+
end
19+
20+
it "join when single" do
21+
formatter = CrontabFormatter.new(:minute => [30])
22+
23+
expect(formatter.join_vals(formatter.minute)).to eq("30")
24+
end
25+
26+
it "return * when none" do
27+
formatter = CrontabFormatter.new(:minute => nil)
28+
29+
expect(formatter.join_vals(formatter.minute)).to eq("*")
30+
end
31+
end
32+
end

spec/spec_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RSpec.configure do |config|
2+
# Use color in STDOUT
3+
config.color_enabled = true
4+
5+
# Use color not only in STDOUT but also in pagers and files
6+
config.tty = true
7+
8+
# Use the specified formatter
9+
config.formatter = :documentation # :progress, :html, :textmate
10+
end

views/index.haml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=@crontab
2+
%form{:action => "/", :method => "post"}
3+
%select{:multiple => true, :name => "minute[]", :size => 10, :style => "width: 50px"}
4+
-(1..60).each do |m|
5+
%option{:value => m}
6+
=m
7+
8+
%select{:multiple => true, :name => "hour[]", :size => 10, :style => "width: 50px"}
9+
-(1..24).each do |m|
10+
%option{:value => m}
11+
=m
12+
13+
14+
%select{:multiple => true, :name => "day_of_month[]", :size => 10, :style => "width: 50px"}
15+
-(1..31).each do |m|
16+
%option{:value => m}
17+
=m
18+
19+
20+
%select{:multiple => true, :name => "month[]", :size => 10, :style => "width: 50px"}
21+
-(1..12).each do |m|
22+
%option{:value => m}
23+
=m
24+
25+
26+
%select{:multiple => true, :name => "day_of_week[]", :size => 10, :style => "width: 50px"}
27+
-(1..7).each do |m|
28+
%option{:value => m}
29+
=m
30+
31+
%input{:type => "text", :name => "command"}
32+
33+
%input{:type => "submit", :value => "Submit"}

0 commit comments

Comments
 (0)