Sha256: 7cf06402716448131d1f8136efe5cafdd9d0225cf8cd70368970efe189b7e934

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

require_relative 'post'

module Contentful
  module Exporter
    module Wordpress
      class PostAttachment < Post
        attr_reader :post, :settings

        def initialize(post, settings)
          @post = post
          @settings = settings
        end

        def attachment_extractor
          create_directory("#{settings.assets_dir}/attachment_post")
          asset = { id: attachment_id, description: attachment_description, url: attachment_url }
          unless asset[:url].nil?
            write_json_to_file("#{settings.assets_dir}/attachment_post/#{attachment_id}.json", asset)
            asset
          end
        end

        private

        def attachment_url
          post.at_xpath('wp:attachment_url').text unless post.at_xpath('wp:attachment_url').nil?
        end

        def attachment_id
          "attachment_#{post_id(post)}"
        end

        def attachment_description
          meta_arr = post.xpath('wp:postmeta').to_a
          unless meta_arr.empty?
            meta_arr.each do |meta|
              return meta.at_xpath('wp:meta_value').text if meta.at_xpath('wp:meta_key').text == '_wp_attachment_image_alt'
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wordpress-exporter-0.0.1 lib/wordpress/post_attachment.rb