class Resource

Public Class Methods

new(table_of_contents) click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 6
def initialize(table_of_contents)
  @table_of_contents = table_of_contents
end

Public Instance Methods

[](reference) click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 22
def [](reference)
  if reference.is_a?(Integer)
    return self.to_a[reference]
  elsif reference.is_a?(Range)
    return self.to_a[reference]
  elsif reference.is_a?(Symbol)
    reference = reference.to_s
  end

  if reference.is_a?(String)
    reference_data = self.to_a.map do |r|
      r[:uri] if r[:id].eql?(reference) || r[:uri].eql?(reference) || r[:uri_ref].eql?(reference)
    end.compact

    if reference_data && !reference_data.empty?
      return @table_of_contents.parser.zip_file.read(reference_data.first)
    end
  end

  return self.to_a
end
css() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 97
def css
  @css ||= self.to_a.select {|r| r[:type] =~ /text\/css/}
end
each() { |resource| ... } click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 44
def each
  self.to_a.each do |resource|
    yield resource
  end
end
first() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 14
def first
  self.to_a.first
end
fonts() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 89
def fonts
  @fonts ||= self.to_a.select {|r| r[:type] =~ /font/}
end
html() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 101
def html
  @html ||= self.to_a.select {|r| r[:type] =~ /application\/xhtml\+xml/}
end
images() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 81
def images
  @images ||= self.to_a.select {|r| r[:type] =~ /image/}
end
javascripts() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 93
def javascripts
  @js ||= self.to_a.select {|r| r[:type] =~ /text\/javascript/}
end
keys() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 50
def keys
  @keys ||= self.to_a.map{|r| r[:id]}
end
last() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 18
def last
  self.to_a.last
end
length() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 10
def length
  self.count
end
ncx() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 68
def ncx
  @ncx ||=
      begin
        @table_of_contents.ncx.nav_map.map do |n|
          path = URI.decode(n['path'])
          {:uri_ref => path,
           :text => n['label'],
           :uri => @table_of_contents.parser.zip_file.entries.map { |p| p.name }.select { |s| s.match(path.gsub(/\#.*$/, '')) }.first
          }
        end
      end
end
spine() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 59
def spine
  @spine ||=
      begin
        spine_resources = @table_of_contents.spine.first.xpath('./itemref').map { |s| s['idref'] }
        spine_resources.map{|a| self.to_a.select{|t| t[:id].eql?(a)}}.flatten
      end
end
to_a() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 105
def to_a
  @resources ||=
      begin
        resources = []
        @table_of_contents.manifest.xpath('//item').each do |resource|
          if resource
            id = resource.attr('id')
            uri = URI.decode(resource.attr('href'))
            mime_type = resource.attr('media-type')
            label = ''
            uri_ref = ''
            order = ''

            nav_point = @table_of_contents.document.xpath("//navPoint[descendant-or-self::*//content[starts-with(@src, '#{uri}')]]").first

            if nav_point #unless nav_point.nil? || nav_point.empty?
              label = nav_point.at('navLabel text').content || ''
              uri_ref = nav_point.at('content').attr('src') || ''
              order = nav_point.attr('playOrder') || ''
            end

            #TODO:make this an OpenStruct

            uri = @table_of_contents.parser.zip_file.entries.map { |p| p.name }.select { |s| s.match(uri.gsub(/\#.*$/, '')) }.first
            resources << {:id => id,
                          :uri => uri,
                          :uri_ref => uri_ref,
                          :text => label,
                          :type => mime_type,
                          :order => order}
          end
        end

        resources
      end
end
types() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 54
def types
  @types ||= self.to_a.map{|r| r[:type]}.uniq

end
videos() click to toggle source
# File lib/epubinfo/models/table_of_contents/resource.rb, line 85
def videos
  @videos ||= self.to_a.select {|r| r[:type] =~ /video/}
end