Skip to content

Commit

Permalink
Merge pull request #322 from interagent/add-zulu-time-helpers
Browse files Browse the repository at this point in the history
Add Pliny::Helpers::ZuluTime to Serializers::Base
  • Loading branch information
gudmundur authored Dec 14, 2017
2 parents 7b3e082 + 4a30e49 commit 6783c4c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/pliny.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative "pliny/helpers/encode"
require_relative "pliny/helpers/params"
require_relative "pliny/helpers/serialize"
require_relative "pliny/helpers/zulu_time"
require_relative "pliny/log"
require_relative "pliny/metrics/backends/logger"
require_relative "pliny/metrics"
Expand Down
7 changes: 7 additions & 0 deletions lib/pliny/helpers/zulu_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Pliny::Helpers
module ZuluTime
def zulu_time(time)
time ? time.getutc.strftime("%Y-%m-%dT%H:%M:%SZ") : nil
end
end
end
4 changes: 2 additions & 2 deletions lib/pliny/templates/serializer.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<%= ident %>class <%= modules.last %> < Serializers::Base
<%= ident %> structure(:default) do |arg|
<%= ident %> {
<%= ident %> created_at: arg.created_at.try(:iso8601),
<%= ident %> created_at: zulu_time(arg.created_at),
<%= ident %> id: arg.id,
<%= ident %> updated_at: arg.updated_at.try(:iso8601),
<%= ident %> updated_at: zulu_time(arg.updated_at)
<%= ident %> }
<%= ident %> end
<%= ident %>end
Expand Down
2 changes: 2 additions & 0 deletions lib/template/lib/serializers/base.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Serializers
class Base
extend Pliny::Helpers::ZuluTime

@@structures = {}

def self.structure(type, &blk)
Expand Down
24 changes: 24 additions & 0 deletions spec/helpers/zulu_time_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "spec_helper"
require "active_support/core_ext/numeric/time"

describe Pliny::Helpers::ZuluTime do
context "zulu_time" do
class ZuluTimeTest
extend Pliny::Helpers::ZuluTime
end

it "it formats Time instances" do
formatted = ZuluTimeTest.zulu_time(Time.parse("2017-11-28T21:49:52.123+00:00"))
assert_equal "2017-11-28T21:49:52Z", formatted
end

it "it formats DateTime instances" do
formatted = ZuluTimeTest.zulu_time(DateTime.parse("2017-11-28T21:49:52.123+00:00"))
assert_equal "2017-11-28T21:49:52Z", formatted
end

it "when called with nil" do
assert_nil ZuluTimeTest.zulu_time(nil)
end
end
end

0 comments on commit 6783c4c

Please sign in to comment.