Class: Parklife::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/parklife/build.rb

Constant Summary collapse

META_PATH =
File.join('.parklife', 'build.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, nested_index:) ⇒ Build

Returns a new instance of Build.



44
45
46
47
48
# File 'lib/parklife/build.rb', line 44

def initialize(dir, nested_index:)
  @dir = dir
  @nested_index = nested_index
  @paths = {}
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



42
43
44
# File 'lib/parklife/build.rb', line 42

def dir
  @dir
end

#nested_indexObject (readonly)

Returns the value of attribute nested_index.



42
43
44
# File 'lib/parklife/build.rb', line 42

def nested_index
  @nested_index
end

#pathsObject (readonly)

Returns the value of attribute paths.



42
43
44
# File 'lib/parklife/build.rb', line 42

def paths
  @paths
end

Class Method Details

.from_dir(dir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/parklife/build.rb', line 9

def self.from_dir(dir)
  return unless dir.exist?
  path = dir.join(META_PATH)
  return unless path.exist?
  data = YAML.safe_load(path.read)

  build = new(dir, nested_index: data.dig('config', 'nested_index'))
  build.paths.merge!(data['paths'])
  build
end

.path_for(path, media_type: nil, nested_index:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/parklife/build.rb', line 20

def self.path_for(path, media_type: nil, nested_index:)
  # Remove leading/trailing slashes.
  path = path.gsub(/^\/|\/$/, '')

  # The root is always expected to be an HTML page regardless of the
  # response's content type.
  return 'index.html' if path.empty?

  text_html = media_type.nil? || media_type == 'text/html'

  # Store a text/html response in an .html file.
  if text_html && !path.end_with?('.html')
    if nested_index
      path << '/index.html'
    else
      path << '.html'
    end
  end

  path
end

Instance Method Details

#add(route, response) ⇒ Object



50
51
52
53
54
# File 'lib/parklife/build.rb', line 50

def add(route, response)
  build_path = self.build_path(route, response)
  write(build_path, response.body)
  add_path_meta(route, response, build_path)
end

#build_path(route, response) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/parklife/build.rb', line 56

def build_path(route, response)
  self.class.path_for(
    route.path,
    media_type: response.media_type,
    nested_index: nested_index,
  )
end

#copy(src, route, response) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/parklife/build.rb', line 64

def copy(src, route, response)
  build_path = self.build_path(route, response)

  dest = dir.join(build_path)
  dest.dirname.mkpath
  FileUtils.cp(src, dest)

  add_path_meta(route, response, build_path)
end

#etag(path) ⇒ Object



74
75
76
# File 'lib/parklife/build.rb', line 74

def etag(path)
  paths.dig(path, 'etag')
end

#get(route, response) ⇒ Object



78
79
80
# File 'lib/parklife/build.rb', line 78

def get(route, response)
  dir.join(build_path(route, response))
end

#to_yamlObject



82
83
84
85
86
87
88
89
# File 'lib/parklife/build.rb', line 82

def to_yaml
  YAML.dump({
    'config' => {
      'nested_index' => nested_index,
    },
    'paths' => paths,
  })
end

#write(path, content) ⇒ Object



91
92
93
94
95
# File 'lib/parklife/build.rb', line 91

def write(path, content)
  file = dir.join(path)
  file.dirname.mkpath
  file.write(content, mode: 'wb')
end

#write_metaObject



97
98
99
# File 'lib/parklife/build.rb', line 97

def write_meta
  write(META_PATH, to_yaml)
end