Class: Icarus::Mod::Firestore

Inherits:
Object
  • Object
show all
Defined in:
lib/icarus/mod/firestore.rb

Overview

Helper methods for interacting with the Firestore API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFirestore

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

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/icarus/mod/firestore.rb', line 11

def client
  @client
end

#collectionsObject (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.author == author }
end

#modinfoObject



27
28
29
# File 'lib/icarus/mod/firestore.rb', line 27

def modinfo
  @modinfo ||= list(:modinfo)
end

#modsObject



35
36
37
# File 'lib/icarus/mod/firestore.rb', line 35

def mods
  @mods ||= list(:mods)
end

#repositoriesObject



23
24
25
# File 'lib/icarus/mod/firestore.rb', line 23

def repositories
  @repositories ||= list(:repositories)
end

#toolinfoObject



31
32
33
# File 'lib/icarus/mod/firestore.rb', line 31

def toolinfo
  @toolinfo ||= list(:toolinfo)
end

#toolsObject



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.meta.send(type)).set({list: update_array}, merge:) if update_array.any?
  when :repositories
    @client.doc(collections.meta.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