No commit activity in last 3 years
No release in over 3 years
A request is never sent till a response have need.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
= 2.14.1

Runtime

 Project Readme

Faraday::Lazyable

A request is never sent till a response have need.

Installation

gem install faraday-lazyable

Usage

require "faraday-lazyable"

connection = Faraday.new do |connection|
  connection.use Faraday::Lazyable
  connection.adapter :net_http
  connection.response :logger
end

# A dummy response is returned.
response = connection.get("http://example.com")

# The HTTP request is sent when any method call happened to the response.
response.status #=> 200

Example

Suppose that you are building a client web application for your API server. Your controller sends HTTP request to the server. You're trying to cache the HTTP request by using Fragment Cache in the view layer to improve performance.

Controller

class RecipesController < ApplicationController
  def show
    response = client.get("http://example.com/recipes/#{params[:id]}")
    @recipe = Recipe.new(response)
  end

  private

  def client
    Faraday.new
  end
end

View

<% cache do %>
  <article class="recipe">
    <h1><%= @recipe.title %></h1>
    <p><%= @recipe.description %></p>
  </article>
<% end %>

Problem

But you found that you cannot cache the HTTP request this way because the HTTP request is already sent at the controller.

Solution

Faraday::Lazyable fits this type of problem. The response (@recipe) will become lazy-evaluated and the HTTP request will never be sent till @recipe.title is called at the view.

def client
  Faraday.new do |connection|
    connection.use Faraday::Lazyable
  end
end