Class: Icarus::Mod::Firestore
- Inherits:
-
Object
- Object
- Icarus::Mod::Firestore
- Defined in:
- lib/icarus/mod/firestore.rb
Overview
Helper methods for interacting with the Firestore API
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#collections ⇒ Object
readonly
Returns the value of attribute collections.
Instance Method Summary collapse
- #delete(type, payload) ⇒ Object
- #find_by_type(type:, name:, author:) ⇒ Object
-
#initialize ⇒ Firestore
constructor
A new instance of Firestore.
- #modinfo ⇒ Object
- #mods ⇒ Object
- #repositories ⇒ Object
- #toolinfo ⇒ Object
- #tools ⇒ Object
- #update(type, payload, merge: false) ⇒ Object
Constructor Details
#initialize ⇒ Firestore
Returns a new instance of Firestore.
13 14 15 16 17 18 19 20 21 |
# File 'lib/icarus/mod/firestore.rb', line 13 def initialize @client = Google::Cloud::Firestore.new(credentials: Config.firebase.credentials.to_h) @collections = Config.firebase.collections @repositories = repositories @modinfo = modinfo @toolinfo = toolinfo @mods = mods @tools = tools end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
11 12 13 |
# File 'lib/icarus/mod/firestore.rb', line 11 def client @client end |
#collections ⇒ Object (readonly)
Returns the value of attribute collections.
11 12 13 |
# File 'lib/icarus/mod/firestore.rb', line 11 def collections @collections end |
Instance Method Details
#delete(type, payload) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/icarus/mod/firestore.rb', line 65 def delete(type, payload) case type.to_sym when :mod, :tool response = @client.doc("#{collections.send(pluralize(type))}/#{payload.id}").delete else raise "Invalid type: #{type}" end response.is_a?(Google::Cloud::Firestore::CommitResponse::WriteResult) end |
#find_by_type(type:, name:, author:) ⇒ Object
43 44 45 |
# File 'lib/icarus/mod/firestore.rb', line 43 def find_by_type(type:, name:, author:) instance_variable_get("@#{type}").find { |obj| obj.name == name && obj. == } end |
#modinfo ⇒ Object
27 28 29 |
# File 'lib/icarus/mod/firestore.rb', line 27 def modinfo @modinfo ||= list(:modinfo) end |
#mods ⇒ Object
35 36 37 |
# File 'lib/icarus/mod/firestore.rb', line 35 def mods @mods ||= list(:mods) end |
#repositories ⇒ Object
23 24 25 |
# File 'lib/icarus/mod/firestore.rb', line 23 def repositories @repositories ||= list(:repositories) end |
#toolinfo ⇒ Object
31 32 33 |
# File 'lib/icarus/mod/firestore.rb', line 31 def toolinfo @toolinfo ||= list(:toolinfo) end |
#tools ⇒ Object
39 40 41 |
# File 'lib/icarus/mod/firestore.rb', line 39 def tools @tools ||= list(:tools) end |
#update(type, payload, merge: false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/icarus/mod/firestore.rb', line 47 def update(type, payload, merge: false) raise "You must specify a payload to update" if payload&.empty? || payload.nil? response = case type.to_sym when :modinfo, :toolinfo update_array = (send(type) + [payload]).flatten.uniq @client.doc(collections..send(type)).set({list: update_array}, merge:) if update_array.any? when :repositories @client.doc(collections..repositories).set({list: payload}, merge:) when :mod, :tool create_or_update(pluralize(type), payload, merge:) else raise "Invalid type: #{type}" end response.is_a?(Google::Cloud::Firestore::DocumentReference) || response.is_a?(Google::Cloud::Firestore::CommitResponse::WriteResult) end |