Module: Ms::Mascot::Dat::Archive::Utils

Included in:
Ms::Mascot::Dat::Archive
Defined in:
lib/ms/mascot/dat/archive.rb

Class Method Summary collapse

Class Method Details

.content_type_class(metadata) ⇒ Object

Resolves a content type class from a hash of metadata like:

 = {
  :content_type => 'application/x-Mascot',
  :section_name => 'header'
}
Dat.content_type_class()   # => Ms::Mascot::Dat::Header

Raises an error if the content type is not ‘application/x-Mascot’ or if the name is not registered in CONTENT_TYPE_CLASSES.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ms/mascot/dat/archive.rb', line 73

def content_type_class()
  unless [:content_type] == 'application/x-Mascot'
    raise "unknown content_type: #{.inspect}"
  end
  
  name = [:section_name]
  name = 'query' if name =~ /^query(\d+)$/
  case const = CONTENT_TYPE_CLASSES[name]
  when String
    require const
    CONTENT_TYPE_CLASSES[name] = Dat.const_get(name.capitalize)
  else
    const
  end
end

.parse_content_type(str) ⇒ Object

Parses a mascot-style content type declaration. This method uses a simple regexp and is very brittle, but it works for all known dat files.



55
56
57
58
59
60
61
# File 'lib/ms/mascot/dat/archive.rb', line 55

def parse_content_type(str)
  unless str =~ /^Content-Type: (.*?); name=\"(.*)\"/
    raise "unparseable content-type declaration: #{str.inspect}"
  end
  
  {:content_type => $1, :section_name => $2}
end

.parse_metadata(io) ⇒ Object

Parses a hash of metadata (content_type, boundary, etc) from io. parse_metadata does not reposition io.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ms/mascot/dat/archive.rb', line 29

def (io)
  current_pos = io.pos
  io.rewind
  
   = {}
  line = io.readline
  unless line =~ /MIME-Version: (\d+\.\d+) \(Generated by Mascot version (\d+\.\d+)\)/
    raise "could not parse mime-version or mascot-version: #{line}"
  end
  [:mime_version] = $1
  [:mascot_version] = $2
  
  line = io.readline
  unless line =~ /Content-Type: (.*?); boundary=(.*)/
    raise "could not parse content-type: #{line}"
  end
  [:content_type] = $1
  [:boundary] = $2
  
  io.pos = current_pos
  
end