mongo_api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.byebug_history +18 -0
- data/.env +5 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +73 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +12 -0
- data/lib/mongo_api/base.rb +103 -0
- data/lib/mongo_api/request.rb +190 -0
- data/lib/mongo_api/version.rb +5 -0
- data/lib/mongo_api.rb +5 -0
- data/sig/mongo_api.rbs +4 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6001ced1910df6e81de93e36b958637d92e4e0cb9edc20ca0afa1ca0ec53fa0f
|
4
|
+
data.tar.gz: c07e8e771d53832530d3acc0f5707e7a3970aeb47aabf95d39c4227c1ce735fb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d010ddab038f1603c6d55ba7b79cefeceb1998e49b9aaf0821344f5e99872db5b127a69bf369f96281550b4d1f3ba7ec3d30ee12373958f8f2f078ac3809429f
|
7
|
+
data.tar.gz: 01ef197bcd8f69c308e35b53ce898708e10880cdbc4329d21c16fceddcad82a88cec7c34ce25ca06f574bd6c52e538a4a6d8800f5a76f274a207fce67f6f9cae
|
data/.byebug_history
ADDED
data/.env
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
MONGO_ATLAS_API_URL='https://sa-east-1.aws.data.mongodb-api.com/app/data-ixudq/endpoint/data/v1'
|
2
|
+
MONGO_ATLAS_API_KEY='y2XeNaFtEH2PtFFvJtHS1v9ZnvzaR6r8vUwdPNhWTgGvJfa9XzxFjlVaVAH6aw3z'
|
3
|
+
MONGO_DATABASE_NAME='mongo_api'
|
4
|
+
MONGO_COLLECTION_NAME='users'
|
5
|
+
MONGO_DATASOURCE_NAME='ojeda'
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in mongo_api.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "service_client", "~> 0.1.10"
|
13
|
+
|
14
|
+
group :development, :test do
|
15
|
+
gem "byebug", "~> 11.0"
|
16
|
+
gem "dotenv", "~> 2.7"
|
17
|
+
gem "rubocop", "~> 1.21"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mongo_api (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
byebug (11.1.3)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
dotenv (2.8.1)
|
13
|
+
httparty (0.21.0)
|
14
|
+
mini_mime (>= 1.0.0)
|
15
|
+
multi_xml (>= 0.5.2)
|
16
|
+
json (2.6.3)
|
17
|
+
language_server-protocol (3.17.0.3)
|
18
|
+
mini_mime (1.1.2)
|
19
|
+
multi_xml (0.6.0)
|
20
|
+
parallel (1.23.0)
|
21
|
+
parser (3.2.2.3)
|
22
|
+
ast (~> 2.4.1)
|
23
|
+
racc
|
24
|
+
racc (1.7.1)
|
25
|
+
rainbow (3.1.1)
|
26
|
+
rake (13.0.6)
|
27
|
+
regexp_parser (2.8.1)
|
28
|
+
rexml (3.2.6)
|
29
|
+
rspec (3.12.0)
|
30
|
+
rspec-core (~> 3.12.0)
|
31
|
+
rspec-expectations (~> 3.12.0)
|
32
|
+
rspec-mocks (~> 3.12.0)
|
33
|
+
rspec-core (3.12.2)
|
34
|
+
rspec-support (~> 3.12.0)
|
35
|
+
rspec-expectations (3.12.3)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-mocks (3.12.5)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.12.0)
|
41
|
+
rspec-support (3.12.0)
|
42
|
+
rubocop (1.55.1)
|
43
|
+
json (~> 2.3)
|
44
|
+
language_server-protocol (>= 3.17.0)
|
45
|
+
parallel (~> 1.10)
|
46
|
+
parser (>= 3.2.2.3)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
regexp_parser (>= 1.8, < 3.0)
|
49
|
+
rexml (>= 3.2.5, < 4.0)
|
50
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
51
|
+
ruby-progressbar (~> 1.7)
|
52
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
53
|
+
rubocop-ast (1.29.0)
|
54
|
+
parser (>= 3.2.1.0)
|
55
|
+
ruby-progressbar (1.13.0)
|
56
|
+
service_client (0.1.10)
|
57
|
+
httparty (~> 0.21.0)
|
58
|
+
unicode-display_width (2.4.2)
|
59
|
+
|
60
|
+
PLATFORMS
|
61
|
+
x86_64-linux
|
62
|
+
|
63
|
+
DEPENDENCIES
|
64
|
+
byebug (~> 11.0)
|
65
|
+
dotenv (~> 2.7)
|
66
|
+
mongo_api!
|
67
|
+
rake (~> 13.0)
|
68
|
+
rspec (~> 3.0)
|
69
|
+
rubocop (~> 1.21)
|
70
|
+
service_client (~> 0.1.10)
|
71
|
+
|
72
|
+
BUNDLED WITH
|
73
|
+
2.4.12
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Alef Ojeda de Oliveira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# MongoApi
|
2
|
+
|
3
|
+
The gem 'mongo_api' is a simple wrapper around the MongoDb Atlas Data API to make it easier to use in Ruby applications to access data in MongoDb Atlas.
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mongo_api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add mongo_api
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install mongo_api
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
The gem is a simple wrapper around the MongoDb Atlas Data API to make it easier to use in Ruby applications to access data in MongoDb Atlas.
|
20
|
+
|
21
|
+
### Example
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'mongo_api'
|
25
|
+
|
26
|
+
class Users < MongoApi::Base
|
27
|
+
base_url ENV['MONGO_ATLAS_API_URL']
|
28
|
+
default_headers 'Content-Type': 'application/json',
|
29
|
+
'api-key': ENV['MONGO_ATLAS_API_KEY'],
|
30
|
+
'Access-Control-Request-Headers': '*'
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Development
|
35
|
+
|
36
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
37
|
+
|
38
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongo_api.
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "mongo_api/request"
|
4
|
+
|
5
|
+
module MongoApi
|
6
|
+
# MongoApi::Base is a class to help you to create a REST API using MongoDB Atlas Data API.
|
7
|
+
class Base < Request
|
8
|
+
# This method is used to find all documents
|
9
|
+
# Example: Mongo::Atlas::Api::Base.all(limit: 100, sort: { _id: 1 })
|
10
|
+
#
|
11
|
+
# @param [Integer] limit
|
12
|
+
# @param [Hash] sort
|
13
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
14
|
+
def self.all(limit: 100, sort: { _id: 1 })
|
15
|
+
parse_response { find(limit:, sort:) }
|
16
|
+
end
|
17
|
+
|
18
|
+
# This method is used to select documents by filter
|
19
|
+
# Example: Mongo::Atlas::Api::Base.select(filter: { name: 'John' }, limit: 100, sort: { _id: 1 })
|
20
|
+
#
|
21
|
+
# @param [Hash] filter
|
22
|
+
# @param [Integer] limit
|
23
|
+
# @param [Hash] sort
|
24
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
25
|
+
def self.select(filter:, limit: 100, sort: { _id: 1 })
|
26
|
+
parse_response { find(filter:, limit:, sort:) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# This method is used to insert one document
|
30
|
+
# Example: Mongo::Atlas::Api::Base.insert(document)
|
31
|
+
#
|
32
|
+
# @param [Hash] document
|
33
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
34
|
+
def self.insert(document)
|
35
|
+
parse_response { insert_one(document) }
|
36
|
+
end
|
37
|
+
|
38
|
+
# This method is used to insert many documents
|
39
|
+
# Example: Mongo::Atlas::Api::Base.insert_all(documents)
|
40
|
+
#
|
41
|
+
# @param [Array] documents
|
42
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
43
|
+
def self.insert_all(documents)
|
44
|
+
parse_response { insert_many(documents) }
|
45
|
+
end
|
46
|
+
|
47
|
+
# This method is used to delete one document
|
48
|
+
# Example: Mongo::Atlas::Api::Base.destroy(where: { name: 'John' })
|
49
|
+
#
|
50
|
+
# @param [Hash] where
|
51
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
52
|
+
def self.destroy(where: {})
|
53
|
+
parse_response { delete_one(filter: where) }
|
54
|
+
end
|
55
|
+
|
56
|
+
# This method is used to delete many documents
|
57
|
+
# Example: Mongo::Atlas::Api::Base.destroy_all(where: { name: 'John' })
|
58
|
+
#
|
59
|
+
# @param [Hash] where
|
60
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
61
|
+
def self.destroy_all(where: {})
|
62
|
+
parse_response { delete_many(filter: where) }
|
63
|
+
end
|
64
|
+
|
65
|
+
# This method is used to update one document
|
66
|
+
# Example: Mongo::Atlas::Api::Base.update(where: { name: 'John' }, set: { name: 'John Doe' })
|
67
|
+
#
|
68
|
+
# @param [Hash] where
|
69
|
+
# @param [Hash] set
|
70
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
71
|
+
def self.update(where: {}, set: {})
|
72
|
+
parse_response { update_one(filter: where, update: { '$set': set }) }
|
73
|
+
end
|
74
|
+
|
75
|
+
# This method is used to update many documents
|
76
|
+
# Example: Mongo::Atlas::Api::Base.update_all(where: { name: 'John' }, set: { name: 'John Doe' })
|
77
|
+
#
|
78
|
+
# @param [Hash] where
|
79
|
+
# @param [Hash] set
|
80
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
81
|
+
def self.update_all(where: {}, set: {})
|
82
|
+
parse_response { update_many(filter: where, update: { '$set': set }) }
|
83
|
+
end
|
84
|
+
|
85
|
+
# This method is used to count documents
|
86
|
+
# Example: Mongo::Atlas::Api::Base.count
|
87
|
+
#
|
88
|
+
# @return [Integer|ServiceClient::Response] the documents count or the response
|
89
|
+
def self.size
|
90
|
+
count
|
91
|
+
end
|
92
|
+
|
93
|
+
class << self
|
94
|
+
def body
|
95
|
+
{
|
96
|
+
database: ENV["MONGO_DATABASE_NAME"],
|
97
|
+
collection: ENV["MONGO_COLLECTION_NAME"],
|
98
|
+
dataSource: ENV["MONGO_DATASOURCE_NAME"]
|
99
|
+
}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "service_client/base"
|
4
|
+
|
5
|
+
# This class is used to make requests to the cluster mongo-atlas
|
6
|
+
module MongoApi
|
7
|
+
# This class is used to make requests to the cluster mongo-atlas
|
8
|
+
class Request < ServiceClient::Base
|
9
|
+
# This method is used to find one document
|
10
|
+
# Example: Mongo::Atlas::Api::Request.find_one(projection: { _id: 0 })
|
11
|
+
#
|
12
|
+
# @param [Hash] filter
|
13
|
+
# @param [Hash] projection
|
14
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
15
|
+
def self.find_one(filter: {}, projection: {})
|
16
|
+
data = body.merge(projection:, filter:)
|
17
|
+
response = post("action/findOne", body: data.to_json)
|
18
|
+
|
19
|
+
response.data
|
20
|
+
rescue StandardError => e
|
21
|
+
e.response
|
22
|
+
end
|
23
|
+
|
24
|
+
# This method is used to find many documents
|
25
|
+
# Example: Mongo::Atlas::Api::Request
|
26
|
+
# .find(limit: 20, filter: { name: 'John' }, sort: { name: 1 }, projection: { _id: 0 }, skip: 0)
|
27
|
+
#
|
28
|
+
# @param [Integer] limit
|
29
|
+
# @param [Hash] filter
|
30
|
+
# @param [Hash] sort
|
31
|
+
# @param [Hash] projection
|
32
|
+
# @param [Integer] skip
|
33
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
34
|
+
def self.find(limit: 20, filter: {}, sort: {}, projection: {}, skip: 0)
|
35
|
+
data = body.merge(limit:, filter:, sort:, skip:, projection:)
|
36
|
+
response = post("action/find", body: data.to_json)
|
37
|
+
|
38
|
+
response.data
|
39
|
+
rescue StandardError => e
|
40
|
+
e.response
|
41
|
+
end
|
42
|
+
|
43
|
+
# This method is used to insert one document
|
44
|
+
# Example: Mongo::Atlas::Api::Request.insert_one({ name: 'John', email: 'user@teste.com' })
|
45
|
+
#
|
46
|
+
# @param [Hash] document
|
47
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
48
|
+
def self.insert_one(document, url: "action/insertOne")
|
49
|
+
data = url.eql?("action/insertOne") ? body.merge(document:) : body.merge(documents: document)
|
50
|
+
|
51
|
+
response = post(url, body: data.to_json)
|
52
|
+
|
53
|
+
response.data
|
54
|
+
rescue StandardError => e
|
55
|
+
e.response
|
56
|
+
end
|
57
|
+
|
58
|
+
# This method is used to insert many documents
|
59
|
+
# Example: Mongo::Atlas::Api::Request.insert_many([{ name: 'John', email: 'user@teste.com' }, ...])
|
60
|
+
#
|
61
|
+
# @param [Array] documents
|
62
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
63
|
+
def self.insert_many(documents)
|
64
|
+
documents.each_slice(1000).map do |documents_slice|
|
65
|
+
insert_one(documents_slice, url: "action/insertMany")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# This method is used to update one document
|
70
|
+
# Example: Mongo::Atlas::Api::Request.update_one(filter: { name: 'John' }, update: { $set: { name: 'John Doe' } })
|
71
|
+
# @param [Hash] filter
|
72
|
+
# @param [Hash] update
|
73
|
+
# @param [Boolean] upsert
|
74
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
75
|
+
def self.update_one(filter: {}, update: {}, upsert: false)
|
76
|
+
data = body.merge(filter:, update:, upsert:)
|
77
|
+
response = post("action/updateOne", body: data.to_json)
|
78
|
+
|
79
|
+
response.data
|
80
|
+
rescue StandardError => e
|
81
|
+
e.response
|
82
|
+
end
|
83
|
+
|
84
|
+
# This method is used to update many documents
|
85
|
+
# Example: Mongo::Atlas::Api::Request.update_many(filter: { name: 'John' }, update: { $set: { name: 'John Doe' } })
|
86
|
+
#
|
87
|
+
# @param [Hash] filter
|
88
|
+
# @param [Hash] update
|
89
|
+
# @param [Boolean] upsert
|
90
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
91
|
+
def self.update_many(filter: {}, update: {}, upsert: false)
|
92
|
+
data = body.merge(filter:, update:, upsert:)
|
93
|
+
response = post("action/updateMany", body: data.to_json)
|
94
|
+
|
95
|
+
response.data
|
96
|
+
rescue StandardError => e
|
97
|
+
e.response
|
98
|
+
end
|
99
|
+
|
100
|
+
# This method is used to replace one document
|
101
|
+
# Example: Mongo::Atlas::Api::Request.replace_one(filter: { name: 'John' }, replacement: { name: 'John Doe' })
|
102
|
+
#
|
103
|
+
# @param [Hash] filter
|
104
|
+
# @param [Hash] replacement
|
105
|
+
# @param [Boolean] upsert
|
106
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
107
|
+
def self.replace_one(filter: {}, replacement: {}, upsert: false)
|
108
|
+
data = body.merge(filter:, replacement:, upsert:)
|
109
|
+
response = post("action/replaceOne", body: data.to_json)
|
110
|
+
|
111
|
+
response.data
|
112
|
+
rescue StandardError => e
|
113
|
+
e.response
|
114
|
+
end
|
115
|
+
|
116
|
+
# This method is used to delete one document
|
117
|
+
# Example: Mongo::Atlas::Api::Request.delete_one(filter: { name: 'John' })
|
118
|
+
#
|
119
|
+
# @param [Hash] filter
|
120
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
121
|
+
def self.delete_one(filter: {})
|
122
|
+
data = body.merge(filter:)
|
123
|
+
response = post("action/deleteOne", body: data.to_json)
|
124
|
+
|
125
|
+
response.data
|
126
|
+
rescue StandardError => e
|
127
|
+
e.response
|
128
|
+
end
|
129
|
+
|
130
|
+
# This method is used to delete many documents
|
131
|
+
# Example: Mongo::Atlas::Api::Request.delete_many(filter: { name: 'John' })
|
132
|
+
#
|
133
|
+
# @param [Hash] filter
|
134
|
+
# @return [Hash|ServiceClient::Response] the document hash or the response
|
135
|
+
def self.delete_many(filter: {})
|
136
|
+
data = body.merge(filter:)
|
137
|
+
response = post("action/deleteMany", body: data.to_json)
|
138
|
+
|
139
|
+
response.data
|
140
|
+
rescue StandardError => e
|
141
|
+
e.response
|
142
|
+
end
|
143
|
+
|
144
|
+
# This method is used to aggregate documents
|
145
|
+
# Example: Mongo::Atlas::Api::Request.aggregate(pipeline: [{ '$match': { name: 'John' } }])
|
146
|
+
#
|
147
|
+
# @param [Array] pipeline
|
148
|
+
# @return [Array|ServiceClient::Response] the documents array or the response
|
149
|
+
def self.aggregate(pipeline: [])
|
150
|
+
data = body.merge(pipeline:)
|
151
|
+
response = post("action/aggregate", body: data.to_json)
|
152
|
+
|
153
|
+
response.data
|
154
|
+
rescue StandardError => e
|
155
|
+
e.response
|
156
|
+
end
|
157
|
+
|
158
|
+
# This method is used to count documents
|
159
|
+
# Example: Mongo::Atlas::Api::Request.count
|
160
|
+
#
|
161
|
+
# @return [Integer|ServiceClient::Response] the documents count or the response
|
162
|
+
def self.count
|
163
|
+
pipeline = [
|
164
|
+
{ '$group': { _id: nil, count: { '$sum': 1 } } },
|
165
|
+
{ '$project': { _id: 0 } }
|
166
|
+
].freeze
|
167
|
+
|
168
|
+
response = aggregate(pipeline:)
|
169
|
+
|
170
|
+
response["documents"].first["count"]
|
171
|
+
rescue StandardError => e
|
172
|
+
e.response
|
173
|
+
end
|
174
|
+
|
175
|
+
class << self
|
176
|
+
private
|
177
|
+
|
178
|
+
def parse_response(&block)
|
179
|
+
data = block.call
|
180
|
+
|
181
|
+
case data
|
182
|
+
when ServiceClient::Response
|
183
|
+
{ code: data.code, error: data.data }
|
184
|
+
when Hash
|
185
|
+
data["documents"] || data
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/lib/mongo_api.rb
ADDED
data/sig/mongo_api.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alef Ojeda de Oliveira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Use this gem to create a REST API using MongoDB Atlas Data API.
|
14
|
+
email:
|
15
|
+
- alef.oliveira@dimensa.com.br
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".byebug_history"
|
21
|
+
- ".env"
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- lib/mongo_api.rb
|
31
|
+
- lib/mongo_api/base.rb
|
32
|
+
- lib/mongo_api/request.rb
|
33
|
+
- lib/mongo_api/version.rb
|
34
|
+
- sig/mongo_api.rbs
|
35
|
+
homepage: https://github.com/nemuba/mongo_api
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/nemuba/mongo_api
|
40
|
+
source_code_uri: https://github.com/nemuba/mongo_api/tree/main
|
41
|
+
changelog_uri: https://github.com/nemuba/mongo_api/blob/main/CHANGELOG.md
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 3.2.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.4.12
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: MongoApi is a gem to help you to create a REST API using MongoDB Atlas Data
|
61
|
+
API.
|
62
|
+
test_files: []
|