Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
R Vircurex API wrapper
library(rjson)
library(RCurl)
#Magic: this function chooses the endpoint type based on the name
# of the caller.
# Arguments are a list of values which are matched
# with their named defaults.
request <- function(added_args, named_args){
#Matching nameless arguments to named arguments in declaration:
if(length(added_args) > 0) for(i in 1:length(added_args)){
named_args[[i]] <- added_args[[i]]
}
caller_details <- sys.calls()[[sys.nframe()-1]]
request_type <- caller_details[[1]] #gets name of caller
parameters <- ""
# Any parameters to this call? If so, join them as k=v&k2=v2...
if(length(named_args) > 0){
argnames <- names(named_args)
argnames <- argnames[nchar(argnames) > 0]
kvpairs <- sapply(argnames, function(name) paste0(name, "=", named_args[name]))
kvlist <- paste(kvpairs, collapse="&")
parameters <- paste0("?", kvlist)
}
url <- paste0("https://api.vircurex.com/api/", request_type, ".json", parameters)
fromJSON(json_str=getURL(url))
}
query_trade <- function(base = "BTC", alt="USD")
request(as.list(match.call())[-1], formals())
#This works because request looks at the caller's name to pick an endpoint:
get_last_trade <- query_trade
get_lowest_ask <- query_trade
get_highest_bid <- query_trade
get_highest_bid <- query_trade
get_volume <- query_trade
get_info_for_1_currency <- query_trade
orderbook <- query_trade
trades <- query_trade
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment