Skip to content

Instantly share code, notes, and snippets.

@rslhdyt
Last active March 23, 2022 02:16
Show Gist options
  • Select an option

  • Save rslhdyt/e5bd3222b07f0243acb4d7611dac788b to your computer and use it in GitHub Desktop.

Select an option

Save rslhdyt/e5bd3222b07f0243acb4d7611dac788b to your computer and use it in GitHub Desktop.

Revisions

  1. rslhdyt revised this gist Mar 23, 2022. 1 changed file with 22 additions and 22 deletions.
    44 changes: 22 additions & 22 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -2,32 +2,32 @@

    class Api
    def initialize; end

    def get(path, params: {})
    client.get(path, params)
    end
    client.get(path, params)
    end

    def post(path, params: {})
    client.post(path, params)
    end
    def post(path, params: {})
    client.post(path, params)
    end

    def put(path, params: {})
    client.put(path, params)
    end
    def put(path, params: {})
    client.put(path, params)
    end

    def delete(path, params: {})
    client.delete(path, params)
    end
    def delete(path, params: {})
    client.delete(path, params)
    end

    private
    private

    def client
    @client ||= Faraday.new(base_url) do |faraday|
    faraday.adapter Faraday.default_adapter
    end
    end
    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
    def base_url
    'https://reqres.in/api/'
    end
    end
  2. rslhdyt revised this gist Mar 23, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    require 'faraday'

    class Api
    def initialize; end
    def initialize; end

    def get(path, params: {})
    def get(path, params: {})
    client.get(path, params)
    end

  3. rslhdyt revised this gist Mar 23, 2022. No changes.
  4. rslhdyt revised this gist Mar 23, 2022. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -24,4 +24,18 @@ client = Api.new
    response = client.get('users')
    response_json = JSON.parse(response.body)
    response_json['data']
    ```

    ### result

    ```
    [
    {"id"=>1, "email"=>"[email protected]", "first_name"=>"George", "last_name"=>"Bluth", "avatar"=>"https://reqres.in/img/faces/1-image.jpg"},
    {"id"=>2, "email"=>"[email protected]", "first_name"=>"Janet", "last_name"=>"Weaver", "avatar"=>"https://reqres.in/img/faces/2-image.jpg"},
    {"id"=>3, "email"=>"[email protected]", "first_name"=>"Emma", "last_name"=>"Wong", "avatar"=>"https://reqres.in/img/faces/3-image.jpg"},
    {"id"=>4, "email"=>"[email protected]", "first_name"=>"Eve", "last_name"=>"Holt", "avatar"=>"https://reqres.in/img/faces/4-image.jpg"},
    {"id"=>5, "email"=>"[email protected]", "first_name"=>"Charles", "last_name"=>"Morris", "avatar"=>"https://reqres.in/img/faces/5-image.jpg"},
    {"id"=>6, "email"=>"[email protected]", "first_name"=>"Tracey", "last_name"=>"Ramos", "avatar"=>"https://reqres.in/img/faces/6-image.jpg"}
    ]
    ```
  5. rslhdyt created this gist Mar 23, 2022.
    27 changes: 27 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # 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
    ```
    3. call the API methods

    ```
    client = Api.new
    response = client.get('users')
    response_json = JSON.parse(response.body)
    ```
    33 changes: 33 additions & 0 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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