Skip to content

Instantly share code, notes, and snippets.

@rslhdyt
Last active March 23, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rslhdyt/e5bd3222b07f0243acb4d7611dac788b to your computer and use it in GitHub Desktop.
Save rslhdyt/e5bd3222b07f0243acb4d7611dac788b to your computer and use it in GitHub Desktop.
The API wrapper example for https://reqres.in/ in ruby

Reqres.in API

please see the link (https://reqres.in/) for list of endpoints

Dependencies

This API wrapper use Faraday client, please install faraday client in your local env

gem install faraday

How to use the API

  1. save the api.rb
  2. open IRB and load the api.rb
irb -I . -r api.rb
  1. call the API methods
client = Api.new

response = client.get('users')
response_json = JSON.parse(response.body)
response_json['data']

result

[
  {"id"=>1, "email"=>"george.bluth@reqres.in", "first_name"=>"George", "last_name"=>"Bluth", "avatar"=>"https://reqres.in/img/faces/1-image.jpg"},
  {"id"=>2, "email"=>"janet.weaver@reqres.in", "first_name"=>"Janet", "last_name"=>"Weaver", "avatar"=>"https://reqres.in/img/faces/2-image.jpg"},
  {"id"=>3, "email"=>"emma.wong@reqres.in", "first_name"=>"Emma", "last_name"=>"Wong", "avatar"=>"https://reqres.in/img/faces/3-image.jpg"},
  {"id"=>4, "email"=>"eve.holt@reqres.in", "first_name"=>"Eve", "last_name"=>"Holt", "avatar"=>"https://reqres.in/img/faces/4-image.jpg"},
  {"id"=>5, "email"=>"charles.morris@reqres.in", "first_name"=>"Charles", "last_name"=>"Morris", "avatar"=>"https://reqres.in/img/faces/5-image.jpg"},
  {"id"=>6, "email"=>"tracey.ramos@reqres.in", "first_name"=>"Tracey", "last_name"=>"Ramos", "avatar"=>"https://reqres.in/img/faces/6-image.jpg"}
]      
require 'faraday'
class Api
def initialize; end
def get(path, params: {})
client.get(path, params)
end
def post(path, params: {})
client.post(path, params)
end
def put(path, params: {})
client.put(path, params)
end
def delete(path, params: {})
client.delete(path, params)
end
private
def client
@client ||= Faraday.new(base_url) do |faraday|
faraday.adapter Faraday.default_adapter
end
end
def base_url
'https://reqres.in/api/'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment