Project

clientura

0.0
No commit activity in last 3 years
No release in over 3 years
DSL for defining HTTP clients
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Project Readme

Clientura Build Status Test Coverage Code Climate Issue Count Gem Version

Basic concepts

Create client class

class RandomApiClient
  include Clientura::Client
end

Define middleware (blocks which can be composed to configure your request before it is sent)

class RandomApiClient
  include Clientura::Client

  middleware :with_token, -> { headers(token: client.config[:token]) }
end

Define pipes (blocks which can be composed to process response)

class RandomApiClient
  include Clientura::Client

  middleware :with_token, -> { headers(token: client.config[:token]) }

  pipe :body_retriever, -> (res) { res.body.to_s }
  pipe :parser, -> (res, parser) { parser.parse res }
  pipe :data_retriever, -> (res) { res['data'] }
end

Compose this stuff!

class RandomApiClient
  include Clientura::Client

  middleware :with_token, -> { headers(token: client.config[:token]) }

  pipe :body_retriever, -> (res) { res.body.to_s }
  pipe :parser, -> (res, parser) { parser.parse res }
  pipe :data_retriever, -> (res) { res['data'] }

  use_middleware :with_token do
    pipe_through :body_retriever, [:parser, JSON], :data_retriever do
      get :comments # implicit path 'comments'
      get :users, path: 'api/users' # explicit path
      get :user, path: -> (params) { "api/users/#{params[:id]}"} # dynamic path
    end
  end
end

Also instance should be created with token which will be used by middleware

# ...
  def initialize(token:)
    save_config token: token
  end
# ...

Instantiate and use!

client = RandomApiClient.new(token: 'Moms Birthday')
client.comments
client.comments_promise # yeap, asyncrony baby, backed by concurrent-ruby
client.users_promise.value # just retrieve result from promise
client.user(id: 1)
client.user_promise(id: 1).then do |data_retrieved_through_pipes|
  # process it asyncronously ...
end