Sha256: 803941888bc0ce1793e71123eabcb16d8a874e44294a4c363f23dd19d7f7a38e

Contents?: true

Size: 1.92 KB

Versions: 21

Compression:

Stored size: 1.92 KB

Contents

module Contentful
  module Management
    module Resource
      # Wrapper for Resources with /published API
      module Publisher
        # Publishes a resource.
        #
        # @return [Contentful::Management::Resource]
        def publish
          ResourceRequester.new(client, self.class).publish(
            self,
            {
              space_id: space.id,
              environment_id: environment_id,
              resource_id: id,
              suffix: '/published'
            },
            version: sys[:version]
          )
        end

        # Unpublishes a resource.
        #
        # @return [Contentful::Management::Resource]
        def unpublish
          ResourceRequester.new(client, self.class).unpublish(
            self,
            {
              space_id: space.id,
              environment_id: environment_id,
              resource_id: id,
              suffix: '/published'
            },
            version: sys[:version]
          )
        end

        # Checks if a resource is published.
        #
        # @return [Boolean]
        def published?
          sys[:publishedAt] ? true : false
        end

        # Checks if a resource has been updated since last publish.
        # Returns false if resource has not been published before.
        #
        # @return [Boolean]
        def updated?
          return false unless sys[:publishedAt]

          sanitize_date(sys[:publishedAt]) < sanitize_date(sys[:updatedAt])
        end

        private

        # In order to have a more accurate comparison due to minimal delays
        # upon publishing entries. We strip milliseconds from the dates we compare.
        #
        # @param date [::DateTime]
        # @return [::Time] without milliseconds.
        def sanitize_date(date)
          time = date.to_time

          ::Time.new(time.year, time.month, time.day, time.hour, time.min, time.sec, time.utc_offset)
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
contentful-management-3.8.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.7.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.6.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.5.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.4.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.3.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.2.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.1.0 lib/contentful/management/resource/publisher.rb
contentful-management-3.0.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.13.1 lib/contentful/management/resource/publisher.rb
contentful-management-2.13.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.12.1 lib/contentful/management/resource/publisher.rb
contentful-management-2.12.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.11.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.10.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.9.1 lib/contentful/management/resource/publisher.rb
contentful-management-2.9.0 lib/contentful/management/resource/publisher.rb
contentful-management-2.8.2 lib/contentful/management/resource/publisher.rb
contentful-management-2.8.1 lib/contentful/management/resource/publisher.rb
contentful-management-2.8.0 lib/contentful/management/resource/publisher.rb