Last active
August 29, 2015 14:15
-
-
Save plukevdh/27601caaf974554eead0 to your computer and use it in GitHub Desktop.
api sketch for a project
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
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