Class: Resource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/epubinfo/models/table_of_contents/resource.rb

Instance Method Summary collapse

Constructor Details

#initialize(table_of_contents) ⇒ Resource

Returns a new instance of Resource.



6
7
8
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 6

def initialize(table_of_contents)
  @table_of_contents = table_of_contents
end

Instance Method Details

#[](reference) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

#cssObject



97
98
99
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 97

def css
  @css ||= self.to_a.select {|r| r[:type] =~ /text\/css/}
end

#eachObject



44
45
46
47
48
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 44

def each
  self.to_a.each do |resource|
    yield resource
  end
end

#firstObject



14
15
16
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 14

def first
  self.to_a.first
end

#fontsObject



89
90
91
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 89

def fonts
  @fonts ||= self.to_a.select {|r| r[:type] =~ /font/}
end

#htmlObject



101
102
103
# 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

#imagesObject



81
82
83
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 81

def images
  @images ||= self.to_a.select {|r| r[:type] =~ /image/}
end

#javascriptsObject



93
94
95
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 93

def javascripts
  @js ||= self.to_a.select {|r| r[:type] =~ /text\/javascript/}
end

#keysObject



50
51
52
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 50

def keys
  @keys ||= self.to_a.map{|r| r[:id]}
end

#lastObject



18
19
20
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 18

def last
  self.to_a.last
end

#lengthObject



10
11
12
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 10

def length
  self.count
end

#ncxObject



68
69
70
71
72
73
74
75
76
77
78
79
# 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

#spineObject



59
60
61
62
63
64
65
# 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_aObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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

#typesObject



54
55
56
57
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 54

def types
  @types ||= self.to_a.map{|r| r[:type]}.uniq

end

#videosObject



85
86
87
# File 'lib/epubinfo/models/table_of_contents/resource.rb', line 85

def videos
  @videos ||= self.to_a.select {|r| r[:type] =~ /video/}
end