schemaless_rest_api 0.2.2 → 0.3.1

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: 3731a9bf8d9709cc8590747fa3393be24b2bbf08dcbf369885f6117020c55044
4
+ data.tar.gz: f9688d82dd36200103071c0145d9ba30d51e66df14ef4d25b3ba586bd62aad84
5
5
  SHA512:
6
- metadata.gz: abfb1f463d723d366327e16067e751e2e616d7026adabd83365ddf0f5841e69f422ebcfbb1a8b7a6c756606241b8d400052ac496bbf011e9bd807ced28c0ebfb
7
- data.tar.gz: 37a864e55ffd3740404a7ce7d632c3496c9d1e546d4e9a75092a5dfe3655e127a69f2a82c0253f7dccff5faaa7dcb5559549e702f0da760aaad36696300f55a5
6
+ metadata.gz: b890245a0f5a8d6ef1882bd6a84836bbffd392727b6451e163aaf10cefbc42421866308e03cd06ff285ffa849e52ef2bd96ae5752f4c1fa191f4253b6c406e47
7
+ data.tar.gz: a70a459a193612c444da3ecacc54f6484abc7a8104d3f2e3dca20a168eb6fa586319fe36d03a387f8b270fe0fb0db918ce04cbf732afde1ac61bd1291e4a62e6
@@ -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.1"
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,15 +1,29 @@
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.1
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
+ - !ruby/object:Gem::Dependency
14
+ name: rackup
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: mongo
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -132,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
146
  - !ruby/object:Gem::Version
133
147
  version: '0'
134
148
  requirements: []
135
- rubygems_version: 3.2.3
149
+ rubygems_version: 3.5.3
136
150
  signing_key:
137
151
  specification_version: 4
138
152
  summary: TSimple schema less rest API that have entities configured through environment