Class: TridentAssistant::CLI::Collection

Inherits:
Base
  • Object
show all
Defined in:
lib/trident_assistant/cli/collection.rb

Overview

CLI tool of collection

Defined Under Namespace

Classes: InvalidError

Constant Summary

Constants inherited from Base

Base::UI

Instance Attribute Summary

Attributes inherited from Base

#api, #bot, #client, #keystore

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from TridentAssistant::CLI::Base

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trident_assistant/cli/collection.rb', line 24

def create
  name = options[:name] || UI.ask("Please input collection name")
  raise InvalidError, "Name cannot be blank" if name.blank?

  description = options[:description] || UI.ask("Please input collection description")
  raise InvalidError, "Description cannot be blank" if description.blank?

  icon = options[:icon] || UI.ask("Please input icon file or icon url")
  icon_file = File.open(icon) if File.exist? icon

  external_url = options[:url] || UI.ask("Please input collection external url, start with https:// or http://")
  split = options[:split] || UI.ask("Please input collection split", default: "0.0")
  payload = {
    name: name,
    description: description,
    external_url: external_url,
    split: split
  }

  if icon_file.present?
    payload[:icon_base64] = Base64.strict_encode64(icon_file.read)
  else
    payload[:icon_url] = icon
  end

  log api.create_collection(**payload)
rescue InvalidError => e
  log UI.fmt("{{x}} failed: #{e.inspect}")
ensure
  icon_file&.close
end

#indexObject



13
14
15
# File 'lib/trident_assistant/cli/collection.rb', line 13

def index
  log api.collections
end

#show(id) ⇒ Object



76
77
78
# File 'lib/trident_assistant/cli/collection.rb', line 76

def show(id)
  log api.collection id
end

#update(id) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/trident_assistant/cli/collection.rb', line 61

def update(id)
  payload = {}
  payload[:description] = options[:description] if options[:description].present?
  payload[:external_url] = options[:url] if options[:url].present?
  if options[:icon].present? && File.exist?(options[:icon])
    icon = File.open options[:icon]
    payload[:icon_base64] = Base64.strict_encode64(icon.read)
  end
  log api.update_collection(id, **payload)
ensure
  icon&.close
end