{"id":21297751,"url":"https://github.com/ninech/rest-in-peace","last_synced_at":"2025-07-11T18:32:35.589Z","repository":{"id":18609252,"uuid":"21814515","full_name":"ninech/REST-in-Peace","owner":"ninech","description":"A KISS REST Client.","archived":false,"fork":false,"pushed_at":"2022-05-25T05:46:15.000Z","size":148,"stargazers_count":12,"open_issues_count":1,"forks_count":4,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-07-04T12:15:44.272Z","etag":null,"topics":["activemodel","rest","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ninech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-14T09:38:14.000Z","updated_at":"2025-05-24T07:42:57.000Z","dependencies_parsed_at":"2022-09-09T15:51:15.218Z","dependency_job_id":null,"html_url":"https://github.com/ninech/REST-in-Peace","commit_stats":null,"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/ninech/REST-in-Peace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ninech%2FREST-in-Peace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ninech%2FREST-in-Peace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ninech%2FREST-in-Peace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ninech%2FREST-in-Peace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ninech","download_url":"https://codeload.github.com/ninech/REST-in-Peace/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ninech%2FREST-in-Peace/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264870629,"owners_count":23676281,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activemodel","rest","ruby"],"created_at":"2024-11-21T14:40:53.235Z","updated_at":"2025-07-11T18:32:35.149Z","avatar_url":"https://github.com/ninech.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# REST in Peace [![Build Status](https://travis-ci.org/ninech/REST-in-Peace.svg)](https://travis-ci.org/ninech/REST-in-Peace) [![Code Climate](https://codeclimate.com/github/ninech/REST-in-Peace.png)](https://codeclimate.com/github/ninech/REST-in-Peace)\n\nA ruby REST client that lets you feel like in heaven when consuming APIs.\n\n![logo](https://raw.githubusercontent.com/ninech/REST-in-Peace/master/images/rest_in_peace.gif)\n\n## Getting Started\n\n1. Add `REST-in-Peace` to your dependencies\n\n        gem 'rest-in-peace'\n\n2. Choose which HTTP client you want to use\n\n        gem 'faraday'\n        \n3. Choose which HTTP adapter you want to use\n\n        gem 'excon'\n\n## Usage\n\n### HTTP Client Library\n\nThis gem depends on the HTTP client library [Faraday](https://github.com/lostisland/faraday). REST-in-Peace has been tested in combination \nwith [Faraday](https://github.com/lostisland/faraday) and [Excon](https://github.com/excon/excon) only. \n\n### Configuration\n\n#### HTTP Client\n\nYou need to configure and specify the HTTP client library to use. You can either specify a block (for lazy loading) or a client instance directly.\n\n```ruby\nclass Resource\n  include RESTinPeace\n    \n  rest_in_peace do\n    use_api -\u003e() do\n      ::Faraday.new(url: 'http://rip.dev', headers: { 'Accept' =\u003e 'application/json' }) do |faraday|\n        faraday.request :json\n        faraday.response :json\n        faraday.adapter :excon # make requests with Excon\n      end\n    end\n  end\nend\n\nclass ResourceTwo\n  include RESTinPeace\n  \n  rest_in_peace do\n    use_api -\u003e() { MyClient.api }\n  end\nend\n```\n\n#### Attributes\n\nYou need to specify all the attributes which should be read out of the parsed JSON. You have to specify whether an attribute\nis readonly or writeable:\n\n```ruby\nrest_in_peace do\n  attributes do\n    read :id\n    write :name\n  end\nend\n```\n\n**There must be at least an attribute called `id` to allow REST-in-Peace to work properly.** \n\n#### API Endpoints\n\nYou need to define all the API endpoints you want to consume with `REST-in-Peace`. Currently the five verbs `GET`, `POST`, `PUT`, `PATCH` and `DELETE` are supported.\n\nThere are two sections where you can specify endpoints: `resource` and `collection`. `collection` supports the HTTP verb `GET` only.\n\n```ruby\nrest_in_peace do\n  resource do\n    get :reload, '/rip/:id'\n  end\n  collection do\n    get :find, '/rip/:id'\n  end\nend\n```\n\n#### Resource\n\nIf you define anything inside the `resource` block, it will define a method on the instances of the class:\n```ruby\nclass Resource\n  include RESTinPeace\n  \n  rest_in_peace do\n    resource do\n      get :reload, '/rip/:id'\n      post :create, '/rip'\n    end\n  end\nend\n\nresource = Resource.new(id: 1)\nresource.create # calls \"POST /rip\"\nresource.reload # calls \"GET /rip/1\"\n```\n\n#### Collection\n\nIf you define anything inside the `collection` block, it will define a method on the class:\n\n```ruby\nclass Resource\n  include RESTinPeace\n  \n  rest_in_peace do\n    collection do\n      get :find, '/rip/:id'\n      get :find_on_other, '/other/:other_id/rip/:id'\n    end\n  end\nend\n\nresource = Resource.find(id: 1) # calls \"GET /rip/1\"\nresource = Resource.find_on_other(other_id: 42, id: 1337) # calls \"GET /other/42/rip/1337\"\n```\n\n#### HTTP Verb differences\n\nDepending on the given HTTP verb, a different set of attributes will be used as payload and query parameters.\n\nHTTP Verb           | passed | all | only changed and id | only id\n------------------- | :----: | :-: | :-----------------: | :-----:\n`GET` on collection | ✔      | ✘   | ✘                   | ✘\n`GET` on resource   | ✘      | ✔   | ✘                   | ✘\n`POST`              | ✘      | ✔   | ✘                   | ✘\n`PUT`               | ✘      | ✔   | ✘                   | ✘\n`PATCH`             | ✘      | ✘   | ✔                   | ✘\n`DELETE`            | ✘      | ✘   | ✘                   | ✔\n\n#### Pagination\n\nYou can define your own pagination module which will be mixed in when calling the API:\n\n```ruby\nclass Resource\n  include RESTinPeace\n  \n  rest_in_peace do\n    collection do\n      get :all, '/rips', paginate_with: MyClient::Paginator\n    end\n  end\nend\n```\n\nAn example pagination mixin with HTTP headers can be found in the [examples directory](https://github.com/ninech/REST-in-Peace/blob/master/examples) of this repo.\n\n#### ActiveModel Support\n\nFor easy interoperability with Rails, there is the ability to include ActiveModel into your class. To enable this support, follow these steps:\n\n* Define a `create` method (To be called for saving new objects)\n* Define a `save` method (To be called for updates)\n* Call `acts_as_active_model` **after** your *API endpoints* and *attribute* definitions\n\n##### Example\n\n```ruby\nrequire 'rest_in_peace'\nrequire 'faraday'\n\nmodule MyClient\n  class Fabric\n    include RESTinPeace\n\n    rest_in_peace do\n      use_api -\u003e() { MyClient.api }\n    \n      attributes do\n        read :id\n        write :name\n      end\n\n      resource do\n        post :create, '/fabrics'\n        patch :save, '/fabrics/:id'\n      end\n      \n      acts_as_active_model\n    end\n  end\nend\n```\n\n### Complete Configurations\n\n#### Configured Fabric class with all dependencies\n\n```ruby \nrequire 'my_client/paginator'\nrequire 'rest_in_peace'\nrequire 'faraday'\n\nmodule MyClient\n  class Fabric\n    include RESTinPeace\n\n    rest_in_peace do\n      use_api -\u003e() { MyClient.api }\n    \n      attributes do\n        read :id\n        write :name\n      end\n\n      resource do\n        patch :save, '/fabrics/:id'\n        post :create, '/fabrics'\n        delete :destroy, '/fabrics/:id'\n        get :reload, '/fabrics/:id'\n      end\n\n      collection do\n        get :all, '/fabrics', paginate_with: MyClient::Paginator\n        get :find, '/fabrics/:id'\n      end\n\n      acts_as_active_model\n    end\n  end\nend\n```\n\n#### Configured Fabric class using Faraday\n\n```ruby\nrequire 'my_client/paginator'\nrequire 'rest_in_peace'\nrequire 'faraday'\n\nmodule MyClient\n  class Fabric\n    include RESTinPeace\n\n    rest_in_peace do\n      use_api -\u003e() do\n        ::Faraday.new(url: 'http://localhost:3001', headers: { 'Accept' =\u003e 'application/json' }) do |faraday|\n          faraday.request :json\n          faraday.response :json\n          faraday.adapter :excon\n        end\n      end\n    \n      attributes do\n        read :id\n        write :name\n      end\n\n      resource do\n        patch :save, '/fabrics/:id'\n        post :create, '/fabrics'\n        delete :destroy, '/fabrics/:id'\n        get :reload, '/fabrics/:id'\n      end\n\n      collection do\n        get :all, '/fabrics', paginate_with: MyClient::Paginator\n        get :find, '/fabrics/:id'\n      end\n\n      acts_as_active_model\n    end\n  end\nend\n```\n\n#### CRUD options example of use\n```ruby\n# CREATE\nfabric = MyClient::Fabric.new(name: 'my new fabric')\nfabric.create # calls \"POST /fabrics\"\nfabric.reload # calls \"GET /fabrics/1\"\n\n# READ\nlast_fabric = MyClient::Fabric.find(id: fabric.id) # calls \"GET /fabrics/1\"\n\n# UPDATE - first way\nupdated_fabric = last_fabric.update_attributes(name: 'first way fabric')\nupdated_fabric.save # calls \"PATCH /fabrics/1\"\n\n# UPDATE - second way \nupdated_fabric = last_fabric.update(name: 'second way fabric') # calls \"PATCH /fabrics/1\"\n\n# DELETE\nupdated_fabric.destroy # calls \"DELETE /fabrics/1\"\n```\nMethod `update_attributes` sets updated value, `save` method stores all changes. This change can be done with calling \nsingle line method `update` which do the both things. \n\n## Helpers\n\n### SSL Configuration for Faraday\n\nThere is a helper class which can be used to create a Faraday compatible SSL configuration hash (with support for client certificates).\n\n```ruby\nssl_config = {\n  \"client_cert\" =\u003e \"/etc/ssl/private/client.crt\",\n  \"client_key\"  =\u003e \"/etc/ssl/private/client.key\",\n  \"ca_cert\"     =\u003e \"/etc/ssl/certs/ca-chain.crt\"\n}\n\nssl_config_creator = RESTinPeace::Faraday::SSLConfigCreator.new(ssl_config, :peer)\nssl_config_creator.faraday_options.inspect\n# =\u003e\n{\n  :client_cert =\u003e #\u003cOpenSSL::X509::Certificate\u003e,\n  :client_key  =\u003e Long key is long,\n  :ca_file     =\u003e \"/etc/ssl/certs/ca-chain.crt\",\n  :verify_mode =\u003e 1\n}\n```\n\n### Faraday Middleware: RIP Raise Errors\n\nThis middleware is mostly equivalent to [this one](https://github.com/lostisland/faraday/blob/cf549f4d883a3cae15db0d835628daa33f6f3a2b/lib/faraday/response/raise_error.rb) but it does not raise an error when the HTTP status code is `422` as this code is used to return validation errors.\n\n```ruby\nFaraday.new do |faraday|\n  # ...\n  faraday.response :rip_raise_errors\n  # ...\nend\n```\n\n## About\n\nThis gem is currently maintained and funded by [nine](https://nine.ch).\n\n[![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fninech%2Frest-in-peace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fninech%2Frest-in-peace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fninech%2Frest-in-peace/lists"}