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



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

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)
  else warn "Unknown selfcare command: #{subcommand}"
       exit 1
  end
end

Instance Method Details

#compose(args = []) ⇒ Object

rubocop:disable Metrics/MethodLength



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/holivia/commands/selfcare.rb', line 83

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



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

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 }
  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



35
36
37
# File 'lib/holivia/commands/selfcare.rb', line 35

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

#schema(_args = []) ⇒ Object



102
103
104
# File 'lib/holivia/commands/selfcare.rb', line 102

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

#show(args = []) ⇒ Object



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

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



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

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 }
  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