mongo_api 0.1.0 → 0.1.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: 6001ced1910df6e81de93e36b958637d92e4e0cb9edc20ca0afa1ca0ec53fa0f
4
- data.tar.gz: c07e8e771d53832530d3acc0f5707e7a3970aeb47aabf95d39c4227c1ce735fb
3
+ metadata.gz: e35665e93037350fedb5c17c84f80cda9524388ec20c1160a6160e233723d1f7
4
+ data.tar.gz: 20bfae5242c903349258b4ec7fc4532b461eb8ddb0ab606bd4da4aba7d1236ec
5
5
  SHA512:
6
- metadata.gz: d010ddab038f1603c6d55ba7b79cefeceb1998e49b9aaf0821344f5e99872db5b127a69bf369f96281550b4d1f3ba7ec3d30ee12373958f8f2f078ac3809429f
7
- data.tar.gz: 01ef197bcd8f69c308e35b53ce898708e10880cdbc4329d21c16fceddcad82a88cec7c34ce25ca06f574bd6c52e538a4a6d8800f5a76f274a207fce67f6f9cae
6
+ metadata.gz: f262ed2e54e7965b33137af848d08d2ed9e14efa0e7a2573b9ec829da81be1c02c7e5170ce063f32aa46add1ca2ea114d9de03744e7c7f907a9353b798fa3c61
7
+ data.tar.gz: d6391c6921483b9a9017c9295995ef531e7c68676f049b068da0a35df37e58617a7c7c19c5492942fb6688fa6fe8dd216fb49636245cb2b249d9c5af160da0e4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
- ## [Unreleased]
1
+ # [Release]
2
2
 
3
3
  ## [0.1.0] - 2023-08-05
4
4
 
5
5
  - Initial release
6
+ - Add class `MongoApi::Request` and `MongoApi::Base` to handle requests to the API and to handle requests to the database in a simple way on MongoDb Atlas Data API.
7
+ - `MongoApi::Request` methods:
8
+ - `#find_one`: returns a document from a collection by filter and projection
9
+ - `#insert_one`: inserts a document into a collection
10
+ - `#insert_many`: inserts multiple documents into a collection
11
+ - `#update_one`: updates a document from a collection by filter
12
+ - `#update_many`: updates multiple documents from a collection by filter
13
+ - `#replace_one`: replaces a document from a collection by filter
14
+ - `#delete_one`: deletes a document from a collection by filter
15
+ - `#delete_many`: deletes multiple documents from a collection by filter
16
+ - `MongoApi::Base` methods:
17
+ - `#all`: returns all documents from a collection
18
+ - `#find`: returns a document from a collection by filter, projection, sort and limit
19
+ - `#insert`: inserts a document into a collection
20
+ - `#update`: updates a document from a collection by filter
21
+ - `#replace`: replaces a document from a collection by filter
22
+ - `#destroy`: deletes a document from a collection by filter
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongo_api (0.1.0)
4
+ mongo_api (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -18,17 +18,76 @@ If bundler is not being used to manage dependencies, install the gem by executin
18
18
 
19
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
20
 
21
+ Set environment variables for the MongoDb Atlas API URL, API Key, Database Name, Collection Name and Data Source Name:
22
+
23
+ ```env
24
+ MONGO_ATLAS_API_URL=https://cloud.mongodb.com/api/atlas/v1.0
25
+ MONGO_ATLAS_API_KEY=1234567890
26
+ MONGO_DATABASE_NAME=sample_mflix
27
+ MONGO_COLLECTION_NAME=users
28
+ MONGO_DATASOURCE_NAME=sample_mflix
29
+ ```
30
+
21
31
  ### Example
22
32
 
23
33
  ```ruby
24
34
  require 'mongo_api'
25
35
 
26
36
  class Users < MongoApi::Base
27
- base_url ENV['MONGO_ATLAS_API_URL']
37
+ base_url ENV['MONGO_ATLAS_API_URL']
28
38
  default_headers 'Content-Type': 'application/json',
29
39
  'api-key': ENV['MONGO_ATLAS_API_KEY'],
30
40
  'Access-Control-Request-Headers': '*'
31
41
  end
42
+
43
+ # Get all users
44
+ # @param [Hash] filter: The query predicate.
45
+ # @param [Hash] sort: The order in which to return matching documents.
46
+ users = Users.all(limit: 10, sort: { _id: 1 }) # => [ { "_id" => "5f9a9a9a9a9a9a9a9a9a9a9a", "name" => "John Doe" }, ... ]
47
+
48
+ # Insert a new user
49
+ # @param [Hash] document: The document to insert.
50
+ user = Users.insert({ name: 'Jane Doe' }) # => {"insertedId"=>"64cf1065f549de65c4908375"}
51
+
52
+ # Find a user by name
53
+ # @param [Hash] filter: The query predicate.
54
+ # @param [Hash] projection: The fields to return in the matching document.
55
+ # @param [Hash] sort: The order in which to return matching documents.
56
+ # @param [Integer] limit: The maximum number of documents to return.
57
+ user = Users.find(filter: { name: 'Jane Doe' }) # => [ { "_id" => "5f9a9a9a9a9a9a9a9a9a9a9a", "name" => "Jane Doe" } ]
58
+
59
+ # Update a user
60
+ # @param [Hash] where: The query predicate.
61
+ # @param [Hash] set: The fields to update in the matching document.
62
+ user = Users.update(where: { name: 'Jane Doe' }, set: { name: 'Jane Smith' }) # => {"matchedCount"=>1, "modifiedCount"=>1}
63
+
64
+ # Update All users
65
+ # @param [Hash] where: The query predicate.
66
+ # @param [Hash] set: The fields to update in the matching document.
67
+ user = Users.update_all(where: { name: 'Jane Smith' }, set: { name: 'Jane Doe' }) # => {"matchedCount"=>1, "modifiedCount"=>1}
68
+
69
+ # Destroy a user
70
+ # @param [Hash] where: The query predicate.
71
+ user = Users.destroy(where: { name: 'Jane Smith' }) # => {"deletedCount"=>1}
72
+
73
+ # Destroy All users
74
+ # @param [Hash] where: The query predicate.
75
+ user = Users.destroy_all(where: { name: 'Jane Smith' }) # => {"deletedCount"=>1}
76
+
77
+ # Count users
78
+ user = Users.count # => 10
79
+
80
+ # Others methods
81
+ Users.find_one(filter: {}, :projection: {})
82
+ Users.insert_one({})
83
+ Users.insert_many([{}, {}])
84
+ Users.update_one(filter: {}, :update: {}, upsert: false)
85
+ Users.update_many(filter: {}, :update: {}, upsert: false)
86
+ Users.delete_one(filter: {})
87
+ Users.delete_many(filter: {})
88
+ Users.replace_one(filter: {}, replacement: {}, upsert: false)
89
+ Users.aggregate(pipeline: [])
90
+ Users.count
32
91
  ```
33
92
 
34
93
  ## Development
@@ -90,11 +90,18 @@ module MongoApi
90
90
  count
91
91
  end
92
92
 
93
+ # This method sets the collection name
94
+ #
95
+ # @return [String] the collection name
96
+ def self.collection_name
97
+ ancestors.first.to_s.gsub("::", "_").downcase
98
+ end
99
+
93
100
  class << self
94
101
  def body
95
102
  {
96
103
  database: ENV["MONGO_DATABASE_NAME"],
97
- collection: ENV["MONGO_COLLECTION_NAME"],
104
+ collection: collection_name,
98
105
  dataSource: ENV["MONGO_DATASOURCE_NAME"]
99
106
  }
100
107
  end
@@ -175,6 +175,10 @@ module MongoApi
175
175
  class << self
176
176
  private
177
177
 
178
+ # This method is used to parse the response
179
+ #
180
+ # @param [Block] block
181
+ # @return [Hash|Array] the documents array or the response
178
182
  def parse_response(&block)
179
183
  data = block.call
180
184
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MongoApi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/mongo_api.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/mongo_api/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mongo_api"
7
+ spec.version = MongoApi::VERSION
8
+ spec.authors = ["Alef Ojeda de Oliveira"]
9
+ spec.email = ["alef.oliveira@dimensa.com.br"]
10
+
11
+ spec.summary = "MongoApi is a gem to help you to create a REST API using MongoDB Atlas Data API."
12
+ spec.description = "Use this gem to create a REST API using MongoDB Atlas Data API."
13
+ spec.homepage = "https://github.com/nemuba/mongo_api"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.2.0"
16
+
17
+ spec.metadata["homepage_uri"] = "https://github.com/nemuba/mongo_api"
18
+ spec.metadata["source_code_uri"] = "https://github.com/nemuba/mongo_api/tree/main"
19
+ spec.metadata["changelog_uri"] = "https://github.com/nemuba/mongo_api/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alef Ojeda de Oliveira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-06 00:00:00.000000000 Z
11
+ date: 2023-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use this gem to create a REST API using MongoDB Atlas Data API.
14
14
  email:
@@ -17,8 +17,6 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".byebug_history"
21
- - ".env"
22
20
  - ".rspec"
23
21
  - ".rubocop.yml"
24
22
  - CHANGELOG.md
@@ -31,6 +29,7 @@ files:
31
29
  - lib/mongo_api/base.rb
32
30
  - lib/mongo_api/request.rb
33
31
  - lib/mongo_api/version.rb
32
+ - mongo_api.gemspec
34
33
  - sig/mongo_api.rbs
35
34
  homepage: https://github.com/nemuba/mongo_api
36
35
  licenses:
@@ -54,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
53
  - !ruby/object:Gem::Version
55
54
  version: '0'
56
55
  requirements: []
57
- rubygems_version: 3.4.12
56
+ rubygems_version: 3.4.10
58
57
  signing_key:
59
58
  specification_version: 4
60
59
  summary: MongoApi is a gem to help you to create a REST API using MongoDB Atlas Data
data/.byebug_history DELETED
@@ -1,18 +0,0 @@
1
- c
2
- response
3
- c
4
- Users.all
5
- Users.body
6
- c
7
- Users.body
8
- c
9
- Users.all
10
- c
11
- Users.base_url
12
- .base_url
13
- Users
14
- c
15
- Users.base_url
16
- Users.all
17
- Users
18
- ENV['MONGO_DATABASE_NAME']
data/.env DELETED
@@ -1,5 +0,0 @@
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'