Skip to content

Commit

Permalink
Doc fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ericentin committed Jul 12, 2016
1 parent ded4f3d commit 6b4a8a8
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions lib/jsonrpc2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ defmodule JSONRPC2 do
@moduledoc ~S"""
`JSONRPC2` is an Elixir library for JSON-RPC 2.0.
It includes request and response utility modules, a transport-agnostic server handler, and a
It includes request and response utility modules, a transport-agnostic server handler, a
line-based TCP server and client, which are based on [Ranch](https://github.com/ninenines/ranch)
and [shackle](https://github.com/lpgauth/shackle), respectively.
and [shackle](https://github.com/lpgauth/shackle), respectively, and a JSON-in-the-body HTTP(S)
server and client, based on [Plug](https://github.com/elixir-lang/plug) and
[hackney](https://github.com/benoitc/hackney), respectively.
## Example
## TCP Example
# Define a handler
defmodule Handler do
Expand Down Expand Up @@ -79,6 +81,59 @@ defmodule JSONRPC2 do
Client.notify("Elixir")
#=> You have been notified, Elixir!
## HTTP Example
# Define a handler
defmodule Handler do
use JSONRPC2.Server.Handler
def handle_request("hello", [name]) do
"Hello, #{name}!"
end
def handle_request("hello2", %{"name" => name}) do
"Hello again, #{name}!"
end
def handle_request("notify", [name]) do
IO.puts "You have been notified, #{name}!"
end
end
# Start the server (this will usually go in your OTP application's start/2)
JSONRPC2.Servers.HTTP.http(Handler)
# Define the client
defmodule Client do
alias JSONRPC2.Clients.HTTP
@url "http://localhost:4000/"
def hello(name) do
HTTP.call(@url, "hello", [name])
end
def hello2(args) do
HTTP.call(@url, "hello2", Map.new(args))
end
def notify(name) do
HTTP.notify(@url, "notify", [name])
end
end
# Make a call with the client to the server
IO.inspect Client.hello("Elixir")
#=> {:ok, "Hello, Elixir!"}
# Make a call with the client to the server, using named args
IO.inspect Client.hello2(name: "Elixir")
#=> {:ok, "Hello again, Elixir!"}
# Notifications
Client.notify("Elixir")
#=> You have been notified, Elixir!
## Serializers
Any module which conforms to the same API as Poison's `Poison.encode/1` and `Poison.decode/1` can
Expand Down

0 comments on commit 6b4a8a8

Please sign in to comment.