Git Product home page Git Product logo

rawg_wrapper's Introduction

Rawg Api wrapper

Api-wrapper activity for Avion School

Ruby version

  • Ruby 3.1.2

System dependencies

  • rest-client 1.8.0

Sample Usage

client.rb will be the one responsible on calling and passing http_method and endpoints to request.rb

module Rawg
  class Client
    def self.creator_roles
      response = Request.call('get', '/creator-roles')
    end
  end
end

controller.rb

module Api
  class CreatorRolesController < ApplicationController
    def index
      creator_roles = Rawg::Client.creator_roles
      render json: creator_roles
    end
  end
end

routes.rb

Rails.application.routes.draw do
  namespace :api do
    get '/creator-roles', to: 'creator_roles#index'
  end
end

request.rb will then send a request to the Third party API and upon receiving a response it will be parsed into JSON

module Rawg
  class Request
    require 'rest_client'

    TOKEN = Rails.application.config.token
    BASE_URL = "https://api.rawg.io/api"

    def self.call (http_method, endpoint)
      result = RestClient::Request.execute(
        method: http_method,
        url: "#{BASE_URL}#{endpoint}?key=#{TOKEN}",
        headers: {"Content-Type" => "application/json"}
      )
      {code: result.code, status: "Success", data: JSON.parse(result.body)}
    rescue RestClient::ExceptionWithResponse => error
      {code: error.http_code, status: error.message, data: Errors.map(error.http_code)}
    end
  end
end

if the response reach an error it will be handled by error.rb and return an error message depending on the error code otherwise it will return a reponse code 200, which is a success response.

module Rawg
  class Errors
    def self.map(code)
      case code 
      when 401
        return 'Unauthorized request. Please try again!'
      when 404
        return 'Invalid Request!'
      else
        return 'Service Unavailable. Please try again!'
      end
    end
  end
end

Sample success response

{
  "code": 200,
  "status": "Success",
  "data": {
    "count": 7,
    "next": null,
    "previous": null,
    "results": [
      {
        "id": 1,
        "name": "writer",
        "slug": "writer"
      }

License

MIT

rawg_wrapper's People

Watchers

Jmnahan avatar

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.