Socket
Book a DemoInstallSign in
Socket

nestful

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestful

1.1.4
bundlerRubygems
Version published
Maintainers
1
Created
Source

Nestful is a simple Ruby HTTP/REST client with a sane API.

Installation

sudo gem install nestful

Features

  • Simple API
  • JSON requests
  • Resource API
  • Proxy support
  • SSL support

API

GET request

Nestful.get 'http://example.com' #=> "body"

POST request

# url-encoded form POST
Nestful.post 'http://example.com', :foo => 'bar'

# JSON POST
Nestful.post 'http://example.com', {:foo => 'bar'}, :format => :json

Parameters

# You can also provide nestled params
Nestful.get 'http://example.com', :nestled => {:vars => 1}

Request

Request is the base class for making HTTP requests - everthing else is just an abstraction upon it.

Nestful::Request.new(url, options).execute #=> <Nestful::Response>

Valid Request options are:

  • headers (hash)
  • params (hash)
  • method (:get/:post/:put/:delete/:head)
  • proxy
  • user
  • password
  • auth_type (:basic/:bearer)
  • timeout
  • ssl_options

Requests are run via the execute method.

Endpoint

The Endpoint class provides a single object to work with restful services. The following example does a GET request to the URL; http://example.com/assets/1/

Nestful::Endpoint.new('http://example.com')['assets'][1].get #=> Nestful::Response

Resource

If you're building a binding for a REST API, then you should consider using the Resource class.

class Charge < Nestful::Resource
  endpoint 'https://api.stripe.com/v1/charges'
  options :auth_type => :bearer, :password => 'sk_bar'

  def self.all
    self.new(get)
  end

  def self.find(id)
    self.new(get(id))
  end

  def refund
    post(:refund)
  end
end

Charge.all #=> []
Charge.find('ch_bar').amount

Response

All HTTP responses are in the form of a Nestful::Response instance. This contains the raw HTTP response, body, headers and a few helper methods:

response = Nestful.get('http://www.google.com')
response.body #=> '<html>...'
response.headers #=> {'Content-Type' => 'text/html'}
response.status #=> 200

You can also access the decoded body if available, such as for JSON responses:

response = Nestful.get('http://api.stripe.com/v1/charges')
charges  = response.decoded

All calls are proxied to the decoded body, so you can access JSON properties like this:

charges = Nestful.get('http://api.stripe.com/v1/charges')['data']

Credits

Parts of the connection code were inspired from ActiveResource.

FAQs

Package last updated on 07 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.