class String

Public Instance Methods

serve() click to toggle source
# File lib/string_serve.rb, line 6
def serve
  Rack::Handler::WEBrick.run(->(env) {
    [200, {'Content-Type' => content_type}, [self]]
  })
end

Private Instance Methods

content_type() click to toggle source
# File lib/string_serve.rb, line 14
def content_type
  case
  when valid_html?
    'text/html'
  when valid_json?
    'application/json'
  else
    'text/plain'
  end
end
valid_html?() click to toggle source
# File lib/string_serve.rb, line 29
def valid_html?
  W3CValidators::MarkupValidator.new
                                .validate_text(self)
                                .errors
                                .length == 0
end
valid_json?() click to toggle source
# File lib/string_serve.rb, line 25
def valid_json?
  !!JSON.parse(self) rescue false
end