2021-02-21

Send me your comments/requests from VIM

Here's another fantastic VIM plugin that let's you send an HTTP request to a REST web API.

You can send me your comments from VIM, as follows:

- Make sure you have the VRC plugin installed then open Vim and set the buffer filetype to rest:

   :set ft=rest

- Copy/paste and edit the following snippet:

   https://tessarinseve.pythonanywhere.com
   post /feedback
   { 
       "name":"your name",
       "email": "your@email.com",
       "phone" :"",
       "url" :"",
       "message" :"your website is awesome."

   }

- Press CTRL-j

Below the server-side code for Bottle (python 2.7) micro-framework.

    @post('/feedback')
    def feedback_from_users():
        import re
        import os
        email   = request.json.get('email')
        name    = request.json.get('name')
        url     = request.json.get('url')
        message = request.json.get('message')
        phone   = request.json.get('phone')
        
        # check if larger than 254
        if len(email)>254:
            response = "Not a valid email"
            return {"msg":response}
        
        # check overflow
        size= os.path.getsize(USERDIR+"feedback_from_vim.txt")
        
        # Limited to around 40 comments
        if size>504:
            response = "Overflow Error"
            return {"msg":response}
        
        # validate email
        valid= re.search(r'[\w.-]+@[\w.-]+.\w+', email)
        if valid:
            with open(USERDIR+"feedback_from_vim.txt", "a") as myfile:
                myfile.write(";".join([name,email,phone,url,message])+" \n")
            response = "Thank you"
        else:
            response = "Not a valid email"
   
        return {"msg":response}