Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pliny::Helpers::ZuluTime to Serializers::Base #322

Merged
merged 3 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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