Class: RDF::SAK::MimeMagic

Inherits:
MimeMagic
  • Object
show all
Defined in:
lib/rdf/sak/mimemagic.rb

Overview

janky bolt-on MimeMagic

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id) ⇒ Object (private)



69
70
71
# File 'lib/rdf/sak/mimemagic.rb', line 69

def method_missing id
  @type.send id
end

Class Method Details

.all_by_magic(io) ⇒ Object



52
53
54
55
# File 'lib/rdf/sak/mimemagic.rb', line 52

def self.all_by_magic io
  out = super(io)
  out.empty? ? [default_type(io)] : out
end

.binary?(thing) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rdf/sak/mimemagic.rb', line 17

def self.binary? thing
  sample = nil

  # get some stuff out of the IO or get a substring
  if thing.is_a? IO
    pos = thing.tell
    thing.seek 0, 0
    sample = thing.read 100
    thing.seek pos
  else
    sample = thing.to_s[0,100]
  end

  # consider this to be 'binary' if empty
  return true if sample.length == 0
  # control codes minus ordinary whitespace
  sample.b =~ /[\x0-\x8\xe-\x1f\x7f]/ ? true : false
end

.by_extension(io) ⇒ Object



40
41
42
# File 'lib/rdf/sak/mimemagic.rb', line 40

def self.by_extension io
  super(io) || default_type(io)
end

.by_magic(io) ⇒ Object



48
49
50
# File 'lib/rdf/sak/mimemagic.rb', line 48

def self.by_magic io
  super(io) || default_type(io)
end

.by_path(io) ⇒ Object



44
45
46
# File 'lib/rdf/sak/mimemagic.rb', line 44

def self.by_path io
  super(io) || default_type(io)
end

.default_type(thing) ⇒ Object



36
37
38
# File 'lib/rdf/sak/mimemagic.rb', line 36

def self.default_type thing
  new(self.binary?(thing) ? 'application/octet-stream' : 'text/plain')
end

Instance Method Details

#lineageObject

fetches all supertypes



63
64
65
# File 'lib/rdf/sak/mimemagic.rb', line 63

def lineage
  ([self] + parents.map { |t| t.lineage }.flatten).uniq
end

#parentsObject

fetches immediate parent types



58
59
60
# File 'lib/rdf/sak/mimemagic.rb', line 58

def parents
  ::MimeMagic::TYPES.fetch(@type, [nil, []])[1].map { |x| self.class.new x }
end