kagaribi 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7009cb11cae6982c8214c7de69349b851ac8b7475959801b48ebb309762a5e6
4
+ data.tar.gz: 77887aa0a0f38a9cc1a83177505e78ec3bd4d20a6cf2f37eddae03ddb4f17539
5
+ SHA512:
6
+ metadata.gz: f3849fcf52d60d06a23105f2318547bd9a14c49edaa9a9df6cdc28cd08ab1b91b78a533349c68935204a632e86b4b569600209f9cbfb14796f67df0e75fb351d
7
+ data.tar.gz: 26c6ee337021f3c5d68a9d622db22cbc3c54e4052eb4e75e4af36d80c616b3f6af5c095cbc87e23ae8d6c8198dbd21171ade05196baec2bc9bcb850ef19c6f1b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --markup markdown
2
+ --no-private
3
+ --hide-void-return
4
+ -
5
+ CHANGELOG.md
6
+ LICENSE.txt
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+ [full changelog](http://github.com/sue445/kagaribi/compare/v0.1.0...main)
3
+
4
+ ## [0.1.0] - 2024-05-11
5
+
6
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 sue445
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,70 @@
1
+ # Kagaribi(篝火) :fire:
2
+ Simple client for [Cloud Firestore](https://cloud.google.com/firestore)
3
+
4
+ [![test](https://github.com/sue445/kagaribi/actions/workflows/test.yml/badge.svg)](https://github.com/sue445/kagaribi/actions/workflows/test.yml)
5
+
6
+ ## Installation
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ $ bundle add kagaribi
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ $ gem install kagaribi
17
+ ```
18
+
19
+ ## Usage
20
+ ### Authentication
21
+ Pass environment variables for Firestore authentication
22
+
23
+ see https://cloud.google.com/ruby/docs/reference/google-cloud-firestore/latest/AUTHENTICATION
24
+
25
+ ### Simple usage
26
+ ```ruby
27
+ require "kagaribi"
28
+
29
+ collection = Kagaribi.collection("users")
30
+
31
+ collection.set("sue445", name: "sue445", url: "https://github.com/sue445")
32
+
33
+ collection.get("sue445")
34
+ #=> { name: "sue445", url: "https://github.com/sue445" }
35
+ ```
36
+
37
+ All methods are followings
38
+
39
+ https://sue445.github.io/kagaribi/Kagaribi/Collection
40
+
41
+ ## Development
42
+ At first, install [Firebase Local Emulator Suite](https://firebase.google.com/docs/emulator-suite/install_and_configure)
43
+
44
+ Mac
45
+
46
+ ```bash
47
+ brew install firebase-cli
48
+ ```
49
+
50
+ Unix
51
+
52
+ ```bash
53
+ sudo wget https://firebase.tools/bin/linux/latest -O /usr/local/bin/firebase --quiet
54
+ sudo chmod 755 /usr/local/bin/firebase
55
+ firebase setup:emulators:firestore
56
+ ```
57
+
58
+ Run tests with Firebase Local Emulator
59
+
60
+ ```bash
61
+ bundle exec rake spec
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sue445/kagaribi.
67
+
68
+ ## License
69
+
70
+ 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,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ desc "Run spec"
6
+ task :spec do
7
+ sh "firebase --project test emulators:exec --only firestore 'rspec'"
8
+ end
9
+
10
+ desc "Check rbs"
11
+ task :rbs do
12
+ sh "rbs validate"
13
+ sh "steep check"
14
+ end
15
+
16
+ desc "Run all"
17
+ task all: %i(spec rbs)
18
+
19
+ task default: :all
data/Steepfile ADDED
@@ -0,0 +1,31 @@
1
+ # D = Steep::Diagnostic
2
+
3
+ target :lib do
4
+ signature "sig"
5
+
6
+ check "lib" # Directory name
7
+ # check "Gemfile" # File name
8
+ # check "app/models/**/*.rb" # Glob
9
+ # ignore "lib/templates/*.rb"
10
+
11
+ # library "pathname" # Standard libraries
12
+ # library "strong_json" # Gems
13
+
14
+ collection_config "rbs_collection.yaml"
15
+
16
+ # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
17
+ # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
18
+ # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
19
+ # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
20
+ # configure_code_diagnostics do |hash| # You can setup everything yourself
21
+ # hash[D::Ruby::NoMethod] = :information
22
+ # end
23
+ end
24
+
25
+ # target :test do
26
+ # signature "sig", "sig-private"
27
+ #
28
+ # check "test"
29
+ #
30
+ # # library "pathname" # Standard libraries
31
+ # end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kagaribi
4
+ class Collection
5
+ MAX_RETRY_COUNT = 5
6
+
7
+ # @!attribute [r] collection_name
8
+ # @return [String]
9
+ attr_reader :collection_name
10
+
11
+ # @!attribute [r] database_id
12
+ # @return [String,nil]
13
+ attr_reader :database_id
14
+
15
+ # @!attribute [r] logger
16
+ # @return [Logger]
17
+ attr_reader :logger
18
+
19
+ # @param collection_name [String]
20
+ # @param database_id [String,nil] Identifier for a Firestore database. If not present, the default database of the project is used.
21
+ # @param logger [Logger] default is `STDOUT` Logger
22
+ def initialize(collection_name, database_id: nil, logger: nil)
23
+ @collection_name = collection_name
24
+ @database_id = database_id
25
+
26
+ @logger =
27
+ if logger
28
+ logger
29
+ else
30
+ Logger.new($stdout)
31
+ end
32
+ end
33
+
34
+ # Save a document to collection
35
+ # @note If a document with the same key exists, it is overwritten.
36
+ # @param doc_key [String]
37
+ # @param data [Hash]
38
+ def set(doc_key, data)
39
+ ref = firestore.doc(full_doc_key(doc_key))
40
+ ref.set(data)
41
+ end
42
+
43
+ # Update a document that has already been saved
44
+ # @note If a document with the same key exists, it is merged.
45
+ # @param doc_key [String]
46
+ # @param data [Hash]
47
+ def update(doc_key, data)
48
+ ref = firestore.doc(full_doc_key(doc_key))
49
+ ref.update(data)
50
+ end
51
+
52
+ # Get document from collection
53
+ # @param doc_key [String]
54
+ # @return [Hash<Symbol,Object>] return empty Hash if document isn't found
55
+ def get(doc_key)
56
+ with_retry("Kagaribi::Collection#get") do
57
+ ref = firestore.doc(full_doc_key(doc_key))
58
+ snap = ref.get
59
+ snap&.data || {}
60
+ end
61
+ end
62
+
63
+ # Whether document is exists in collection
64
+ # @param doc_key [String]
65
+ # @return [Boolean]
66
+ def exists?(doc_key)
67
+ with_retry("Kagaribi::Collection#exists?") do
68
+ ref = firestore.doc(full_doc_key(doc_key))
69
+ snap = ref.get
70
+ snap&.exists?
71
+ end
72
+ end
73
+
74
+ # Delete document in collection
75
+ # @param doc_key [String]
76
+ def delete(doc_key)
77
+ ref = firestore.doc(full_doc_key(doc_key))
78
+ ref.delete
79
+ end
80
+
81
+ # @param key [String]
82
+ # @return [String]
83
+ def self.sanitize_key(key)
84
+ key.tr("/", "-")
85
+ end
86
+
87
+ private
88
+
89
+ # @return [Google::Cloud::Firestore]
90
+ def firestore
91
+ @firestore ||= Google::Cloud::Firestore.new(database_id: database_id)
92
+ end
93
+
94
+ # @return [String]
95
+ def sanitized_collection_name
96
+ sanitize_key(@collection_name)
97
+ end
98
+
99
+ # @param key [String]
100
+ # @return [String]
101
+ def sanitize_key(key)
102
+ Collection.sanitize_key(key)
103
+ end
104
+
105
+ # @param doc_key [String]
106
+ # @return [String]
107
+ def full_doc_key(doc_key)
108
+ "#{sanitized_collection_name}/#{sanitize_key(doc_key)}"
109
+ end
110
+
111
+ # @param label [String]
112
+ # @yield
113
+ def with_retry(label)
114
+ yield
115
+ rescue TypeError, GRPC::Unavailable, RuntimeError, Signet::AuthorizationError => error
116
+ raise error unless retryable_error?(error)
117
+
118
+ retry_count ||= 0
119
+ retry_count += 1
120
+
121
+ raise error if retry_count > MAX_RETRY_COUNT
122
+
123
+ logger.warn "[#{label}] collection_name=#{@collection_name}, retry_count=#{retry_count}, error=#{error}"
124
+ sleep 1
125
+ retry
126
+ end
127
+
128
+ # @param error [StandardError]
129
+ # @return [Boolean]
130
+ def retryable_error?(error)
131
+ case error
132
+ when RuntimeError
133
+ # e.g.
134
+ # Could not load the default credentials. Browse to
135
+ # https://developers.google.com/accounts/docs/application-default-credentials
136
+ # for more information
137
+ return true if error.message.include?("Could not load the default credentials.")
138
+
139
+ # e.g.
140
+ # Your credentials were not found. To set up Application Default
141
+ # Credentials for your environment, see
142
+ # https://cloud.google.com/docs/authentication/external/set-up-adc
143
+ return true if error.message.include?("Your credentials were not found.")
144
+
145
+ return false
146
+ end
147
+
148
+ true
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kagaribi
4
+ VERSION = "0.1.0"
5
+ end
data/lib/kagaribi.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "google/cloud/firestore"
4
+ require "logger"
5
+
6
+ require_relative "kagaribi/version"
7
+
8
+ module Kagaribi
9
+ class Error < StandardError; end
10
+
11
+ autoload :Collection, "kagaribi/collection"
12
+
13
+ # @param collection_name [String]
14
+ # @param database_id [String,nil] Identifier for a Firestore database. If not present, the default database of the project is used.
15
+ # @param logger [Logger] default is `STDOUT` Logger
16
+ # @return [Kagaribi::Collection]
17
+ def self.collection(collection_name, database_id: nil, logger: nil)
18
+ Collection.new(collection_name, database_id: database_id, logger: logger)
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ ---
2
+ path: ".gem_rbs_collection"
3
+ gems:
4
+ - name: diff-lcs
5
+ version: '1.5'
6
+ source:
7
+ type: git
8
+ name: ruby/gem_rbs_collection
9
+ revision: 7a105f52053ce1c708b605dfa9c1ab8473424036
10
+ remote: https://github.com/ruby/gem_rbs_collection.git
11
+ repo_dir: gems
12
+ - name: fileutils
13
+ version: '0'
14
+ source:
15
+ type: stdlib
16
+ - name: logger
17
+ version: '0'
18
+ source:
19
+ type: stdlib
20
+ - name: monitor
21
+ version: '0'
22
+ source:
23
+ type: stdlib
24
+ - name: rake
25
+ version: '13.0'
26
+ source:
27
+ type: git
28
+ name: ruby/gem_rbs_collection
29
+ revision: 7a105f52053ce1c708b605dfa9c1ab8473424036
30
+ remote: https://github.com/ruby/gem_rbs_collection.git
31
+ repo_dir: gems
32
+ gemfile_lock_path: Gemfile.lock
@@ -0,0 +1,26 @@
1
+ # Download sources
2
+ sources:
3
+ - type: git
4
+ name: ruby/gem_rbs_collection
5
+ remote: https://github.com/ruby/gem_rbs_collection.git
6
+ revision: main
7
+ repo_dir: gems
8
+
9
+ # You can specify local directories as sources also.
10
+ # - type: local
11
+ # path: path/to/your/local/repository
12
+
13
+ # A directory to install the downloaded RBSs
14
+ path: .gem_rbs_collection
15
+
16
+ gems:
17
+ - name: rbs
18
+ ignore: true
19
+ - name: steep
20
+ ignore: true
21
+ - name: yard
22
+ ignore: true
23
+ - name: kagaribi
24
+ ignore: true
25
+
26
+ - name: logger
@@ -0,0 +1,30 @@
1
+ # TypeProf 0.21.9
2
+
3
+ # Classes
4
+ module Kagaribi
5
+ class Collection
6
+ MAX_RETRY_COUNT: Integer
7
+
8
+ @firestore: Google::Cloud::Firestore
9
+
10
+ attr_reader collection_name: String
11
+ attr_reader database_id: String
12
+ attr_reader logger: Logger
13
+
14
+ def initialize: (String collection_name, ?database_id: string?, ?logger: Logger?) -> void
15
+ def set: (String doc_key, Hash[untyped, untyped] data) -> void
16
+ def get: (String doc_key) -> Hash[Symbol, untyped]
17
+ def exists?: (String doc_key) -> bool
18
+ def delete: (String doc_key) -> void
19
+ def self.sanitize_key: (String key) -> String
20
+
21
+ private
22
+
23
+ def firestore: -> Google::Cloud::Firestore
24
+ def sanitized_collection_name: -> String
25
+ def sanitize_key: (String key) -> String
26
+ def full_doc_key: (String doc_key) -> String
27
+ def with_retry: (String label) { -> untyped } -> untyped
28
+ def retryable_error?: (StandardError error) -> bool
29
+ end
30
+ end
data/sig/kagaribi.rbs ADDED
@@ -0,0 +1,10 @@
1
+ module Kagaribi
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ def self.collection: (String collection_name, ?database_id: string?, ?logger: Logger?) -> Collection
8
+
9
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
10
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kagaribi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sue445
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-firestore
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: steep
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple client for Cloud Firestore
84
+ email:
85
+ - sue445@sue445.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".rspec"
91
+ - ".yardopts"
92
+ - CHANGELOG.md
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - Steepfile
97
+ - lib/kagaribi.rb
98
+ - lib/kagaribi/collection.rb
99
+ - lib/kagaribi/version.rb
100
+ - rbs_collection.lock.yaml
101
+ - rbs_collection.yaml
102
+ - sig/kagaribi.rbs
103
+ - sig/kagaribi/collection.rbs
104
+ homepage: https://github.com/sue445/kagaribi
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://github.com/sue445/kagaribi
109
+ source_code_uri: https://github.com/sue445/kagaribi
110
+ changelog_uri: https://github.com/sue445/kagaribi/blob/main/CHANGELOG.md
111
+ documentation_uri: https://sue445.github.io/kagaribi/
112
+ rubygems_mfa_required: 'true'
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 3.0.0
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubygems_version: 3.5.9
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Simple client for Cloud Firestore
132
+ test_files: []