Class: GridCLI::PrettyPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/gridcli/pprinter.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, format = nil) ⇒ PrettyPrinter

format can be one of :cmdcolor, :cmd, :textline, :json



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gridcli/pprinter.rb', line 100

def initialize(string, format=nil)
  @format = format.nil? ? :cmdcolor : format.to_sym
  @original = string
  begin
    j = JSON.parse(string)
    @type = j.keys.first
    @contents = j[@type]
    if @contents.has_key? 'created_at'
      @contents['created_at'] = Time.parse(@contents['created_at']).localtime.to_s
    else
      @contents['created_at'] = Time.now
    end
  rescue JSON::ParserError
    @type = nil
    @format = nil
    @contents = {}
  end
end

Instance Method Details

#formattersObject



119
120
121
# File 'lib/gridcli/pprinter.rb', line 119

def formatters
  { :cmdcolor => PPCmdColorFormat, :cmd => PPCmdFormat, :textline => PPTextLineFormat }
end

#to_sObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gridcli/pprinter.rb', line 123

def to_s
  formatter = formatters.fetch(@format, nil)
  # if the format is unknown or :json return original
  return @original if formatter.nil?
  
  case @type
  when "message" then formatter.message(@original, @contents)
  when "like" then formatter.like(@original, @contents)
  when "dislike" then formatter.dislike(@original, @contents)
  when "status" then formatter.status(@original, @contents)
  when "friend_request" then formatter.message(@original, @contents)
  when "user" then formatter.user(@original, @contents)
  else @original #unknown type
  end
end