Class: Imgurapi::FileType

Inherits:
Object
  • Object
show all
Defined in:
lib/imgurapi/file_type.rb

Constant Summary collapse

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'))

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileType

Returns a new instance of FileType.



9
10
11
# File 'lib/imgurapi/file_type.rb', line 9

def initialize(path)
  @path = path
end

Instance Method Details

#image?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/imgurapi/file_type.rb', line 26

def image?
  !!mime_type
end

#mime_typeObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/imgurapi/file_type.rb', line 13

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

#url?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/imgurapi/file_type.rb', line 30

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