Created
May 30, 2012 22:21
-
-
Save tylr/2839296 to your computer and use it in GitHub Desktop.
Pidgin interface prototype
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
# Example of the Dropbox dialect | |
# DropboxCredentials inherits from Pidgin::Credentials::Base | |
# it's basically a store for what each dialect needs specifically to access an API | |
dialect :dropbox, :credentials_class => DropboxCredentials do | |
# Filter invoked as soon as a conversation is initialized with this dialect | |
converse do |credentials| | |
@session = DropboxSession.new(credentials.consumer_token.key, credentials.consumer_token.secret) | |
@session.set_access_token(credentials.access_token.key, credentials.access_token.secret) | |
@client = DropboxClient.new(@session, :dropbox) | |
end | |
# Filter should return boolean in response to determine | |
# if we really have access | |
authenticate do | |
@session.authorized? | |
end | |
# If the dialect does not respond via a responds_to | |
# handle it here. Could be used for dynamic responders | |
default do |path, marker, &block| | |
response = @client.metadata(path, 2500, true, marker) | |
entries = ([response] + response['contents']).collect do |source_entry| | |
Pidgin::Entry.new({ | |
:path => source_entry['path'], | |
:size => source_entry['bytes'], | |
:revision => source_entry['rev'], | |
:thumbnail => source_entry['thumb_exists'], | |
:modified_at => Time.parse(source_entry['modified']), | |
:mime_type => source_entry['mime_type'], | |
:folder => source_entry['is_dir'], | |
:deleted => source_entry['is_deleted'] | |
}) | |
end | |
block.call(entries, marker) | |
end | |
end |
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
# Pseudo example of facebook | |
dialect :facebook, :credentials_class => FacebookCredentials do | |
converse do |creds| | |
# Setup code | |
end | |
before do | |
# Something you might need to do before each call | |
end | |
responds_to '/Photos/Tagged/Me' do | |
# Facebook code to get photos tagged with access token owner | |
end | |
responds_to '/Photos/Uploaded' do | |
# Get photos uploaded by me | |
end | |
end |
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
# If a provider requires authentication you'll have to satisfy the what attributes it needs or it will warn or raise an exception | |
# The dropbox dialect knows to look for the the consumer token in the config/pidgin.yml | |
conversation = Pidgin::Conversation.new(:dropbox, :credentials => {:access_key => 'xxx', :access_secret => 'xxx'}) | |
# Everything happens concurrently, and batches of entries get passed to the block | |
conversation.say('/Photos/rooftop') do |entries| | |
entries.each{|e| p e } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment