Sha256: 2f66e22e7cbda424986a586bf7f0f7ebc13597770cf715df15c9d9916a784c68

Contents?: true

Size: 655 Bytes

Versions: 3

Compression:

Stored size: 655 Bytes

Contents

module Imgurapi
  class FileType

    GIF = /^GIF8/
    PNG = Regexp.new("^\x89PNG".force_encoding('binary'))
    JPG = Regexp.new("^\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding('binary'))
    JPG2 = Regexp.new("^\xff\xd8\xff\xe1(.*){2}Exif".force_encoding('binary'))

    def initialize(path)
      @path = path
    end

    def mime_type
      case IO.read(@path, 10)
      when GIF
        'image/gif'
      when PNG
        'image/png'
      when JPG
        'image/jpeg'
      when JPG2
        'image/jpeg'
      end
    end

    def image?
      !!mime_type
    end

    def url?
      !!(@path =~ %r(^(http://|https://|ftp://)))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
imgurapi-3.2.2 lib/imgurapi/file_type.rb
imgurapi-3.2.1 lib/imgurapi/file_type.rb
imgurapi-3.1.0 lib/imgurapi/file_type.rb