Coder Social home page Coder Social logo

threez / rack-rpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rack-rpc/rack-rpc

2.0 2.0 2.0 175 KB

Rack middleware for serving up RPC endpoints.

Home Page: http://datagraph.rubyforge.org/rack-rpc/

License: The Unlicense

Ruby 100.00%

rack-rpc's Introduction

JSON-RPC/XML-RPC Server for Rack Applications Build Status

This is a Rack middleware that facilitates the creation of protocol-agnostic RPC servers. The current implementation provides support for JSON-RPC 2.0 and XML-RPC.

Features

  • Handles JSON-RPC and XML-RPC requests with the same code.
  • Compatible with any Rack application and any Rack-based framework.
  • Provides Rails-style controller filtering for your RPC methods.

Examples

A basic RPC server

require 'rack/rpc'

class Server < Rack::RPC::Server
  def hello_world
    "Hello, world!"
  end
  rpc 'hello_world' => :hello_world
end

Simple filtering

require 'rack/rpc'

class Server < Rack::RPC::Server
  before_filter :check_auth

  def hello_world
    "Hello, world!"
  end
  rpc 'hello_world' => :hello_world

  private

  def check_auth
    raise "Not authorized" unless authorized
  end
end

Filtering via a proc with more options

require 'rack/rpc'

class Server < Rack::RPC::Server
  before_filter :check_auth, :only => :super_secret_hello_world do
    raise "Not authorized" unless authorized
  end

  def hello_world
    "Hello, world!"
  end
  rpc 'hello_world' => :hello_world

  def super_secret_hello_world
    'super_secret_hello_world'
  end
  rpc 'super_secret_hello_world' => :super_secret_hello_world
end

Running the server

# config.ru
use Rack::RPC::Endpoint, Server.new

run MyApplication

Customizing the default RPC path

# config.ru
use Rack::RPC::Endpoint, Server.new, :path => '/api'

run MyApplication

More on Filters

The :only and :except options for filters can take a single method or an array of methods.

You can halt execution in a filter by raising an exception. An error response will be returned with the exception's message set as the error object's message text.

Communicationg with the Server

By default, methods will only be invoked on POST requests to "/rpc". The default path can be overridden by sending a :path option when creating your middleware (see example above).

The protocol used is determined by the CONTENT_TYPE header ("application/xml" and "text/xml" for XML and "application/json" for JSON).

Dependencies

Installation

The recommended installation method is via RubyGems. To install the latest official release of the gem, do:

% [sudo] gem install rack-rpc

Download

To get a local working copy of the development repository, do:

% git clone git://github.com/datagraph/rack-rpc.git

Alternatively, download the latest development version as a tarball as follows:

% wget http://github.com/datagraph/rack-rpc/tarball/master

Mailing List

Authors

License

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.

rack-rpc's People

Contributors

artob avatar jhuckabee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

sumup obfuscoder

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.