Skip to content

Commit

Permalink
feat: display times in UTC in API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 29, 2019
1 parent 295b0b9 commit a423111
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/api/decorators/base_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'roar/json/hal'
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/api/decorators/decorator_context'
require 'pact_broker/api/decorators/format_date_time'

module PactBroker

Expand All @@ -13,6 +14,7 @@ class BaseDecorator < Roar::Decorator
include Roar::JSON::HAL
include Roar::JSON::HAL::Links
include PactBroker::Api::PactBrokerUrls
include FormatDateTime
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/api/decorators/dashboard_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'ostruct'
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/api/decorators/format_date_time'

module PactBroker
module Api
module Decorators
class DashboardDecorator
include PactBroker::Api::PactBrokerUrls
include FormatDateTime

def initialize(index_items)
@index_items = index_items
Expand Down Expand Up @@ -90,7 +92,7 @@ def provider_hash(index_item, provider, base_url)

def pact_hash(index_item, base_url)
{
createdAt: index_item.latest_pact.created_at.to_datetime.xmlschema,
createdAt: format_date_time(index_item.latest_pact.created_at),
_links: {
self: {
href: pact_url(base_url, index_item.latest_pact)
Expand All @@ -103,7 +105,7 @@ def verification_hash(index_item, base_url)
if index_item.latest_verification
{
success: index_item.latest_verification.success,
verifiedAt: index_item.latest_verification.created_at.to_datetime.xmlschema,
verifiedAt: format_date_time(index_item.latest_verification.created_at),
_links: {
self: {
href: verification_url(index_item.latest_verification, base_url)
Expand Down Expand Up @@ -148,7 +150,7 @@ def verification_tags(index_item, base_url)
def latest_webhook_execution(index_item, base_url)
if index_item.last_webhook_execution_date
{
triggeredAt: index_item.last_webhook_execution_date.to_datetime.xmlschema
triggeredAt: format_date_time(index_item.last_webhook_execution_date)
}
end
end
Expand Down
15 changes: 15 additions & 0 deletions lib/pact_broker/api/decorators/format_date_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module PactBroker
module Api
module Decorators
module FormatDateTime
def self.call(date_time)
date_time.to_time.utc.to_datetime.xmlschema if date_time
end

def format_date_time(date_time)
FormatDateTime.call(date_time)
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'ostruct'
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/api/decorators/reason_decorator'
require 'pact_broker/api/decorators/format_date_time'

module PactBroker
module Api
module Decorators
class MatrixDecorator
include PactBroker::Api::PactBrokerUrls
include FormatDateTime

def initialize(query_results_with_deployment_status_summary)
@query_results_with_deployment_status_summary = query_results_with_deployment_status_summary
Expand Down Expand Up @@ -102,7 +104,7 @@ def provider_hash(line, provider, base_url)

def pact_hash(line, base_url)
{
createdAt: line.pact_created_at.to_datetime.xmlschema,
createdAt: format_date_time(line.pact_created_at),
_links: {
self: {
href: pact_url(base_url, line)
Expand All @@ -120,7 +122,7 @@ def verification_hash(line, base_url)
}
{
success: line.success,
verifiedAt: line.verification_executed_at.to_datetime.xmlschema,
verifiedAt: format_date_time(line.verification_executed_at),
_links: {
self: {
href: verification_url_from_params(url_params, base_url)
Expand Down
9 changes: 3 additions & 6 deletions lib/pact_broker/api/decorators/timestamps.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
require 'roar/json'
require 'pact_broker/api/decorators/format_date_time'

module PactBroker

module Api

module Decorators

module Timestamps

include Roar::JSON

property :optional_updated_at, as: :updatedAt, exec_context: :decorator, writeable: false
property :createdAt, getter: lambda { |_| created_at.xmlschema }, writeable: false
property :createdAt, getter: lambda { |_| FormatDateTime.call(created_at) }, writeable: false

def optional_updated_at
if represented.respond_to?(:updated_at) && represented.updated_at != represented.created_at
represented.updated_at.xmlschema
FormatDateTime.call(represented.updated_at)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"verifiedAt": "2018-01-01T00:00:00+00:00"
},
"latestWebhookExecution": {
"triggeredAt": "2018-01-01T00:00:00+00:00"
"triggeredAt": "2017-12-31T13:00:00+00:00"
},
"pactTags": [
{
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module Decorators
end

it "includes the createdAt date" do
expect(subject[:createdAt]).to eq created_at.xmlschema
expect(subject[:createdAt]).to eq FormatDateTime.call(created_at)
end

it "includes a link to itself" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module Decorators
subject { JSON.parse PacticipantDecorator.new(pacticipant).to_json(user_options: {base_url: base_url}), symbolize_names: true }

it "includes timestamps" do
expect(subject[:createdAt]).to eq created_at.xmlschema
expect(subject[:updatedAt]).to eq updated_at.xmlschema
expect(subject[:createdAt]).to eq FormatDateTime.call(created_at)
expect(subject[:updatedAt]).to eq FormatDateTime.call(updated_at)
end

it "includes embedded labels" do
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ module Decorators
end

it 'includes timestamps' do
expect(parsed_json[:createdAt]).to eq created_at.xmlschema
expect(parsed_json[:updatedAt]).to eq updated_at.xmlschema
expect(parsed_json[:createdAt]).to eq FormatDateTime.call(created_at)
expect(parsed_json[:updatedAt]).to eq FormatDateTime.call(updated_at)
end

context 'when the headers are empty' do
Expand Down

0 comments on commit a423111

Please sign in to comment.