Skip to content

Instantly share code, notes, and snippets.

@nofxx
Created May 20, 2010 10:02
Show Gist options
  • Save nofxx/407418 to your computer and use it in GitHub Desktop.
Save nofxx/407418 to your computer and use it in GitHub Desktop.
class DeliciousToken < ConsumerToken
DELICIOUS_SETTINGS = {
:site => 'https://api.login.yahoo.com',
:http_method => :get,
:scheme => :query_string,
:request_token_path => '/oauth/v2/get_request_token',
:access_token_path => '/oauth/v2/get_token',
:authorize_path => '/oauth/v2/request_auth',
}
include OAuth::Helper
def self.consumer
@consumer ||= OAuth::Consumer.new credentials[:key], credentials[:secret], DELICIOUS_SETTINGS
end
def self.delicious_consumer
@client ||= OAuth::Consumer.new(credentials[:key],
credentials[:secret],
{:site => 'http://api.del.icio.us',
:scheme => :header,
#:scheme => :query_string,
:http_method => :get,
:realm => 'yahooapis.com'})
end
def client
@client ||= OAuth::AccessToken.new(DeliciousToken.delicious_consumer, token, secret)
end
def add(url, tags, desc=nil, full=nil)
posts_add!(:url => url, :tags => tags)
end
def get(path, parameters = {})
query = parameters.map { |k, v| "#{escape(k.to_s)}=#{escape(v)}" } * '&'
components = [path]
components << query unless query.empty?
url = components * '?'
client.get(url)
end
def posts_add!(parameters)
response = get('/v2/posts/add', parameters)
unless response.is_a?(Net::HTTPOK)
raise "HTTP response code: #{response.code} #{response.body}"
end
matches = Regexp.new('<result code="([^\"]*)" />').match(response.body)
unless matches && matches[1] == 'done'
raise "Delicious API code: '#{matches[1]}'"
end
response
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment