Class: ClassicApi::Util::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/classic_api/util/format.rb

Constant Summary collapse

COLOR_BOLD =
"\e[1m"
COLOR_RED =
"\e[31m"
COLOR_RED_BG =
"\e[41m"
COLOR_WHITE =
"\e[97m"
COLOR_GREY =
"\e[38;5;245m"
COLOR_GREY_BG =
"\e[48;5;255m"
COLOR_END =
"\e[0m"
COLOR_BLUE =
"\e[34m"
COLOR_BLUE_BG =
"\e[44m"
COLOR_GREEN =
"\e[32m"
COLOR_GREEN_BG =
"\e[42m"
COLOR_MAGENTA =
"\e[35m"
COLOR_MAGENTA_BG =
"\e[45m"
ARROW =
" \e[37m⇢\e[0m "
QUOTE =
"\e[38;5;15m\"\e[0m"

Class Method Summary collapse

Class Method Details

.ask_input(label, default = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/classic_api/util/format.rb', line 80

def ask_input(label, default=nil)
  if default.nil?
    default_text = COLOR_WHITE+default+COLOR_END
  else
    default_text = COLOR_GREEN+default+COLOR_END
  end
  result = Thor::LineEditor.readline("#{ARROW}#{COLOR_BOLD}#{label}#{COLOR_END} #{default_text}: ")
  result.strip!
  if default && result.blank?
    default
  else
    result
  end
end

.format_value(value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/classic_api/util/format.rb', line 107

def format_value(value)
  if value.is_a?(Fixnum) or value.is_a?(Float)
    COLOR_BLUE+value.to_s+COLOR_END
  elsif value.is_a?(String)
    QUOTE+value+QUOTE
  elsif value.is_a?(DateTime)
    COLOR_GREEN+value.strftime("%d.%m.%Y %H:%M")+COLOR_END
  else
    value
  end
end

.present(entity, indent = 0) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/classic_api/util/format.rb', line 49

def present(entity, indent=0)
  padding = " " * indent * 2
  if entity == true
    puts "#{padding}#{ARROW+COLOR_GREEN+COLOR_BOLD}success#{COLOR_END}"
  elsif entity == false
    puts "#{padding}#{ARROW+COLOR_RED+COLOR_BOLD}error#{COLOR_END}"
  elsif entity.is_a?(JsonApiClient::ResultSet)
    entity.each do |e|
      present(e, indent)
    end
  else
    puts "#{padding}#{(indent.zero? ? COLOR_BLUE_BG : COLOR_MAGENTA_BG)+COLOR_WHITE+COLOR_BOLD} #{entity.class.resource_name.upcase}##{entity.id} #{COLOR_END}"
    entity.attributes.each do |key, value|
      puts "#{padding}#{ARROW+COLOR_MAGENTA+COLOR_BOLD}#{key}:#{COLOR_END} #{format_value(value)}"
    end
  end
end


67
68
69
70
71
72
73
# File 'lib/classic_api/util/format.rb', line 67

def print_error(title, message=nil)
  if message.nil?
    puts "#{COLOR_RED_BG+COLOR_BOLD+COLOR_WHITE} ERROR #{COLOR_END} #{COLOR_BOLD}#{title}#{COLOR_END}"
  else
    puts "#{COLOR_RED_BG+COLOR_BOLD+COLOR_WHITE} ERROR #{COLOR_END} #{COLOR_BOLD}#{title}: #{COLOR_RED}#{message}#{COLOR_END}"
  end
end


95
96
97
# File 'lib/classic_api/util/format.rb', line 95

def print_header(title, color=COLOR_BLUE_BG)
  puts "#{color+COLOR_BOLD+COLOR_WHITE} #{title} #{COLOR_END}"
end


99
100
101
# File 'lib/classic_api/util/format.rb', line 99

def print_info(title, message)
  puts "#{COLOR_BLUE_BG+COLOR_BOLD+COLOR_WHITE} #{title} #{COLOR_END} #{COLOR_BLUE+COLOR_BOLD}#{message}#{COLOR_END}"
end


75
76
77
# File 'lib/classic_api/util/format.rb', line 75

def print_key_value(key, value)
  puts "#{ARROW+COLOR_MAGENTA+COLOR_BOLD}#{key}:#{COLOR_END} #{value}"
end


103
104
105
# File 'lib/classic_api/util/format.rb', line 103

def print_success(message)
  puts "#{COLOR_GREEN_BG+COLOR_BOLD+COLOR_WHITE} SUCCESS #{COLOR_END} #{COLOR_GREEN+COLOR_BOLD}#{message}#{COLOR_END}"
end

.show_error(e) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/classic_api/util/format.rb', line 21

def show_error(e)
  case e
  when Errno::EPIPE
    nil
  when Thor::Error
    print_error(e.message)
  when ClassicApi::APIEntityException
    print_error("Could not save #{e.entity.class.resource_name}:")
    e.errors.messages.each do |key, value|
      value.each do |v|
        print_key_value(key, v)
      end
    end
  when JsonApiClient::Errors::ConnectionError
    print_error("Connection Error", "Could not connect to '#{e.env.url}'")
  when JsonApiClient::Errors::NotFound
    print_error("Not Found", e.message)
  when JsonApiClient::Errors::ServerError
    print_error(e.env.status.to_s, e.env.body.to_json)
  when JsonApiClient::Errors::AccessDenied, JsonApiClient::Errors::NotAuthorized
    print_error(e.env.reason_phrase, e.env.body["error"])
  when RuntimeError, ArgumentError
    print_error(e.class.name, e.message)
  else
    raise e
  end
end