class Lightning::Bolt::Cypher

Public Class Methods

new(bookstore) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 5
def initialize(bookstore)
  @bookstore = bookstore
  @reports = []
  bridge(@bookstore)
end

Public Instance Methods

arc(pin) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 27
def arc(pin)
  value = ""
  @reports.each do | report |
    if report["key"].eql?(pin)
      data = report["cypher"]
      xrand = SecureRandom.random_number(data.size)
      value = data[xrand]
    end
  end
  return value
end
bridge(dirname) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 11
def bridge(dirname)
  data = ''
  Dir.foreach(dirname) do |dir|
    path = dirname + '/' + dir
    if File.directory?(path) then
      if dir != '.' && dir != '..' then
        data += bridge(path)
      end
    else
      capture(path)
      data += path
    end
  end
  return data
end

Private Instance Methods

capture(data) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 53
def capture(data)
  report = {}
  key = "@:" + data.gsub(@bookstore, "").split(".")[0][1..-1].split("/").join("-")
  ext = File.extname(data)
  cypher = nil
  if ext.match(/yma?l/)
    begin
      cypher = YAML.load_file(data)
    rescue
    end
  elsif ext.match(/json/)
    begin
      cypher = JSON.parse(File.read(data))
    rescue
    end
  elsif ext.match(/txt/)
    begin
      cypher = File.read(data).split("\r\n")
    rescue
    end
  else
    # the file type is not supported
  end
  @reports += span(cypher, key)
end
span(data, path) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 41
def span(data, path)
  parts = []
  if data.is_a?(Hash)
    data.each do | key, value |
      parts += span(data[key], "#{path}-#{key}")
    end
  else
    parts.push({"key" => path, "cypher" => data})
  end
  return parts
end
srand(min, max) click to toggle source
# File lib/lightning/bolt/cypher.rb, line 79
def srand(min, max)
  return SecureRandom.random_number(max-min)+min
end