From f7f54ed4302a548cf05fbe549e88f1ef661e1c49 Mon Sep 17 00:00:00 2001 From: Martijn Versluis Date: Thu, 16 Jul 2026 22:34:49 +0200 Subject: [PATCH] Expose sessionStart and sessionEnd on ongoing sessions --- lib/easee/session.rb | 12 ++++++++++++ spec/easee/session_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 spec/easee/session_spec.rb diff --git a/lib/easee/session.rb b/lib/easee/session.rb index c39fc68..ecade00 100644 --- a/lib/easee/session.rb +++ b/lib/easee/session.rb @@ -7,5 +7,17 @@ def initialize(data) def id = @data.fetch(:sessionId) def energy = @data.fetch(:sessionEnergy).to_f + + def session_start = parse_time(:sessionStart) + def session_end = parse_time(:sessionEnd) + + private + + def parse_time(key) + value = @data[key] + return if value.nil? + + Time.zone.parse(value) + end end end diff --git a/spec/easee/session_spec.rb b/spec/easee/session_spec.rb new file mode 100644 index 0000000..bc6674f --- /dev/null +++ b/spec/easee/session_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe Easee::Session do + it "exposes the session fields" do + session = described_class.new( + "sessionId" => 12345, + "sessionEnergy" => 3.42, + "sessionStart" => "2026-01-05T10:00:00Z", + "sessionEnd" => "2026-01-05T11:30:00Z", + ) + + expect(session).to have_attributes( + id: 12345, + energy: 3.42, + session_start: Time.zone.parse("2026-01-05T10:00:00Z"), + session_end: Time.zone.parse("2026-01-05T11:30:00Z"), + ) + end + + it "returns nil for session_start and session_end when the fields are missing or null" do + session = described_class.new( + "sessionId" => 12345, + "sessionEnergy" => 0.0, + "sessionStart" => nil, + "sessionEnd" => nil, + ) + + expect(session).to have_attributes( + session_start: nil, + session_end: nil, + ) + end +end