Skip to content

Instantly share code, notes, and snippets.

@xl1
Created July 27, 2014 15:12
Show Gist options
  • Save xl1/58e33a3eb13d5a47a1e2 to your computer and use it in GitHub Desktop.
Save xl1/58e33a3eb13d5a47a1e2 to your computer and use it in GitHub Desktop.
api = require('./urlmapper')('https://api.example.com/v1')
api.users.GET(limit: 1)
.then ([user]) ->
console.log user.name, user.age
api.users[user.id].posts.GET()
.then (posts) ->
posts.forEach ({ content }) -> console.log content
require 'harmony-reflect'
Promise = require 'bluebird'
module.exports = createUrlMapper = (base) ->
new Proxy {},
get: (target, name) ->
if /^[A-Z]+$/.test name
(data) ->
Promise.resolve $.ajax(base, method: name, data: data)
else
createUrlMapper "#{base}/#{encodeURIComponent name}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment