diff --git a/spec/data/work-dates.json b/spec/data/work-dates.json new file mode 100644 index 0000000..0eecd84 --- /dev/null +++ b/spec/data/work-dates.json @@ -0,0 +1,14 @@ +[ + { + "date": "2019-01-01", + "hours": 10 + }, + { + "date": "2019-01-02", + "hours": 6 + }, + { + "date": "2019-01-03", + "hours": 4 + } +] \ No newline at end of file diff --git a/spec/dayoff_spec.cr b/spec/dayoff_spec.cr index 5cfe5ef..a35f282 100644 --- a/spec/dayoff_spec.cr +++ b/spec/dayoff_spec.cr @@ -1,9 +1,8 @@ require "./spec_helper" describe Dayoff do - # TODO: Write tests - - it "works" do - false.should eq(true) + it "can calc work hours" do + app = Dayoff::App.new("./spec/data/work-dates.json") + app.get_work_hours.should eq 20 end end diff --git a/src/dayoff.cr b/src/dayoff.cr index 589b05f..8295190 100644 --- a/src/dayoff.cr +++ b/src/dayoff.cr @@ -1,6 +1,29 @@ -# TODO: Write documentation for `Dayoff` +require "json" + module Dayoff VERSION = "0.1.0" - # TODO: Put your code here + class WorkDate + JSON.mapping( + date: String, + hours: Int32, + ) + end + + class App + def initialize(wh_path) + content = File.open(wh_path) do |file| + file.gets_to_end + end + @wdates = Array(WorkDate).from_json(content) + end + + def get_work_hours + sum = 0 + @wdates.each do |wd| + sum += wd.hours + end + sum + end + end end