Sha256: ad5c03e7f814080c65106b14560a372d95bb3e2065cc374552c5d656e6df990c

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require "firestore"
require "tools/sync/helpers"

module Icarus
  module Mod
    module Tools
      module Sync
        # Sync methods
        class Tools
          include Helpers

          def initialize(client: nil)
            @firestore = client || Firestore.new
          end

          def tools
            @firestore.tools
          end

          def info_array
            @info_array ||= @firestore.toolinfo.map do |url|
              next unless url

              retrieve_from_url(url)[:tools].map { |tool| Icarus::Mod::Tools::Toolinfo.new(tool) if tool[:name] =~ /[a-z0-9]+/i }
            rescue Icarus::Mod::Tools::Sync::RequestFailed
              warn "Skipped; Failed to retrieve #{url}"
              next
            rescue JSON::ParserError => e
              warn "Skipped; Invalid JSON: #{e.full_message}"
              next
            end.flatten.compact
          end

          def find(toolinfo)
            @firestore.find_by_type(type: "tools", name: toolinfo.name, author: toolinfo.author)&.id
          end

          def find_info(toolinfo)
            @info_array.find { |tool| tool.name == toolinfo.name }
          end

          def update(toolinfo)
            @firestore.update(:tool, toolinfo, merge: false)
          end

          def delete(toolinfo)
            @firestore.delete(:tool, toolinfo)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
Icarus-Mod-Tools-2.1.3 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.1.2 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.1.1 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.1.0 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.0.4 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.0.3 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.0.1 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-2.0 lib/icarus/mod/tools/sync/tools.rb
Icarus-Mod-Tools-1.9.0 lib/icarus/mod/tools/sync/tools.rb