This repo contains code for generasi GIGIH Final Project in the Backend track. The problem for this project is to make an API for simple social media. Like other social media, users can make a post with hashtags and one attachment, also users can comment and many details for the API you can see in the API documentation.
To run this app you need to install some dependencies below:
- install Ruby, for developing this app I use ruby 2.7
- install sinatra
gem install sinatra
- install mysql2, in some OS mysql2 need build gem native extension and in linux we can install by
sudo apt-get install ruby2.7-dev
gem install mysql2
- import the database that I have provided. file
social_media_db.sql
- setting the environtment variable. please make sure the value of the environment variable matches your database environment.
export DB_HOST=localhost
export DB_USER=root
export DB_PASS=
export DB_NAME=social_media_db
- clone this repo and run this command to start the app
ruby web.rb
for testing I use rspec and simplecov. Please make sure you already installed it.
gem install rspec
gem install simplecov
to run the test you can run by command below. (actually i prefer to use format document in rspec)
rspec -f d
to test endpoint you can go to API documentation and import the url from there or you can import the collection from exported collection in postman collection
folder.
I create API documentation with postman API documentation tool. You can visit the link below to see the detail about API
In postman I use some global variable. Please make sure you provide it in your postman.
name | initial value | type |
---|---|---|
base_url | http://34.131.84.223:4567 (url GCP machine) | string |
user_id | 1 | int |
tag | tag | string |
post_id | 1 | int |
For the database I build schema like in the picture. I provide an imported sql with name social_media_db.sql
.
I write some notes for you easier to understand my erd and database schema.
- for field
created_time
I use the typeTIMESTAMP
. I think this type is easier for me if I want to group data by time. - field
attachment
stores the path where the attachment file is stored. For the name of the file, I use the name of tempfile. Just to make it easier for me, I don't create a function to generate a unique name for the attachment file. For now the extension that can be save for attachment file is.jpg
,.png
,.gif
,.mp4
and other extension outside picture and video format. - field
tag
contains the hashtag in lowercase.
- when i want to send many hashtags and attachment, i got just one hashtag. After surfing the internet, i found the strange way. Add
[]
in the end of parameter name (it's so funny ๐). this the references https://dev.to/jesusislord3/getting-arrays-in-the-params-hash-for-sinatra-4agh