Class: Mascot::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mascot.rb

Overview

Represents a page in a web server context.

Constant Summary collapse

DEFAULT_MIME_TYPE =

If we can’t resolve a mime type for the resource, we’ll fall back to this binary octet-stream type so the client can download the resource and figure out what to do with it.

MIME::Types["application/octet-stream"].first

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_path:, file_path:, mime_type: nil) ⇒ Resource

Returns a new instance of Resource.



45
46
47
48
49
50
# File 'lib/mascot.rb', line 45

def initialize(request_path: , file_path: , mime_type: nil)
  @request_path = request_path
  @file_path = Pathname.new file_path
  @frontmatter = Frontmatter.new File.read @file_path
  @mime_types = Array(mime_type) if mime_type
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



40
41
42
# File 'lib/mascot.rb', line 40

def file_path
  @file_path
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



40
41
42
# File 'lib/mascot.rb', line 40

def request_path
  @request_path
end

Instance Method Details

#extensionsObject

List of all file extensions.



53
54
55
# File 'lib/mascot.rb', line 53

def extensions
  @file_path.basename.to_s.split(".").drop(1)
end

#format_extensionObject

Returns the format extension.



58
59
60
# File 'lib/mascot.rb', line 58

def format_extension
  extensions.first
end

#mime_typeObject



67
68
69
# File 'lib/mascot.rb', line 67

def mime_type
  (@mime_types ||= Array(resolve_mime_type)).push(DEFAULT_MIME_TYPE).first
end

#template_extensionsObject

Returns a list of the rendering extensions.



63
64
65
# File 'lib/mascot.rb', line 63

def template_extensions
  extensions.drop(1)
end