Sha256: abee3a658282d01f409f3244d804483103313203fcc07f6ed56864b084cdf34f

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require_relative "schemaless_rest_api/version"
require_relative "schemaless_rest_api/entities"

require "json"
require "securerandom"

def models_env_not_set
  !ENV["models"] || ENV["models"].empty?
end

def extract_models
  return [] if (!Entities.models.empty? && models_env_not_set)
  
  error_msg = "Make 'models' environment variable an array (e.g ['model1', 'model2'])"
  raise "Please set 'models' ENV variable or create 'db.json'. #{error_msg}" if ENV["models"].nil?

  models = eval(ENV["models"])
  raise error_msg unless models.is_a? Array

  models
end

SEED_FILE = 'db.json'.freeze

if File.exist? SEED_FILE
  puts "Seeding db based on '#{SEED_FILE}'"
  seed_data = JSON.parse(File.read(SEED_FILE))
  seed_data.each do |entity, values|
    Entities.models[entity.to_sym] = {} unless Entities.models[entity.to_sym]
    if values.is_a? Array
      values.each do |value|
        next unless value['id']
        Entities.models[entity.to_sym][value['id'].to_s] = value
      end
    else 
      puts "Entity 'entity' doesn't have array, skipping"
    end
  end
end

extract_models.each do |model|
  Entities.models[model.to_sym] = {}
end

puts "Modelling #{Entities.models.keys}"

if ENV['mongodb']
  require_relative 'schemaless_rest_api/mongo_client'
else
  puts "[INFO] Using in memory storage. Pass in 'mongodb' ENV variable to store
in db"
end
require_relative "schemaless_rest_api/rest_server"

module SchemalessRestApi
  class Error < StandardError; end
  # Your code goes here...
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schemaless_rest_api-0.3.1 lib/schemaless_rest_api.rb
schemaless_rest_api-0.3.0 lib/schemaless_rest_api.rb