Sha256: 62c668d7a841b63d048b7cd6bb38c4d509f04cd979f2680c8a4a46e20ac90b7a

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

class CityGrid
  class Session
    class APIAccessor
      attr_accessor :session
      
      def initialize session
        self.session = session
      end
      
      def content klass
      end
      
      def ad_center klass
      end
    end
    
    attr_accessor :username, :auth_token, :logged_in, :api
    
    def initialize
      self.logged_in = false
      self.api = {:ad_center => 1, :content => 2}
    end
    
    def logged_in?
      self.logged_in == true
    end
    
    def login username, password
      res = CityGrid::API::Accounts::Authentication.login :username => username, :password => password
      self.username = username

      case res.code
      when 201
        self.auth_token = res.authToken
        self.logged_in = true
        # puts "logged in ok"
      when 400
        # puts "error"
      else
        # puts "unexpected response code"  
      end
    end
    
    def call_api klass, action, options = {}
      klass.send action, options.merge(:token => self.auth_token)
    end
    
    class << self
      def login username, password
        session = Session.new
        session.login username, password
        
        session
      end
      
      def from_auth_token auth_token
        session = Session.new
        session.auth_token = auth_token
        session.logged_in = true
        
        session
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
citygrid_api-0.0.6 lib/citygrid/session.rb