Giter Site home page Giter Site logo

romand / rest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iron-io/rest

0.0 2.0 0.0 197 KB

HTTP/REST client wrapper that provides a standard interface for making http requests using different http clients.

Home Page: www.iron.io

License: BSD 2-Clause "Simplified" License

Ruby 100.00%

rest's Introduction

Rest Wrapper

HTTP/REST client wrapper that provides a standard interface for making http requests using different http clients. If no client is specified it will choose the best http client you have installed based on our performance tests.

Features

  • All clients behave exactly the same:
    • Same error behavior
    • Same 30X redirect behavior
    • Same response object methods
    • Same way to access and manipulate requests and responses such as body, headers, code, etc.
  • Chooses best client you have installed on your system based on what we have found performs the best.
    • Currently net_http_persistent and typhoeus are nearly the same, but since net_http_persistent doesn't have a binary dependency, it wins.
    • You can run performance tests yourself by running: ruby test/test_performance.rb, quite a difference between the libs.
  • Handles 503 errors with exponential backoff.

Getting Started

Install the gem:

gem install rest

Create an Rest client:

@rest = Rest::Client.new()

To choose a specific underlying http client lib:

@rest = Rest::Client.new(:gem=>:typhoeus)

Supported http libraries are:

  • rest_client
  • net_http_persistent
  • typhoeus
  • internal - this gem's built in client.

Then use it:

GET

@rest.get(url, options...)

options:

  • :params => query params for url
  • :headers => headers

POST

@rest.post(url, options...)

options:

  • :body => POST body
  • :headers => headers hash
  • :form_data => hash of fields/values, sent form encoded (only tested with default net-http-persistent)

PUT

@rest.put(url, options...)

options:

  • :body => POST body
  • :headers => headers

DELETE

@rest.delete(url, options...)

Responses

The response object you get back will always be consistent and will have the following functions:

response.code
response.body

Exceptions

If it didn't get a response for whatever reason, you will get a Rest::ClientError

If status code is 40X or 50X, it will raise an exception with the following methods.

err.code
err.response (which has body: err.response.body)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.