Class: Stylish::Developer::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/stylish/developer/path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject (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_typeObject (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

.sprocketsObject



35
36
37
# File 'lib/stylish/developer/path.rb', line 35

def self.sprockets
  @sprockets ||= Stylish::Developer.config.environment
end

Instance Method Details

#assetObject



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

Returns:

  • (Boolean)


43
44
45
# File 'lib/stylish/developer/path.rb', line 43

def asset_exists?
  expanded_path.exist?
end

#compiled?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/stylish/developer/path.rb', line 59

def compiled?
  request_type == "compiled"
end

#compiled_asset_contentObject



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.message }"
  end
end

#content?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/stylish/developer/path.rb', line 55

def content?
  request_type == "content"
end

#content_typeObject



108
109
110
# File 'lib/stylish/developer/path.rb', line 108

def content_type
  asset.content_type
end

#error?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/stylish/developer/path.rb', line 159

def error?
  status_code == 500
end

#exists_under_root?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/stylish/developer/path.rb', line 27

def exists_under_root?
  expanded_path.exist?
end

#expanded_pathObject



39
40
41
# File 'lib/stylish/developer/path.rb', line 39

def expanded_path
  Pathname(request_path).expand_path(Stylish::Developer.config.root)
end

#extensionObject



104
105
106
# File 'lib/stylish/developer/path.rb', line 104

def extension
  File.extname(asset.logical_path)
end

#metaObject



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 meta
  {
    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

Returns:

  • (Boolean)


51
52
53
# File 'lib/stylish/developer/path.rb', line 51

def meta?
  request_type == "meta"
end

#not_found?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/stylish/developer/path.rb', line 155

def not_found?
  status_code == 404
end

#prefixObject



116
117
118
# File 'lib/stylish/developer/path.rb', line 116

def prefix
  Stylish::Developer.config.base_url
end

#raw_asset_contentObject



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.message}"
  end
end

#relative_pathnameObject



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_bodyObject



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 meta?
                         meta.to_json
                       end
                     end
end

#response_headersObject



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

#sprocketsObject



31
32
33
# File 'lib/stylish/developer/path.rb', line 31

def sprockets
  self.class.sprockets
end

#sprockets_assetObject



47
48
49
# File 'lib/stylish/developer/path.rb', line 47

def sprockets_asset
  sprockets.find_asset(asset)
end

#status_codeObject



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

Returns:

  • (Boolean)


163
164
165
# File 'lib/stylish/developer/path.rb', line 163

def success?
  status_code == 200 || status_code == 304
end

#to_rack_responseObject



151
152
153
# File 'lib/stylish/developer/path.rb', line 151

def to_rack_response
  [status_code, response_headers, [response_body]]
end