Sha256: 9ce95a1614fdbc186773fdfc34486100272444126ff4c90e0a9bbbac2308ef69

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

module AppleNews
  class Article
    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, mime)
          @files[name] = UploadIO.new(StringIO.new(contents), mime, name)
        end

        def add_file_at_url(name, url, mime)
          @files[name] = UploadIO.new(open(url), mime, 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

4 entries across 4 versions & 1 rubygems

Version Path
apple-news-0.3.0 lib/apple-news/article/attachments.rb
apple-news-0.2.5 lib/apple-news/article/attachments.rb
apple-news-0.2.4 lib/apple-news/article/attachments.rb
apple-news-0.2.3 lib/apple-news/article/attachments.rb