schemaless_rest_api 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a86e21c3979d2542206f5c993491444ee5e91e38d348eaaf4cc4a6a242eaf11f
4
- data.tar.gz: 2c82b7556b1ed4ce916224bdcc9404efe20283d7a7c7f2564bc750968b9f9cc3
3
+ metadata.gz: 5970ba634fc9a47c750d7716b380b5af871942554a0f06b9927cb1f612a39d5d
4
+ data.tar.gz: 2b6734f5b0f5b102e39d488e8f27ca2270d3fdff20b0f3a04b4cdf903163e8d0
5
5
  SHA512:
6
- metadata.gz: abfb1f463d723d366327e16067e751e2e616d7026adabd83365ddf0f5841e69f422ebcfbb1a8b7a6c756606241b8d400052ac496bbf011e9bd807ced28c0ebfb
7
- data.tar.gz: 37a864e55ffd3740404a7ce7d632c3496c9d1e546d4e9a75092a5dfe3655e127a69f2a82c0253f7dccff5faaa7dcb5559549e702f0da760aaad36696300f55a5
6
+ metadata.gz: f879739e486794d5464584cad902a764e0eeb8c9a90c1c92f607eef8773a2fb8739260bffff5b8a2c94bb0db2a594f6f267c8b97bd5df7bf7beef140efc00d64
7
+ data.tar.gz: f5ff9ddf4bf0f7f3870fb6a21354397913b8a6b158ca5dc0078e547d50b84097aa179dadae37daaa96960bb1c38cdbf828f7d4c77aff062115cdf4e61bb3a2d4
@@ -42,6 +42,13 @@ module MongoClient
42
42
  end
43
43
  end
44
44
 
45
+ if ENV['mongo_root_password']
45
46
  MongoClient.client = Mongo::Client.new(
46
47
  [ "#{ENV['mongodb']}:27017" ],
47
- :database => 'api')
48
+ database: 'api', password: ENV['mongo_root_password'],
49
+ user: 'root')
50
+ else
51
+ MongoClient.client = Mongo::Client.new(
52
+ [ "#{ENV['mongodb']}:27017" ],
53
+ database: 'api')
54
+ end
@@ -49,14 +49,20 @@ class RestServer < Sinatra::Base
49
49
  status 201
50
50
  end
51
51
  post "/#{model.downcase}" do
52
- id = SecureRandom.uuid
53
52
  data = JSON.parse(request.body.read)
53
+ id = ''
54
+ if (data['id'])
55
+ id = data['id'].to_s
56
+ else
57
+ id = SecureRandom.uuid
58
+ data['id'] = id
59
+ end
54
60
  if ENV['mongodb']
55
61
  MongoClient.insert(model, data, id)
56
62
  else
57
63
  Entities.models[model][id] = data
58
64
  end
59
- [201, id]
65
+ [201, JSON.generate({ id: id })]
60
66
  end
61
67
 
62
68
  documentation "Retrieve all instances of #{model} or filtered by query param" do
@@ -73,7 +79,7 @@ class RestServer < Sinatra::Base
73
79
  [200, JSON.generate(MongoClient.find_all(model, params))]
74
80
  end
75
81
  else
76
- return [200, Entities.models[model].to_s] if params == {}
82
+ return JSON.generate(Entities.models[model].values) if params == {}
77
83
 
78
84
  Entities.models[model].values.find_all { |val| val[params.keys[0]] == params.values[0] }
79
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemalessRestApi
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -6,9 +6,15 @@ require_relative "schemaless_rest_api/entities"
6
6
  require "json"
7
7
  require "securerandom"
8
8
 
9
+ def models_env_not_set
10
+ !ENV["models"] || ENV["models"].empty?
11
+ end
12
+
9
13
  def extract_models
14
+ return [] if (!Entities.models.empty? && models_env_not_set)
15
+
10
16
  error_msg = "Make 'models' environment variable an array (e.g ['model1', 'model2'])"
11
- raise "Please set 'models' ENV variable. #{error_msg}" if ENV["models"].nil?
17
+ raise "Please set 'models' ENV variable or create 'db.json'. #{error_msg}" if ENV["models"].nil?
12
18
 
13
19
  models = eval(ENV["models"])
14
20
  raise error_msg unless models.is_a? Array
@@ -16,6 +22,24 @@ def extract_models
16
22
  models
17
23
  end
18
24
 
25
+ SEED_FILE = 'db.json'.freeze
26
+
27
+ if File.exist? SEED_FILE
28
+ puts "Seeding db based on '#{SEED_FILE}'"
29
+ seed_data = JSON.parse(File.read(SEED_FILE))
30
+ seed_data.each do |entity, values|
31
+ Entities.models[entity.to_sym] = {} unless Entities.models[entity.to_sym]
32
+ if values.is_a? Array
33
+ values.each do |value|
34
+ next unless value['id']
35
+ Entities.models[entity.to_sym][value['id'].to_s] = value
36
+ end
37
+ else
38
+ puts "Entity 'entity' doesn't have array, skipping"
39
+ end
40
+ end
41
+ end
42
+
19
43
  extract_models.each do |model|
20
44
  Entities.models[model.to_sym] = {}
21
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schemaless_rest_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2024-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo