class Backdrop::APIBuilder

Public Class Methods

new(default_url = nil) click to toggle source
# File lib/backdrop/api_builder.rb, line 7
def initialize(default_url = nil)
  @config = Backdrop.configuration
  @url = default_url
end

Public Instance Methods

build(input) click to toggle source
# File lib/backdrop/api_builder.rb, line 12
def build(input)
  hashes = Array(input).map(&:to_h)
  url = extract_url(hashes)

  if input.respond_to?(:map)
    write_api(@url || url, hashes)
  else
    write_api(@url || url, hashes.first)
  end
end

Private Instance Methods

extract_url(hashes) click to toggle source
# File lib/backdrop/api_builder.rb, line 25
def extract_url(hashes)
  hashes.reduce(nil) do |_, record|
    record.delete :backdrop_url
  end
end
write_api(url, hashes) click to toggle source
# File lib/backdrop/api_builder.rb, line 31
def write_api(url, hashes)
  base_dir = @config.output_dir
  file = "#{File.join(base_dir, url)}.json"
  FileUtils.mkdir_p(File.dirname(file))
  File.write(file, JSON.generate(hashes))
end