Class: Holivia::Commands::Selfcare

Inherits:
Base
  • Object
show all
Defined in:
lib/holivia/commands/selfcare.rb

Constant Summary collapse

BASE_PATH =
"/api/v1/backoffice/selfcare_contents"
COMPOSE_EXAMPLE_PATH =
Holivia.root.join("examples", "compose.json").to_s
IMAGE_MIME_TYPES =
{
  ".jpg" => "image/jpeg",
  ".jpeg" => "image/jpeg",
  ".png" => "image/png",
  ".webp" => "image/webp",
  ".gif" => "image/gif"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.route(args) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/holivia/commands/selfcare.rb', line 19

def self.route(args) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/AbcSize
  subcommand = args.shift
  case subcommand
  when "index"   then new.index(args)
  when "show"    then new.show(args)
  when "create"  then new.create(args)
  when "update"  then new.update(args)
  when "compose" then new.compose(args)
  when "schema"  then new.schema(args)
  when "format"  then Format.route(args)
  when "slide"   then Slide.route(args)
  when "item"    then Item.route(args)
  when "objective" then SelfcareObjective.route(args)
  else warn "Unknown selfcare command: #{subcommand}"
       exit 1
  end
end

Instance Method Details

#compose(args = []) ⇒ Object

rubocop:disable Metrics/MethodLength



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/holivia/commands/selfcare.rb', line 87

def compose(args = []) # rubocop:disable Metrics/MethodLength
  show_example = false
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: holivia selfcare compose --file <path>"
    opts.on("--file FILE") { |v| options = JSON.parse(File.read(v), symbolize_names: true) }
    opts.on("--example", "Print a valid compose JSON template") { show_example = true }
  end.parse!(args)

  if show_example
    puts File.read(COMPOSE_EXAMPLE_PATH)
    return
  end

  options = options.merge(piped_json)
  abort "Usage: holivia selfcare compose --file <path>" if options.empty?
  output(client.post("#{BASE_PATH}/compose", body: options))
end

#create(args = []) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/holivia/commands/selfcare.rb', line 48

def create(args = []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: holivia selfcare create [options]"
    opts.on("--title TITLE") { |v| options[:title] = v }
    opts.on("--locale LOCALE") { |v| options[:locale] = v }
    opts.on("--content-type TYPE") { |v| options[:content_type] = v }
    opts.on("--duration DURATION", Integer) { |v| options[:duration] = v }
    opts.on("--description DESC") { |v| options[:description] = v }
    opts.on("--image FILE") { |v| options[:image] = v }
    opts.on("--state STATE") { |v| options[:state] = v }
  end.parse!(args)
  options = options.merge(piped_json)

  abort "No options provided. Use --help for usage." if options.empty?
  output(client.post(BASE_PATH, body: build_body(options)))
end

#index(_args = []) ⇒ Object



37
38
39
# File 'lib/holivia/commands/selfcare.rb', line 37

def index(_args = [])
  output(client.get(BASE_PATH))
end

#schema(_args = []) ⇒ Object



106
107
108
# File 'lib/holivia/commands/selfcare.rb', line 106

def schema(_args = [])
  output(client.get("#{BASE_PATH}/schema"))
end

#show(args = []) ⇒ Object



41
42
43
44
45
46
# File 'lib/holivia/commands/selfcare.rb', line 41

def show(args = [])
  id = args.shift
  abort "Usage: holivia selfcare show <id>" unless id

  output(client.get("#{BASE_PATH}/#{id}"))
end

#update(args = []) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/holivia/commands/selfcare.rb', line 66

def update(args = []) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  id = args.shift
  abort "Usage: holivia selfcare update <id> [options]" unless id

  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: holivia selfcare update <id> [options]"
    opts.on("--title TITLE") { |v| options[:title] = v }
    opts.on("--locale LOCALE") { |v| options[:locale] = v }
    opts.on("--content-type TYPE") { |v| options[:content_type] = v }
    opts.on("--duration DURATION", Integer) { |v| options[:duration] = v }
    opts.on("--description DESC") { |v| options[:description] = v }
    opts.on("--image FILE") { |v| options[:image] = v }
    opts.on("--state STATE") { |v| options[:state] = v }
  end.parse!(args)
  options = options.merge(piped_json)

  abort "No options provided. Use --help for usage." if options.empty?
  output(client.patch("#{BASE_PATH}/#{id}", body: build_body(options)))
end