Skip to content

Instantly share code, notes, and snippets.

@jpmckinney
Created February 10, 2011 23:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpmckinney/821597 to your computer and use it in GitHub Desktop.
Save jpmckinney/821597 to your computer and use it in GitHub Desktop.
DokDok API access with HTTParty
# blog post: http://blog.slashpoundbang.com/post/3230523402/use-the-dokdok-api-from-ruby
require 'base64'
require 'openssl'
require 'httparty'
class DokDok
include HTTParty
base_uri 'api.dokdok.com'
attr_reader :consumer_key, :consumer_secret
def initialize(consumer_key, consumer_secret)
@consumer_key = consumer_key
@consumer_secret = consumer_secret
end
def escape(string)
# reserved character regexp, per http://oauth.net/core/1.0/#rfc.section.5.1
URI.escape(string.to_s, /[^a-zA-Z0-9\-\.\_\~]/)
end
def get(method, params = {})
path = "/1.0/#{method}.json"
uri = "#{self.class.base_uri}#{path}"
params.merge!({
:oauth_consumer_key => consumer_key,
:oauth_nonce => Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, ''), # @see OAuth::Helper.generate_key
:oauth_signature_method => 'HMAC-SHA1',
:oauth_timestamp => Time.now.to_i.to_s,
:oauth_version => '1.0',
})
# http://oauth.net/core/1.0/#rfc.section.9.1.1
query_string = params.sort_by{|x| x[0] }.map{ |k,v| "#{escape(k)}=#{escape(v)}" }.join('&')
base_string = ['GET', uri, query_string].map{ |string| escape(string) }.join('&')
oauth_signature = Base64.encode64(OpenSSL::HMAC.digest('sha1', "#{consumer_secret}&", base_string)).chomp
self.class.get("#{path}?#{query_string}&oauth_signature=#{escape(oauth_signature)}")
end
end
# example usage
DokDok.new('CONSUMER_KEY', 'CONSUMER_SECRET').get('imap/discover', :email => 'example@example.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment