Class: Utopia::Middleware::Content
- Inherits:
-
Object
- Object
- Utopia::Middleware::Content
- Defined in:
- lib/utopia/middleware/content.rb,
lib/utopia/middleware/content/node.rb
Defined Under Namespace
Classes: Node, Transaction, UnbalancedTagError
Instance Attribute Summary collapse
-
#passthrough ⇒ Object
readonly
Returns the value of attribute passthrough.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #fetch_xml(path) ⇒ Object
-
#initialize(app, options = {}) ⇒ Content
constructor
A new instance of Content.
- #lookup_node(request_path) ⇒ Object
-
#lookup_tag(name, parent_path) ⇒ Object
Look up a named tag such as <entry />.
Constructor Details
#initialize(app, options = {}) ⇒ Content
Returns a new instance of Content.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/utopia/middleware/content.rb', line 17 def initialize(app, = {}) @app = app @root = File.([:root] || Utopia::Middleware::default_root) LOG.info "** #{self.class.name}: Running in #{@root}" # Set to hash to enable caching @nodes = {} @files = nil @tags = [:tags] || {} end |
Instance Attribute Details
#passthrough ⇒ Object (readonly)
Returns the value of attribute passthrough.
32 33 34 |
# File 'lib/utopia/middleware/content.rb', line 32 def passthrough @passthrough end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
31 32 33 |
# File 'lib/utopia/middleware/content.rb', line 31 def root @root end |
Instance Method Details
#call(env) ⇒ Object
98 99 100 101 102 103 104 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 |
# File 'lib/utopia/middleware/content.rb', line 98 def call(env) request = Rack::Request.new(env) path = Path.create(request.path_info).to_absolute # Check if the request is to a non-specific index. name, extensions = path.basename.split(".", 2) directory_path = File.join(@root, path.dirname.components, name) if File.directory? directory_path if extensions index_path = [name, "index.#{extensions}"] else index_path = [name, "index"] end return [307, {"Location" => path.dirname.join(index_path).to_s}, []] end # Otherwise look up the node node = lookup_node(path) if node if request.head? return [200, {}, []] else response = Rack::Response.new node.process!(request, response) return response.finish end else return @app.call(env) end end |
#fetch_xml(path) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/utopia/middleware/content.rb', line 34 def fetch_xml(path) read_file = lambda { Trenni.new(File.read(path), path) } if @files @files.fetch(path) do @files[path] = read_file.call end else read_file.call end end |
#lookup_node(request_path) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/utopia/middleware/content.rb', line 85 def lookup_node(request_path) name = request_path.basename name_xnode = name + ".xnode" node_path = File.join(@root, request_path.dirname.components, name_xnode) if File.exist? node_path return Node.new(self, request_path.dirname + name, request_path, node_path) end return nil end |
#lookup_tag(name, parent_path) ⇒ Object
Look up a named tag such as <entry />
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/utopia/middleware/content.rb', line 47 def lookup_tag(name, parent_path) if @tags.key? name return @tags[name] elsif Utopia::Tags.all.key? name return Utopia::Tags.all[name] end if String === name && name.index("/") name = Path.create(name) end if Path === name name = parent_path + name name_path = name.components.dup name_path[-1] += ".xnode" else name_path = name + ".xnode" end parent_path.ascend do |dir| tag_path = File.join(root, dir.components, name_path) if File.exist? tag_path return Node.new(self, dir + name, parent_path + name, tag_path) end if String === name_path tag_path = File.join(root, dir.components, "_" + name_path) if File.exist? tag_path return Node.new(self, dir + name, parent_path + name, tag_path) end end end return nil end |