Class: Stylish::Developer::Path
- Inherits:
-
Object
- Object
- Stylish::Developer::Path
- Defined in:
- lib/stylish/developer/path.rb
Instance Attribute Summary collapse
-
#request_path ⇒ Object
readonly
Returns the value of attribute request_path.
-
#request_type ⇒ Object
readonly
Returns the value of attribute request_type.
Class Method Summary collapse
Instance Method Summary collapse
- #asset ⇒ Object
- #asset_exists? ⇒ Boolean
- #compiled? ⇒ Boolean
- #compiled_asset_content ⇒ Object
- #content? ⇒ Boolean
- #content_type ⇒ Object
- #error? ⇒ Boolean
- #exists_under_root? ⇒ Boolean
- #expanded_path ⇒ Object
- #extension ⇒ Object
-
#initialize(request_path, request_type = "content") ⇒ Path
constructor
A new instance of Path.
- #meta ⇒ Object
- #meta? ⇒ Boolean
- #not_found? ⇒ Boolean
- #prefix ⇒ Object
- #raw_asset_content ⇒ Object
- #relative_pathname ⇒ Object
- #response_body ⇒ Object
- #response_headers ⇒ Object
- #sprockets ⇒ Object
- #sprockets_asset ⇒ Object
- #status_code ⇒ Object
- #success? ⇒ Boolean
- #to_rack_response ⇒ Object
Constructor Details
#initialize(request_path, request_type = "content") ⇒ Path
Returns a new instance of Path.
6 7 8 9 |
# File 'lib/stylish/developer/path.rb', line 6 def initialize(request_path, request_type="content") @request_path = request_path.to_s @request_type = request_type end |
Instance Attribute Details
#request_path ⇒ Object (readonly)
Returns the value of attribute request_path.
4 5 6 |
# File 'lib/stylish/developer/path.rb', line 4 def request_path @request_path end |
#request_type ⇒ Object (readonly)
Returns the value of attribute request_type.
4 5 6 |
# File 'lib/stylish/developer/path.rb', line 4 def request_type @request_type end |
Class Method Details
Instance Method Details
#asset ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/stylish/developer/path.rb', line 11 def asset @asset ||= begin parts = request_path.split('/') found = !!(sprockets.find_asset(request_path)) until found || parts.empty? test = parts.join('/') found = !!!(sprockets.find_asset(test).nil?) parts.shift unless found end sprockets.find_asset Array(parts).join('/') end end |
#asset_exists? ⇒ Boolean
43 44 45 |
# File 'lib/stylish/developer/path.rb', line 43 def asset_exists? .exist? end |
#compiled? ⇒ Boolean
59 60 61 |
# File 'lib/stylish/developer/path.rb', line 59 def compiled? request_type == "compiled" end |
#compiled_asset_content ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/stylish/developer/path.rb', line 96 def compiled_asset_content begin asset.to_s rescue => exception "Error compiling asset content:\n\n#{ exception. }" end end |
#content? ⇒ Boolean
55 56 57 |
# File 'lib/stylish/developer/path.rb', line 55 def content? request_type == "content" end |
#content_type ⇒ Object
108 109 110 |
# File 'lib/stylish/developer/path.rb', line 108 def content_type asset.content_type end |
#error? ⇒ Boolean
159 160 161 |
# File 'lib/stylish/developer/path.rb', line 159 def error? status_code == 500 end |
#exists_under_root? ⇒ Boolean
27 28 29 |
# File 'lib/stylish/developer/path.rb', line 27 def exists_under_root? .exist? end |
#expanded_path ⇒ Object
39 40 41 |
# File 'lib/stylish/developer/path.rb', line 39 def Pathname(request_path).(Stylish::Developer.config.root) end |
#extension ⇒ Object
104 105 106 |
# File 'lib/stylish/developer/path.rb', line 104 def extension File.extname(asset.logical_path) end |
#meta ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/stylish/developer/path.rb', line 120 def { logical_path: asset.logical_path, mtime: asset.mtime.to_i.to_s, digest: asset.digest, pathname: relative_pathname.to_s, size: asset.bytesize.to_s, urls: { meta_url: "#{ prefix }/meta/#{relative_pathname}", compiled_url: "#{ prefix }/compiled/#{relative_pathname}", content_url: "#{ prefix }/content/#{relative_pathname}" }, dependencies: asset.dependencies.map do |dependency| dependency.logical_path end } end |
#meta? ⇒ Boolean
51 52 53 |
# File 'lib/stylish/developer/path.rb', line 51 def request_type == "meta" end |
#not_found? ⇒ Boolean
155 156 157 |
# File 'lib/stylish/developer/path.rb', line 155 def not_found? status_code == 404 end |
#prefix ⇒ Object
116 117 118 |
# File 'lib/stylish/developer/path.rb', line 116 def prefix Stylish::Developer.config.base_url end |
#raw_asset_content ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/stylish/developer/path.rb', line 88 def raw_asset_content begin asset.pathname.read rescue => exception "Error reading asset content:\n\n#{exception.}" end end |
#relative_pathname ⇒ Object
112 113 114 |
# File 'lib/stylish/developer/path.rb', line 112 def relative_pathname Pathname(asset.pathname.relative_path_from(Stylish::Developer.config.root)) end |
#response_body ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/stylish/developer/path.rb', line 70 def response_body @response_body ||= begin case when not_found? { status: 404, message: "Not Found" }.to_json when content? asset.pathname.read when compiled? asset.to_s when .to_json end end end |
#response_headers ⇒ Object
63 64 65 66 67 68 |
# File 'lib/stylish/developer/path.rb', line 63 def response_headers { "Content-Length" => "#{Rack::Utils.bytesize(response_body)}", "Content-Type" => content_type } end |
#sprockets ⇒ Object
31 32 33 |
# File 'lib/stylish/developer/path.rb', line 31 def sprockets self.class.sprockets end |
#sprockets_asset ⇒ Object
47 48 49 |
# File 'lib/stylish/developer/path.rb', line 47 def sprockets_asset sprockets.find_asset(asset) end |
#status_code ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/stylish/developer/path.rb', line 138 def status_code @status_code ||= begin case when exists_under_root? 200 when !exists_under_root? 404 else 500 end end end |
#success? ⇒ Boolean
163 164 165 |
# File 'lib/stylish/developer/path.rb', line 163 def success? status_code == 200 || status_code == 304 end |
#to_rack_response ⇒ Object
151 152 153 |
# File 'lib/stylish/developer/path.rb', line 151 def to_rack_response [status_code, response_headers, [response_body]] end |