Evil::Client
Human-friendly DSL for writing HTTP(s) clients in Ruby
Intro
The gem allows writing http(s) clients in a way inspired by Swagger specifications. It stands away from mutable states and monkey patching when possible. To support multithreading all instances are immutable (though not frozen to avoid performance loss).
Installation
Add this line to your application's Gemfile:
gem 'evil-client'
And then execute:
$ bundle
Or install it yourself as:
$ gem install evil-client
Synopsis
The following example gives an idea of how a client to remote API looks like when written on top of Evil::Client
. See full documentation for more details.
require "evil-client"
class CatsClient < Evil::Client
option :domain, proc(&:to_s)
option :user, proc(&:to_s)
option :password, proc(&:to_s)
path { "https://#{domain}.example.com/api" }
security { basic_auth settings.user, settings.password }
scope :cats do
option :version, default: proc { 1 }
path { "v#{version}" }
operation :update do
option :id, proc(&:to_i)
option :name, optional: true
option :color, optional: true
option :age, optional: true
let(:data) { options.select { |key, _| %i(name color age).include? key } }
validate { errors.add :no_filters if data.empty? }
path { "cats/#{id}" }
http_method :patch
format "json"
body { options.except(:id, :version) }
response 200 do |(status, headers, body)|
Cat.new JSON.parse(body)
end
response(400, 422) { |(status, *)| raise "#{status}: Record invalid" }
end
end
end
cat_client = CatClient.new domain: "awesome-cats",
user: "cat_lover",
password: "purr"
cat_client.scopes[:cats].new(version: 2)
.operations[:update].new(id: 4, age: 10, color: "tabby")
.call
cat_client.cats(version: 2).update(id: 4, age: 10, color: "tabby")
License
The gem is available as open source under the terms of the MIT License.