Alex V
Alex V

Reputation: 18306

Ruby on Rails - Session in Model for External API

I have an app without models or any database connectivity.

I have a method defined in the ApplicationController called "api_call" which does all api calls within the app. This method uses the ruby session to store things about the user, such as authentication info, access token info, and user info. In the session I store an Authentication hash for sending to the api for security when the user is logged.

Two things:

  1. I'd like to put all api calls (the api_call method) in models (that don't use db or validation), but the problem is I don't have access session.
  2. If I use a module, the module in the model also doesn't have access to the session.
  3. If I create a model class without using ActiveRecord, should I use class methods rather than object methods?

Upvotes: 1

Views: 589

Answers (1)

Jared Beck
Jared Beck

Reputation: 17528

How about passing the "authentication hash" to the API model's constructor?

class Api
  def initialize auth
    @auth = auth
  end
end

class FooController < ApplicationController
  def index
    api = Api.new session[:auth]
  end
end

Also, if you haven't see Pratik Naik's article about this, it's pretty funny.

Upvotes: 1

Related Questions