Class: Rack::StaticBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/static-builder.rb

Defined Under Namespace

Classes: BuildError, RequestPathQueue

Constant Summary collapse

VERSION =
'0.1.4'

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ StaticBuilder

Returns a new instance of StaticBuilder.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rack/static-builder.rb', line 42

def initialize(opts)
  opts = opts.dup

  @app_dir = [opts.delete(:app_dir), 'app', '.'].compact.find{ |d| File.file?(d + '/config.ru') }
  raise ArgumentError unless @app_dir

  @app_dir = Pathname.new(@app_dir).expand_path.cleanpath
  @dest_dir = Pathname.new(opts.delete(:dest_dir) || 'dist').expand_path.cleanpath
  @app_static_dir = (@app_dir + (opts.delete(:static_dir) || 'public')).expand_path.cleanpath

  @noise_level = (opts.delete(:noise_level) || '0').to_i
  @preserve_on_error = opts.delete(:preserve_on_error)
end

Instance Method Details

#buildObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rack/static-builder.rb', line 56

def build
  @dest_dir.rmtree if @dest_dir.directory?

  queue = RequestPathQueue.new

  enqueue_static_assets(queue)

  req_stats = {
    :status => Hash.new(0),
    :category => Hash.new(0),
    :succeeded => 0,
    :failed => 0,
    :total => 0
  }

  with_rack_client do |client|

    queue.drain do |req_path|
      resp = client.get req_path

      req_status = resp.status
      req_category = (req_status / 100)
      req_succeeded = (req_category == 2)

      req_stats[:status][req_status] += 1
      req_stats[:category][req_category] += 1
      req_stats[(req_succeeded ? :succeeded : :failed)] += 1
      req_stats[:total] += 1

      if @noise_level > 1 or (!req_succeeded and @noise_level > 0)
        channel = req_succeeded ? $stdout : $stderr
        channel.puts("#{req_status} #{req_path}")
      end

      next unless req_succeeded
      next unless store_response!(req_path, resp.body)

      if enqueue_links = capture_method_for(resp.content_type)
        enqueue_links.call(queue, resp.body)
      end
    end

  end

  if req_stats[:failed] > 0 and not @preserve_on_error
    @dest_dir.rmtree if @dest_dir.directory?
  end

  req_stats
end

#build!Object

Raises:



107
108
109
110
111
112
113
# File 'lib/rack/static-builder.rb', line 107

def build!
  req_stats = self.build

  raise BuildError, "#{ req_stats[:failed] } URLs requested with non-2XX responses" unless (req_stats[:failed] == 0)

  req_stats
end