Skip to content

Instantly share code, notes, and snippets.

@teamon
Last active December 12, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teamon/4698874 to your computer and use it in GitHub Desktop.
Save teamon/4698874 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Database.Redis hiding (decode)
import Text.JSON
import Data.ByteString.Char8 (unpack)
import Control.Arrow (left)
import Data.Traversable (traverse)
getValueFromJSON json = decode json >>= valFromObj "value" :: Result (Float)
getEvents = do
result <- zrange "test-range" 0 4
return $ do
items <- left show result
(resultToEither . traverse (getValueFromJSON . unpack)) items
main = do
conn <- connect defaultConnectInfo
runRedis conn getEvents
require "json"
require "redis"
redis = Redis.new
puts redis.zrange("test-range", 0, 4).map {|e| JSON.parse(e)["value"].to_f }.inspect
 ~cur_dir λ ./test.sh 20:14:45
1. No key
= Haskell =
Right []
= Ruby =
[]
2. Valid JSON
= Haskell =
Right [123.0]
= Ruby =
[123.0]
3. Missing JSON field
= Haskell =
Left "valFromObj: Could not find key: \"value\""
= Ruby =
[0.0]
4. Invalid JSON
= Haskell =
Left "Invalid tokens at end of JSON string: \" 123\""
= Ruby =
/Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/json/common.rb:148:in `parse': 743: unexpected token at '"val" 123' (JSON::ParserError)
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/json/common.rb:148:in `parse'
from redis.rb:5:in `block in <main>'
from redis.rb:5:in `map'
from redis.rb:5:in `<main>'
5. Invalid value
= Haskell =
Left "Unable to read Float"
= Ruby =
[0.0]
6. No redis
= Haskell =
redis.hs: connect: does not exist (Connection refused)
= Ruby =
/Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:268:in `rescue in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED) (Redis::CannotConnectError)
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:263:in `establish_connection'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:69:in `connect'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:282:in `ensure_connected'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:173:in `block in process'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:248:in `logging'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:172:in `process'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis/client.rb:84:in `call'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis.rb:1396:in `block in zrange'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis.rb:36:in `block in synchronize'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis.rb:36:in `synchronize'
from /Users/teamon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/redis-3.0.2/lib/redis.rb:1395:in `zrange'
from redis.rb:5:in `<main>'
Test Haskell Ruby
No key OK OK
Valid JSON OK OK
Missing JSON field OK Silent fail
Invalid JSON OK Exception
Invalid value OK Silent fail
No redis Error Exception
#!/usr/bin/env bash
function go {
echo "= Haskell ="
runhaskell redis.hs
echo "= Ruby ="
ruby redis.rb
}
function hd {
echo
echo
echo $1
}
hd "1. No key"
redis-cli DEL "test-range" > /dev/null
go
hd "2. Valid JSON"
redis-cli DEL "test-range" > /dev/null
redis-cli ZADD "test-range" 10.0 '{"value": 123}' > /dev/null
go
hd "3. Missing JSON field"
redis-cli DEL "test-range" > /dev/null
redis-cli ZADD "test-range" 10.0 '{"val": 123}' > /dev/null
go
hd "4. Invalid JSON"
redis-cli DEL "test-range" > /dev/null
redis-cli ZADD "test-range" 10.0 '"val" 123' > /dev/null
go
hd "5. Invalid value"
redis-cli DEL "test-range" > /dev/null
redis-cli ZADD "test-range" 10.0 '{"value": "blah"}' > /dev/null
go
hd "6. No redis"
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
go
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment