Skip to content

Instantly share code, notes, and snippets.

@dmix
Created January 3, 2010 17:29
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 dmix/268050 to your computer and use it in GitHub Desktop.
Save dmix/268050 to your computer and use it in GitHub Desktop.
def search_food(query)
base = "http://platform.fatsecret.com/rest/server.api"
key = "339df5xx11e42b3e7x3x1b21e7d12"
#added the extra & at the end of the secret
secret = "215ba9f90c6e4f41b23a149f322d2s7b1&"
timestamp = Time.now.to_i
signature_method="HMAC-SHA1"
nonce = rand(100)
method = "food.search"
#normalizing the required parameters
required_parameters = Addressable::URI.new
required_parameters.query_values = {:oauth_consumer_key => key, :oauth_nonce => nonce.to_s, :oauth_signature_method => signature_method, :oauth_timestamp => timestamp.to_s, :oauth_version => "1.0"}
to_encrypt = "POST&#{CGI.escape(base)}&#{CGI::escape(required_parameters.query)}"
oauth_signature = Base64.encode64(HMAC::SHA1.digest(to_encrypt, secret)).strip
final_url = base + "?method=" + method + "&oauth_signature=" + CGI.escape(oauth_signature) + "&" + required_parameters.query + "&search_expression=" + query.to_s
url = open(final_url).read
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment