schemaless_rest_api 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/schemaless_rest_api +7 -7
- data/lib/schemaless_rest_api/entities.rb +9 -9
- data/lib/schemaless_rest_api/mongo_client.rb +42 -0
- data/lib/schemaless_rest_api/rest_server.rb +97 -67
- data/lib/schemaless_rest_api/version.rb +1 -1
- data/lib/schemaless_rest_api.rb +36 -30
- metadata +32 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbc856f4da6c97a932cf00591ae9330a8c11107f508394d7d17623bdba76427c
|
4
|
+
data.tar.gz: 1e3286aa8f268ccbb8ce21fb9cf155e213ecaaa7cbcb5c44253bc225829118b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0875ee228c90497a905699ea7c33e85dd4885178533be0551cec74000bfa743cb21630c7bba96322f88336c23dfce045fb0d372a7813d9ec85f2baf19da7cb37'
|
7
|
+
data.tar.gz: 77402ef68678360df7cee76aa40b21c80eba52c9b90154cff13ce50a8600522147bbd807f248da79b7349ff2f6871c444985db73613fa327708dde9ae0f85f3e
|
data/exe/schemaless_rest_api
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
5
|
-
require "schemaless_rest_api"
|
6
|
-
|
7
|
-
RestServer.run!
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
5
|
+
require "schemaless_rest_api"
|
6
|
+
|
7
|
+
RestServer.run!
|
@@ -1,9 +1,9 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Entities mapped by environment variables
|
4
|
-
class Entities
|
5
|
-
@models = {}
|
6
|
-
class << self
|
7
|
-
attr_accessor :models
|
8
|
-
end
|
9
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Entities mapped by environment variables
|
4
|
+
class Entities
|
5
|
+
@models = {}
|
6
|
+
class << self
|
7
|
+
attr_accessor :models
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
|
3
|
+
module MongoClient
|
4
|
+
class << self
|
5
|
+
# @return Client to work with MongoDb
|
6
|
+
attr_accessor :client
|
7
|
+
|
8
|
+
def insert(model, data, id)
|
9
|
+
collection = MongoClient.client[model]
|
10
|
+
collection.insert_one({ id: id, data: data})
|
11
|
+
end
|
12
|
+
|
13
|
+
def find(model, id)
|
14
|
+
collection = MongoClient.client[model]
|
15
|
+
collection.find( { id: id } ).first
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_all(model)
|
19
|
+
collection = MongoClient.client[model]
|
20
|
+
collection.find.collect { |match| match }
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_all(model, query)
|
24
|
+
collection = MongoClient.client[model]
|
25
|
+
collection.find( query ).collect { |match| match }
|
26
|
+
end
|
27
|
+
|
28
|
+
def update(model, id, data)
|
29
|
+
collection = MongoClient.client[model]
|
30
|
+
collection.update_one({ id: id }, { '$set' => data })
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete(model, id)
|
34
|
+
collection = MongoClient.client[model]
|
35
|
+
collection.delete_one({ id: id })
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
MongoClient.client = Mongo::Client.new(
|
41
|
+
[ "#{ENV['mongodb']}:27017" ],
|
42
|
+
:database => 'api')
|
@@ -1,67 +1,97 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "sinatra"
|
4
|
-
require "puma"
|
5
|
-
require 'route_downcaser'
|
6
|
-
|
7
|
-
class RestServer < Sinatra::Base
|
8
|
-
set :server, :puma
|
9
|
-
enable :logging if ENV["debug"] == "true"
|
10
|
-
set :bind, "0.0.0.0"
|
11
|
-
use RouteDowncaser::DowncaseRouteMiddleware
|
12
|
-
|
13
|
-
def has_id?(model, id)
|
14
|
-
Entities.models[model].key?(id)
|
15
|
-
end
|
16
|
-
|
17
|
-
def
|
18
|
-
[404, JSON.generate({ error: "'#{id}' not found" })]
|
19
|
-
end
|
20
|
-
|
21
|
-
get "/" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sinatra"
|
4
|
+
require "puma"
|
5
|
+
require 'route_downcaser'
|
6
|
+
|
7
|
+
class RestServer < Sinatra::Base
|
8
|
+
set :server, :puma
|
9
|
+
enable :logging if ENV["debug"] == "true"
|
10
|
+
set :bind, "0.0.0.0"
|
11
|
+
use RouteDowncaser::DowncaseRouteMiddleware
|
12
|
+
|
13
|
+
def has_id?(model, id)
|
14
|
+
Entities.models[model].key?(id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def not_have(id)
|
18
|
+
[404, JSON.generate({ error: "'#{id}' not found" })]
|
19
|
+
end
|
20
|
+
|
21
|
+
get "/" do
|
22
|
+
summary = { models: Entities.models.keys.to_s }
|
23
|
+
summary[:db] = MongoClient.client.summary.to_s if ENV['mongodb']
|
24
|
+
JSON.generate(summary)
|
25
|
+
rescue Exception => e
|
26
|
+
[500, e.message]
|
27
|
+
end
|
28
|
+
|
29
|
+
Entities.models.each_key do |model|
|
30
|
+
post "/#{model.downcase}" do
|
31
|
+
id = SecureRandom.uuid
|
32
|
+
data = JSON.parse(request.body.read)
|
33
|
+
if ENV['mongodb']
|
34
|
+
MongoClient.insert(model, data, id)
|
35
|
+
else
|
36
|
+
Entities.models[model][id] = data
|
37
|
+
end
|
38
|
+
[201, id]
|
39
|
+
end
|
40
|
+
|
41
|
+
get "/#{model.downcase}" do
|
42
|
+
puts request.path.downcase
|
43
|
+
if ENV['mongodb']
|
44
|
+
if params == {}
|
45
|
+
JSON.generate(MongoClient.get_all(model))
|
46
|
+
else
|
47
|
+
[200, JSON.generate(MongoClient.find_all(model, params))]
|
48
|
+
end
|
49
|
+
else
|
50
|
+
return [200, Entities.models[model].to_s] if params == {}
|
51
|
+
|
52
|
+
Entities.models[model].values.find_all { |val| val[params.keys[0]] == params.values[0] }
|
53
|
+
end
|
54
|
+
rescue Exception => e
|
55
|
+
[404, "Nothing found using #{params}. Only first param considered. #{e.message}"]
|
56
|
+
end
|
57
|
+
|
58
|
+
get "/#{model.downcase}/:id" do |id|
|
59
|
+
if ENV['mongodb']
|
60
|
+
results = MongoClient.find_all(model, { id: id})
|
61
|
+
return not_have(id) unless results
|
62
|
+
JSON.generate(results.first)
|
63
|
+
else
|
64
|
+
return not_have(id) unless has_id?(model, id)
|
65
|
+
|
66
|
+
JSON.generate(Entities.models[model][id])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
put "/#{model.downcase}/:id" do |id|
|
71
|
+
data = JSON.parse(request.body.read)
|
72
|
+
if ENV['mongodb']
|
73
|
+
results = MongoClient.find(model, id)
|
74
|
+
return not_have(id) unless results.count > 0
|
75
|
+
MongoClient.update(model, id, data)
|
76
|
+
else
|
77
|
+
return not_have(id) unless has_id?(model, id)
|
78
|
+
|
79
|
+
Entities.models[model][id] = data
|
80
|
+
end
|
81
|
+
204
|
82
|
+
end
|
83
|
+
|
84
|
+
delete "/#{model.downcase}/:id" do |id|
|
85
|
+
if ENV['mongodb']
|
86
|
+
results = MongoClient.find(model, id)
|
87
|
+
return not_have(id) unless results.count > 0
|
88
|
+
MongoClient.delete(model, id)
|
89
|
+
else
|
90
|
+
return not_have(id) unless has_id?(model, id)
|
91
|
+
|
92
|
+
Entities.models[model].delete(id)
|
93
|
+
end
|
94
|
+
204
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/lib/schemaless_rest_api.rb
CHANGED
@@ -1,30 +1,36 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "schemaless_rest_api/version"
|
4
|
-
require_relative "schemaless_rest_api/entities"
|
5
|
-
|
6
|
-
require "json"
|
7
|
-
require "securerandom"
|
8
|
-
|
9
|
-
def extract_models
|
10
|
-
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?
|
12
|
-
|
13
|
-
models = eval(ENV["models"])
|
14
|
-
raise error_msg unless models.is_a? Array
|
15
|
-
|
16
|
-
models
|
17
|
-
end
|
18
|
-
|
19
|
-
extract_models.each do |model|
|
20
|
-
Entities.models[model.to_sym] = {}
|
21
|
-
end
|
22
|
-
|
23
|
-
puts "Modelling #{Entities.models.keys}"
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "schemaless_rest_api/version"
|
4
|
+
require_relative "schemaless_rest_api/entities"
|
5
|
+
|
6
|
+
require "json"
|
7
|
+
require "securerandom"
|
8
|
+
|
9
|
+
def extract_models
|
10
|
+
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?
|
12
|
+
|
13
|
+
models = eval(ENV["models"])
|
14
|
+
raise error_msg unless models.is_a? Array
|
15
|
+
|
16
|
+
models
|
17
|
+
end
|
18
|
+
|
19
|
+
extract_models.each do |model|
|
20
|
+
Entities.models[model.to_sym] = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
puts "Modelling #{Entities.models.keys}"
|
24
|
+
|
25
|
+
if ENV['mongodb']
|
26
|
+
require_relative 'schemaless_rest_api/mongo_client'
|
27
|
+
else
|
28
|
+
puts "[INFO] Using in memory storage. Pass in 'mongodb' ENV variable to store
|
29
|
+
in db"
|
30
|
+
end
|
31
|
+
require_relative "schemaless_rest_api/rest_server"
|
32
|
+
|
33
|
+
module SchemalessRestApi
|
34
|
+
class Error < StandardError; end
|
35
|
+
# Your code goes here...
|
36
|
+
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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2022-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mongo
|
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: sinatra
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -24,6 +38,20 @@ dependencies:
|
|
24
38
|
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sinatra-docdsl
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: puma
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -79,6 +107,7 @@ files:
|
|
79
107
|
- exe/schemaless_rest_api
|
80
108
|
- lib/schemaless_rest_api.rb
|
81
109
|
- lib/schemaless_rest_api/entities.rb
|
110
|
+
- lib/schemaless_rest_api/mongo_client.rb
|
82
111
|
- lib/schemaless_rest_api/rest_server.rb
|
83
112
|
- lib/schemaless_rest_api/version.rb
|
84
113
|
homepage: https://gitlab.com/samuel-garratt/schemaless_rest_api
|
@@ -103,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
132
|
- !ruby/object:Gem::Version
|
104
133
|
version: '0'
|
105
134
|
requirements: []
|
106
|
-
rubygems_version: 3.2.
|
135
|
+
rubygems_version: 3.2.3
|
107
136
|
signing_key:
|
108
137
|
specification_version: 4
|
109
138
|
summary: TSimple schema less rest API that have entities configured through environment
|