Skip to content

Instantly share code, notes, and snippets.

@tylr
Created May 30, 2012 22:21
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 tylr/2839296 to your computer and use it in GitHub Desktop.
Save tylr/2839296 to your computer and use it in GitHub Desktop.
Pidgin interface prototype
# 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
# 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
# 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