Skip to content

Commit

Permalink
version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lorensr committed Apr 3, 2011
0 parents commit 0418209
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
= adapter-rest

Rest adapter for use with https://github.com/newtoy/toystore.

<tt>
ruby examples/server.rb
ruby examples/rest.rb
</tt>

25 changes: 25 additions & 0 deletions examples/rest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative '../lib/adapter/rest'
require_relative 'restful-key-value-store'

foo_client = RestfulKeyValueStore.new 'localhost:4567', '/kv-store/foo/'
foo_adapter = Adapter[:rest].new(foo_client)

foo_adapter.write('a', '1')
puts 'Should be 1: ' + foo_adapter.get('a')

foo_adapter.delete 'a'
puts 'Should be nil: ' + foo_adapter.get('a').inspect

bar_client = RestfulKeyValueStore.new 'localhost:4567', '/kv-store/bar/', 'user', 'pass'
bar_adapter = Adapter[:rest].new(bar_client)

bar_adapter.write('b', '3')
foo_adapter.write('a', '1')
foo_adapter.write('b', '2')

puts 'Should be 3: ' + bar_adapter.get('b')
puts 'Should be 2: ' + foo_adapter.get('b')

foo_adapter.clear
puts 'Should be nil: ' + foo_adapter.get('a').inspect

25 changes: 25 additions & 0 deletions examples/restful-key-value-store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'httparty'

class RestfulKeyValueStore
include HTTParty

def initialize(base_uri, namespace, username=nil, password=nil)
self.class.base_uri base_uri
@namespace = namespace
@options = {}
@options[:basic_auth] = {username: username, password: password} if username
end

def get(key)
self.class.get @namespace + key, @options
end

def put(key, value)
@options[:body] = value
self.class.put @namespace + key, @options
end

def delete(key)
self.class.delete @namespace + key, @options
end
end
55 changes: 55 additions & 0 deletions examples/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'sinatra'
enable :logging, :dump_errors, :raise_errors

ROUTE = '/kv-store/:hash/:key'
PROTECTED = ['bar']

$hashes = {}

helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Not authorized\n"])
end
end

def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['user', 'pass']
end

def assure hash
$hashes[hash] = {} unless $hashes[hash]
end
end

get '/kv-store/:hash/' do |hash|
if PROTECTED.include? hash
protected!
end

Marshal.dump $hashes[hash].keys
end

get ROUTE do |hash, key|
if PROTECTED.include? hash
protected!
end

assure hash
Marshal.dump $hashes[hash][key]
end

put ROUTE do |hash, key|
if PROTECTED.include? hash
protected!
end

assure hash
$hashes[hash][key] = Marshal.load(request.body.string)
end

delete ROUTE do |hash, key|
$hashes[hash].delete key
end
26 changes: 26 additions & 0 deletions lib/adapter/rest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'adapter'

module Adapter
module Rest
def read(key)
decode(client.get(key_for(key)))
end

def write(key, value)
client.put(key_for(key), encode(value))
end

def delete(key)
client.delete(key_for(key))
end

def clear
keys = read ''
keys.each do |key|
delete key
end
end
end
end

Adapter.define(:rest, Adapter::Rest)
5 changes: 5 additions & 0 deletions lib/adapter/rest/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Adapter
module Rest
VERSION = "0.0.1"
end
end

0 comments on commit 0418209

Please sign in to comment.