Sha256: 2c2236a893ab8c76fcf6b93e33a878ee5aa390ae41f67974232497868c566060

Contents?: true

Size: 1.02 KB

Versions: 17

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

# A REST API
class RestController < ApplicationController
  # GET rest
  def index
    result = params[:order] == 'desc' ? posts.reverse : posts
    render status: :ok, json: result
  end

  # GET rest/:id
  def show
    render status: :ok, json: find_post(id: id)
  end

  # POST rest
  def create
    render status: :created,
           json: create_post(title: params[:title])
  end

  # POST/PUT/PATCH rest/:id
  def update
    render status: :ok,
           json: update_post(id: id, title: params[:title])
  end

  # DELETE rest/:id
  def delete
    render status: :no_content
  end

  private

  def id
    params[:id].to_i
  end

  def posts
    [
      { id: 1, title: 'Title 1' },
      { id: 2, title: 'Title 2' },
      { id: 3, title: 'Title 3' },
    ]
  end

  def find_post(id:)
    posts.find { |p| p[:id] == id }
  end

  def create_post(title:)
    { id: 4, title: title }
  end

  def update_post(id:, title:)
    find_post(id: id).tap do |post|
      post[:title] = title
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
my_api_client-1.1.0 my_api/app/controllers/rest_controller.rb
my_api_client-1.0.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.27.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.26.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.25.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.24.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.23.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.22.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.21.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.20.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.19.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.18.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.17.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.16.1 my_api/app/controllers/rest_controller.rb
my_api_client-0.16.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.15.0 my_api/app/controllers/rest_controller.rb
my_api_client-0.14.0 my_api/app/controllers/rest_controller.rb