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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58b65be1caee8add0cb91dbfbc1b84bbbd16b374682ee950ae2c0b2646dca30a
4
- data.tar.gz: 0f0758e772cae144cd38f321f561fcc39cf33bcb2a93b4655c4be637011d94de
3
+ metadata.gz: dbc856f4da6c97a932cf00591ae9330a8c11107f508394d7d17623bdba76427c
4
+ data.tar.gz: 1e3286aa8f268ccbb8ce21fb9cf155e213ecaaa7cbcb5c44253bc225829118b0
5
5
  SHA512:
6
- metadata.gz: bcf04f3387d292ae0fdc144acda02603a4e712c8f4e0e5841bee098e307c5f051d590b025d5adf1542d16a41ff8e3eca8f538af81306fb660d55d1e3686112af
7
- data.tar.gz: 17aeea57b9ac4655f9ba691a9f3b3452244ded19cc06ed124b6bd117bef7ef29c34be2cf19f9342076a9df2ddc56e0a282e0473312533cc8a044af3878346578
6
+ metadata.gz: '0875ee228c90497a905699ea7c33e85dd4885178533be0551cec74000bfa743cb21630c7bba96322f88336c23dfce045fb0d372a7813d9ec85f2baf19da7cb37'
7
+ data.tar.gz: 77402ef68678360df7cee76aa40b21c80eba52c9b90154cff13ce50a8600522147bbd807f248da79b7349ff2f6871c444985db73613fa327708dde9ae0f85f3e
@@ -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 not_found(id)
18
- [404, JSON.generate({ error: "'#{id}' not found" })]
19
- end
20
-
21
- get "/" do
22
- JSON.generate({ models: Entities.models.keys.to_s })
23
- end
24
-
25
- # get '*' do
26
- # Could do something like following to make case insensitive. Need all 1 method logic to be in 1 place
27
- # file_path = File.join(File.dirname(__FILE__), 'public', request.path.downcase)
28
- # File.exist?(file_path) ? send_file(file_path) : halt 404
29
- # end
30
-
31
- Entities.models.each_key do |model|
32
- post "/#{model.downcase}" do
33
- id = SecureRandom.uuid
34
- Entities.models[model][id] = JSON.parse(request.body.read)
35
- [201, id]
36
- end
37
-
38
- get "/#{model.downcase}" do
39
- puts request.path.downcase
40
- return [200, Entities.models[model].to_s] if params == {}
41
-
42
- Entities.models[model].values.find_all { |val| val[params.keys[0]] == params.values[0] }
43
- rescue Exception
44
- [404, "Nothing found using #{params}. Only first param considered"]
45
- end
46
-
47
- get "/#{model.downcase}/:id" do |id|
48
- return not_found(id) unless has_id?(model, id)
49
-
50
- JSON.generate(Entities.models[model][id])
51
- end
52
-
53
- put "/#{model.downcase}/:id" do |id|
54
- return not_found(id) unless has_id?(model, id)
55
-
56
- Entities.models[model][id] = JSON.parse(request.body.read)
57
- 204
58
- end
59
-
60
- delete "/#{model.downcase}/:id" do |id|
61
- return not_found(id) unless has_id?(model, id)
62
-
63
- Entities.models[model].delete(id)
64
- 204
65
- end
66
- end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemalessRestApi
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -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
- require_relative "schemaless_rest_api/rest_server"
26
-
27
- module SchemalessRestApi
28
- class Error < StandardError; end
29
- # Your code goes here...
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.1.1
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-12 00:00:00.000000000 Z
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.32
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