Sha256: ff179bcaf1041e3423cea73e009504ee7a10aa910027420e113f7bb2dc8ca22d

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module AppleNews
  class Document
    module Attachments
      extend ActiveSupport::Concern

      included do
        def add_file(file, mime = nil)
          filename = Pathname.new(file).basename.to_s
          mime ||= mime_for(filename)

          @files[filename] = UploadIO.new(file, mime, filename)
        end

        def add_file_at_path(path)
          add_file(File.new(path))
        end

        def add_string_as_file(name, contents, type)
          @files[name] = UploadIO.new(StringIO.new(contents), type, name)
        end

        private

        # I used to use the mime-types gem but apparently Rails uses version
        # 1.x of the gem, which isn't even supported anymore. Since the Apple
        # News API only allows a small handful of attachments, I'm okay with
        # handling them myself.
        def mime_for(filename)
          case File.extname(filename).downcase
          when ".jpg", ".jpeg" then "image/jpeg"
          when ".png" then "image/png"
          when ".gif" then "image/gif"
          else "application/octet-stream"
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apple-news-0.1.1 lib/apple-news/document/attachments.rb