diff --git a/lib/pact_broker/api/decorators/base_decorator.rb b/lib/pact_broker/api/decorators/base_decorator.rb index 5a900653a..1d22dee61 100644 --- a/lib/pact_broker/api/decorators/base_decorator.rb +++ b/lib/pact_broker/api/decorators/base_decorator.rb @@ -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 @@ -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 diff --git a/lib/pact_broker/api/decorators/dashboard_decorator.rb b/lib/pact_broker/api/decorators/dashboard_decorator.rb index 785c4f997..8dc5e8c93 100644 --- a/lib/pact_broker/api/decorators/dashboard_decorator.rb +++ b/lib/pact_broker/api/decorators/dashboard_decorator.rb @@ -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 @@ -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) @@ -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) @@ -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 diff --git a/lib/pact_broker/api/decorators/format_date_time.rb b/lib/pact_broker/api/decorators/format_date_time.rb new file mode 100644 index 000000000..bfd81a9b3 --- /dev/null +++ b/lib/pact_broker/api/decorators/format_date_time.rb @@ -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 diff --git a/lib/pact_broker/api/decorators/matrix_decorator.rb b/lib/pact_broker/api/decorators/matrix_decorator.rb index d94600068..1d12d48cf 100644 --- a/lib/pact_broker/api/decorators/matrix_decorator.rb +++ b/lib/pact_broker/api/decorators/matrix_decorator.rb @@ -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 @@ -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) @@ -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) diff --git a/lib/pact_broker/api/decorators/timestamps.rb b/lib/pact_broker/api/decorators/timestamps.rb index 548b37730..140a97700 100644 --- a/lib/pact_broker/api/decorators/timestamps.rb +++ b/lib/pact_broker/api/decorators/timestamps.rb @@ -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 diff --git a/spec/fixtures/dashboard.json b/spec/fixtures/dashboard.json index 0ce6f1d11..0e877c973 100644 --- a/spec/fixtures/dashboard.json +++ b/spec/fixtures/dashboard.json @@ -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": [ { diff --git a/spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb index b52caaafe..f07bbdf38 100644 --- a/spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/pacticipant_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/pacticipant_decorator_spec.rb index 60f08b2ef..8f95d69c5 100644 --- a/spec/lib/pact_broker/api/decorators/pacticipant_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/pacticipant_decorator_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb index 6d3487491..3abdd3f43 100644 --- a/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/webhook_decorator_spec.rb @@ -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