Skip to content

Instantly share code, notes, and snippets.

@plukevdh
Last active August 29, 2015 14:15
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 plukevdh/27601caaf974554eead0 to your computer and use it in GitHub Desktop.
Save plukevdh/27601caaf974554eead0 to your computer and use it in GitHub Desktop.
api sketch for a project
GithubClient.config do |c|
c.endpoint "https://api.github.com/"
c.default_handler JSONHandler # handles responses as JSON
end
class JSONHandler
def initialize(data)
@data = data
end
def parse
JSON.parse(@data.body.to_s)
end
end
module GithubClient
class Users
# The class to wrap the responses in
instance_class User
# will return a collection of `User` objects
get '/users', type: :collection
# will return a single `User` object
get '/users/:username', type: :instance do |response|
# ... custom handling
end
post '/'
not_found do
# handle errors
end
end
# alternatively, if you don't specify `instance_class`, it will simply
# return a hash of data
end
class User
attribute :name
attribute :email
# ...
def from_data(data)
# ... take raw hash, set attrs, etc
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment