Sha256: ed67bdcaf33c8c6ba8fe71d154b8a164d17fff4fb0f08a62d5e6382201cf4192

Contents?: true

Size: 1.81 KB

Versions: 6

Compression:

Stored size: 1.81 KB

Contents

module Dailycred
  module Helpers

    # use as a before_filter to only allow signed in users
    # example:
    #   before_filter :authenticate
    def authenticate
      redirect_to_unauth unless current_user
    end

    # helper method for getting an instance of dailycred
    # example:
    #   dailycred.tagUser "user_id", "tag"
    #
    # for more documentation, visit https://www.dailycred.com/api/ruby
    def dailycred
      config = Rails.configuration
      @dailycred ||= Dailycred::Client.new(config.DAILYCRED_CLIENT_ID, config.DAILYCRED_SECRET_KEY, config.DAILYCRED_OPTIONS)
    end

    # when making oauth calls, we may need to redirect to our oauth callback url
    # make sure we have the correct state passed back and forth
    def set_state
      @state = session["omniauth.state"] = SecureRandom.hex(24)
    end

    def login_path(params={})
      connect_path params
    end

    def connect_path(params={})
      url = "#{request.protocol}#{request.host_with_port}/auth/dailycred"
      params_array = []
      params.each do |k,v|
        params_array << "#{k}=#{v.to_s}"
      end
      url += "?" if params_array.size > 0
      url += params_array.join("&")
    end

    def connect_user provider, user=nil
      if user.nil?
        user = current_user
      end
      connect_path(:access_token => user.token, :identity_provider => provider)
    end

    def redirect_to_auth opts={}
      p 'redirecting to auth'
      conf = Rails.configuration.DAILYCRED_OPTIONS
      path = !conf[:after_auth].nil? ? conf[:after_auth] : dailycred_engine.auth_info_path
      redirect_to path, opts
    end

    def redirect_to_unauth opts = {}
      conf = Rails.configuration.DAILYCRED_OPTIONS
      path = !conf[:after_unauth].nil? ? conf[:after_unauth] : dailycred_engine.auth_info_path
      redirect_to path, opts
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dailycred-0.3.0 lib/dailycred/helper.rb
dailycred-0.2.0 lib/dailycred/helper.rb
dailycred-0.1.8 lib/dailycred/helper.rb
dailycred-0.1.7 lib/dailycred/helper.rb
dailycred-0.1.6 lib/dailycred/helper.rb
dailycred-0.1.5 lib/dailycred/helper.rb