Module: Livefyre::Controller
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/livefyre/controller_extensions.rb
Overview
Public: Controller extensions for Rails. Adds methods to be called from your controller to integrate with Livefyre.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#livefire_profile(user, values = {}) ⇒ Object
Public: Attempt to generate valid Livefire profile dump from the passed user record by guessing at field names.
-
#livefyre_login(id, display_name) ⇒ Object
Public: Creates the Livefyre session cookies.
-
#livefyre_logout ⇒ Object
Public: Destroys the Livefyre session cookies.
-
#parse_livefyre_postback ⇒ Object
Public: Used in your postback handler to parse the Livefyre postback body into an Activity stream for processing.
-
#validate_livefyre_request! ⇒ Object
Public: Check the validity of the JWT that Livefyre sends with pull requests.
Instance Method Details
#livefire_profile(user, values = {}) ⇒ Object
Public: Attempt to generate valid Livefire profile dump from the passed user record by guessing at field names.
user - The user record to generate data from. Assumes it’s ActiveModel-ish. values - [Hash] of values to force values for, rather than guessing at.
Returns [Hash] suitable for conversion to JSON
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/livefyre/controller_extensions.rb', line 34 def livefire_profile(user, values = {}) { :id => user.id, :display_name => user.try(:display_name) || user.try(:name) || user.try(:username), :email => user.try(:email), :profile => url_for(user), :settings_url => url_for(:edit, user), :bio => user.try(:bio) || user.try(:about), :name => { :first_name => user.try(:first_name), :last_name => user.try(:last_name), } }.merge defaults end |
#livefyre_login(id, display_name) ⇒ Object
Public: Creates the Livefyre session cookies. Should be called when the user logs in.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/livefyre/controller_extensions.rb', line 7 def livefyre_login(id, display_name) = (Livefyre.config[:cookie_options] || {}).clone || {:path => "/", :expires => Time.now + 1.year} expiry = .delete(:expires) || (Time.now + 1.year) token = { :domain => Livefyre.client.host, :user_id => id, :expires => expiry.to_i, :display_name => display_name } name = .delete(:name) || "livefyre_utoken" [name] = .merge(:value => JWT.encode(token, Livefyre.client.key), :expires => expiry) end |
#livefyre_logout ⇒ Object
Public: Destroys the Livefyre session cookies. Should be called when the user logs out
23 24 25 26 |
# File 'lib/livefyre/controller_extensions.rb', line 23 def livefyre_logout name = (Livefyre.config[:cookie_options] || {})[:name] || "livefyre_utoken" .delete(name) end |
#parse_livefyre_postback ⇒ Object
Public: Used in your postback handler to parse the Livefyre postback body into an Activity stream for processing
Returns [Array<Activity>] List of activities included in this postback.
62 63 64 |
# File 'lib/livefyre/controller_extensions.rb', line 62 def parse_livefyre_postback JSON.parse(request.body).map {|item| Activity.new(client, item) } end |
#validate_livefyre_request! ⇒ Object
Public: Check the validity of the JWT that Livefyre sends with pull requests.
Raises [JWT::DecodeError] if the token is invalid or missing.
52 53 54 55 56 57 |
# File 'lib/livefyre/controller_extensions.rb', line 52 def validate_livefyre_request! JWT::DecodeError if params[:lftoken].nil? token = JWT.decode params[:lftoken], Livefyre.client.key raise JWT::DecodeError unless token["domain"] == Livefyre.client.host return true end |