Skip to content

Instantly share code, notes, and snippets.

@BryanDonovan
Last active August 29, 2015 14:00
Show Gist options
  • Save BryanDonovan/a06565981f7cd93f9e19 to your computer and use it in GitHub Desktop.
Save BryanDonovan/a06565981f7cd93f9e19 to your computer and use it in GitHub Desktop.
client = MegaMusic::Client.new(:app_token => 'foo')
client.log_in
client.playlists.get_collection(:user_id => client.user.id)
module MegaMusic
class Client
def initialize(args={})
@app_token = args[:app_token]
# etc.
end
def get(path, params)
#.. proxy to http client GET
end
def post(path, params)
#.. proxy to http client POST
end
def log_in
@auth_info = auth.log_in
@user = users.get(:id => @auth_info.user_id)
end
# Essentially a namespace so we can do things like:
# @client.playlists.get(987)
# @client.playlists.get_collection(:user_id => 123)
# Instead of
# @client.get_playlist(987)
# @client.get_playlists(:user_id => 123)
def playlists
@playlist_client ||= PlaylistClient.new(self)
end
def users
@user_client ||= UserClient.new(self)
end
def auth
@auth_client ||= AuthClient.new(self)
end
end
class AuthClient
def initialize(client)
@client = client
end
def log_in(params)
client.post("/api/oauth/tokens")
end
end
class PlaylistClient
def initialize(client)
@client = client
end
def get(params)
client.get("/api/playlists/:id", params)
end
def get_collection(params)
client.get("/api/:user_id/playlists", params)
end
end
class UserClient
def initialize(client)
@client = client
end
def get(params)
client.get("/api/users/:id", params)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment