Skip to content
This repository has been archived by the owner. It is now read-only.
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

UNMAINTAINED -- FatSecret OmniAuth Gem v0.0.2

First things first - This is NOT an OmniAuth Strategy. This is not meant to be used as a Login method for web apps. This gem leverages OmniAuth's OAuth Strategy to obtain auth tokens and auth secrets, and to make calls to the FatSecret REST API.


Installing

  • Add the gem to your Gemfile:
gem 'fatsecret-omniauth'
  • Run bundle install

Authentication

  • Add the following to config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :fatsecret, 'consumer_key', 'consumer_secret'
end

You will need to sign up for a Fatsecret API account. You'll be issued your own consumer_key and consumer_secret, which you should use in the code above.

  • Add your callback route in config/routes.rb:
get '/auth/fatsecret/callback', to: 'YOUR_CONTROLLER#CREATE_METHOD'

NOTE: OmniAuth strategies use get '/auth/:provider/callback', to: 'sessions#create'. The FatSecret OmniAuth gem requires a custom controller#create method to save the auth tokens in a database for future API calls. Also, you should place your fatsecret-omniauth route above your other OmniAuth routes. Otherwise, your API calls will be routed to sessions#create.

  • FOR EXAMPLE: If you had an ApiTokensController, you could use the following route:

    get '/auth/fatsecret/callback', to: 'api_tokens#create'

To authenticate with FatSecret, and obtain auth tokens:

  • Load /auth/fatsecret in your browser

  • You'll be redirected to FatSecret.com to authenticate

  • If you agree to authenticate, you'll be redirected back /auth/fatsecret/callback

  • The request.env['omniauth.auth'] hash will contain your FatSecret data.

    • FatSecret profile data is contained in:
      request.env['omniauth.auth']['info'] -- see the FatSecret profile.get page for more details.

    • FatSecret auth token and auth secret are contained in:
      request.env['omniauth.auth']['credentials']

  • Save the auth tokens to make future API calls for user data


API calls

API calls are created with the Fatsecret::Api.new({}).api_call() method. FatSecret consumer keys and a params hash containing the api call are required. Optionally, you can include an auth_key and auth_secret for authenticated calls.

The structure of an api call looks like this:

Fatsecret::Api.new({}).api_call(
  'CONSUMER_KEY',
  'CONSUMER_SECRET', 
  params
)

The structure of an authenticated api call looks like this:

Fatsecret::Api.new({}).api_call(
  'CONSUMER_KEY',
  'CONSUMER_SECRET', 
  params, 
  'auth_token',
  'auth_secret'
)

The actual API request data is contained in the params hash. Read the FatSecret REST API method docs and supply the required parameters in the params hash.

NOTE The following required parameters are automatically handled by the gem: oauth_signature_method, oauth_timestamp, oauth_nonce, oauth_version and oauth_signature. Also, since consumer keys are required with every API call, oauth_consumer_key is taken care of as well.

As an example, if you wanted to call the foods.search API method, the first thing you would do is look up foods.search in the FatSecret API docs and find the required parameters. Aside from the automatic parameters listed above, the only required parameter is method, and it must be set to "foods.search".

There are also several optional parameters: format, oauth_token, search_expression, page_number, and max_results. You can include any of these as well. The search_expression would contain the food(s) you're searching for.

If you had an apis_controller with a foods_search method, it might look like this:

class ApisController < ApplicationController
  def foods_search
    params['method'] = 'foods.search'
    request = Fatsecret::Api.new({}).api_call(
      'CONSUMER_KEY',
      'CONSUMER_SECRET', 
      params
    )   
    @response = request.body
  end
end

Now you can create a form with a 'search_expression' field that submits to the foods_search method of the ApisController, and the FatSecret API will return your results in @response.

  • FOR EXAMPLE:
    Assuming the ApisController mentioned above, and the following custom route:

    post '/foods_search', to: 'apis#food_search'

    You could create the form below to allow users to search foods on FatSecret.com.

<%= form_tag foods_search_path %>
  <%= label_tag(:search_expression, "Search for food:") %>
  <%= text_field_tag(:search_expression) %>
  <%= submit_tag("Search") %>
<% end %> 

Copyright (c) 2013 Scott McGrath. See LICENSE for details.

About

UNMAINTAINED -- OmniAuth authentication + API wrapper for the FatSecret.com OAuth API.

Resources

License

Releases

No releases published

Packages

No packages published

Languages

You can’t perform that action at this time.