• Stars
    star
    104
  • Rank 330,604 (Top 7 %)
  • Language
    Ruby
  • License
    Other
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Human-friendly DSL for writing HTTP(s) clients in Ruby

Evil::Client

Human-friendly DSL for writing HTTP(s) clients in Ruby

Sponsored by Evil Martians

Gem Version Inline docs Documentation Status Coverage Status

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
  # Define options for the client's initializer
  option :domain,   proc(&:to_s)
  option :user,     proc(&:to_s)
  option :password, proc(&:to_s)

  # Definitions shared by all operations
  path     { "https://#{domain}.example.com/api" }
  security { basic_auth settings.user, settings.password }

  scope :cats do
    # Scope-specific definitions
    option :version,  default: proc { 1 }
    path { "v#{version}" } # subpath added to root path

    # Operation-specific definitions to update a cat by id
    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}" } # added to root path
      http_method :patch # you can use plain syntax instead of a block
      format      "json"
      body        { options.except(:id, :version) } # [#slice] is available too

      # Parses json response and wraps it into Cat instance with additional
      # parameter
      response 200 do |(status, headers, body)|
        # Suppose you define a model for cats
        Cat.new JSON.parse(body)
      end

      # Parses json response, wraps it into model with [#error] and raises
      # an exception where [ResponseError#response] contains the model istance
      response(400, 422) { |(status, *)| raise "#{status}: Record invalid" }
    end
  end
end

# Instantiate a client with a concrete settings
cat_client = CatClient.new domain:   "awesome-cats",
                           user:     "cat_lover",
                           password: "purr"

# Use verbose low-level DSL to send requests
cat_client.scopes[:cats].new(version: 2)
          .operations[:update].new(id: 4, age: 10, color: "tabby")
          .call # sends request

# Use top-level DSL for the same request
cat_client.cats(version: 2).update(id: 4, age: 10, color: "tabby")

# Both the methods send `PATCH https://awesome-cats.example.com/api/v2/cats/4`
# with a specified body and headers (authorization via basic_auth)

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

lefthook

Fast and powerful Git hooks manager for any type of projects.
Go
4,901
star
2

mono

Free and open-source monospaced font from Evil Martians
2,122
star
3

oklch-picker

Color Picker for LCH
TypeScript
757
star
4

terraforming-rails

Terraforming legacy Rails applications guides and tools
Ruby
731
star
5

evil-seed

A Gem for creating partial anonymized dumps of your database using your app model relations.
Ruby
451
star
6

ruby-on-whales

Ruby on Whales example and templates
Ruby
286
star
7

fullstaq-ruby-docker

Docker image for Ruby build from Fullstaq packages based on Debian 10, 11, and 12.
Dockerfile
176
star
8

harmony

Harmony color palette
TypeScript
166
star
9

activerecord-slotted_counters

Active Record extension providing slotted counters support
Ruby
102
star
10

fias

Ruby wrapper for the Russian FIAS database (Федеральная Информационная Адресная Система)
Ruby
82
star
11

chronicles-gql-martian-library

Ruby
71
star
12

chef-kubernetes

Google Kubernetes installer for ubuntu >= 16.04
Ruby
69
star
13

figma-polychrom

Figma plugin for displaying the contrast level and font size recommendations according to the APCA method
TypeScript
67
star
14

liquor

Liquor is a safe sandboxing compiling template language for Ruby
Ruby
59
star
15

graphql-connections

Additional implementations of cursor-based paginations for GraphQL Ruby gem.
Ruby
45
star
16

evil_chat

Code for "Evil Front: Modern Front-end in Rails" 3-part tutorial: https://evilmartians.com/chronicles/evil-front-part-1
Ruby
41
star
17

chef-prometheus-exporters

Ruby
38
star
18

chef-nginx

Chef Nginx recipes
Ruby
35
star
19

telephony

Evil Martians' telephony application that run our phone numbers
JavaScript
30
star
20

capybara-thruster

Thruster server definition for Capybara
Ruby
29
star
21

foundry

Foundry Compiler
OCaml
24
star
22

rubocoping-generator

Ruby application template to configure RuboCop with Standard and plugins
Ruby
15
star
23

furnace-xray

A visualizer for transformations of code in Static Single Assignment form based on the Furnace library.
CoffeeScript
11
star
24

cupid

Create, organize and send emails through ExactTarget SOAP API
Ruby
10
star
25

redis-proxy

Redis Proxy
Go
9
star
26

evil-struct

Nested structure with type constraints, based on the `dry-initializer` DSL
Ruby
9
star
27

omniauth-ebay-oauth

OmniAuth Strategy for eBay Apps (for using with eBay REST APIs)
Ruby
7
star
28

zendesk

Proper Ruby wrapper around the Zendesk API
Ruby
6
star
29

astro-typedoc

A tool for building astro-based documentation sites using Typescript types definitions and TSDocs.
JavaScript
6
star
30

catalyst-tutorial

Beyond Fashion Deep Learning With Catalyst post code
Python
6
star
31

monstro

Cult of Martians screensaver
Objective-C
5
star
32

foundry-lib

Foundry standard library and examples (public)
Fancy
5
star
33

github-tv

Forks your colleagues' open-source repos to your organization page
Ruby
4
star
34

chef-mdadm

mdadm recipe
Ruby
4
star
35

amplifr-node

JavaScript
3
star
36

homebrew-lefthook

Homebrew Formulae to lefthook
Ruby
2
star
37

kibana-logtrail

Kibana docker image with logtrail plugin.
Dockerfile
2
star
38

helm-kubectl-docker

Dockerfile
2
star
39

docker-curl

A simple docker image which contains curl (based on Alpine linux)
Dockerfile
1
star
40

charts

Public Helm charts
Smarty
1
star
41

foundry-web

Foundry website
JavaScript
1
star
42

gcloud-helm-docker

Deploy image for GKE with Helm & kubectl binaries.
Dockerfile
1
star
43

chef-consul-wrapper

Ruby
1
star
44

elasticsearch-k8s

1
star