Giter VIP home page Giter VIP logo

blog_app's Introduction

Ruby Rails

Blog App

Interactive API with database on which you can add users, posts, comments, and likes with the related associations

Built With

  • Major languages: ruby, css, ruby on rails

Live Demo

comming soon

Getting Started

Prerequisites

  • ruby version (3.0) or above
  • Rails 7.0 or above
  • Postgresql db

Setup & Installation

Usage

  • on your console run (rails server command)
  • on the browser navigate to ('http://localhost:3000/')
  • Enjoy ...........

API endpoints

This API uses Devise-JWT for authentication, hence other than the signup and login requests each request should have a token.

  • After running the rails server, you can reach the below API end-points from the browser's console window (The same window where you see the localhost)

  • To signup a new user, a POST request should be sent to the API including at least the required three attributes (name, email and a password). As a result a new user will be created and a new token will be returned, use the below example: fetch("http://127.0.0.1:3001/signup", { method: "post", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user: { name: "test2", email: "[email protected]", password: "password", }, }), }) .then((res) => { if (res.ok) { console.log(res.headers.get("Authorization")); localStorage.setItem("token", res.headers.get("Authorization")); return res.json(); } else { throw new Error(res); } }) .then((json) => console.dir(json)) .catch((err) => console.error(err));

    this command will generate a token and store it in the localStorage

  • if you need to login as an existing user, use the below command with the correct email and password: fetch("http://127.0.0.1:3001/login", { method: "post", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ user: { email: "[email protected]", password: "password", }, }), }) .then((res) => { if (res.ok) { console.log(res.headers.get("Authorization")); localStorage.setItem("token", res.headers.get("Authorization")); return res.json(); } else { throw new Error(res); } }) .then((json) => console.dir(json)) .catch((err) => console.error(err));

    This command will store the token in the localStorage as well

  • To create a new post, you need to pass your user id with the post's title and text, as in the below example: fetch("http://localhost:3001/users/3/posts.json", { method: "post", headers: { "Content-Type": "application/json", Authorization: localStorage.getItem("token"), }, body: JSON.stringify({ post: { title: "json_3rd", text: "json third post", }, }), });

This command should create a new post belongs to the loged user

  • To list all posts for a given user, run the below command: fetch("http://localhost:3001/users/1/posts.json", { headers: { "Content-Type": "application/json", Authorization: localStorage.getItem("token"), }, }) .then((res) => { if (res.ok) { return res.json(); } else if (res.status == "401") { throw new Error("Unauthorized Request. Must be signed in."); } }) .then((json) => console.dir(json)) .catch((err) => console.error(err));

    This command returns an array for the posts list

  • To add a comment on a given post (id = 1) by the logged user, use the below command

fetch("http://localhost:3001/users/1/posts/1/comments", { method: "post", headers: { "Content-Type": "application/json", Authorization: localStorage.getItem("token"), }, body: JSON.stringify({ comment: { text: "json first comment", }, }), });

  • To List all comments on a given post (e.g. post_id =1) use the below command

fetch("http://localhost:3001/users/1/posts/1/comments.json", { headers: { "Content-Type": "application/json", Authorization: localStorage.getItem("token"), }, }) .then((res) => { if (res.ok) { return res.json(); } else if (res.status == "401") { throw new Error("Unauthorized Request. Must be signed in."); } }) .then((json) => console.dir(json)) .catch((err) => console.error(err));

  • Finaly to logout, use the below command: user logout fetch("http://127.0.0.1:3001/logout", { method: "delete", headers: { "Content-Type": "application/json", Authorization: localStorage.getItem("token"), }, });

Run tests

  • on your console run (rspec) command

Author

๐Ÿ‘ค Ja'fer Yousef

๐Ÿค Contributing

Contributions, issues, and feature requests are welcome!

Feel free to check the issues page.

Show your support

Give a โญ๏ธ if you like this project!

Acknowledgments

  • Microverse team

๐Ÿ“ License

This project is MIT licensed.

blog_app's People

Contributors

jaferidrees avatar raminka13 avatar

Stargazers

Cindy Dorantes avatar

Watchers

 avatar

blog_app's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.