-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0418209
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Adapter | ||
module Rest | ||
VERSION = "0.0.1" | ||
end | ||
end |