Module: Livefyre::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/livefyre/controller_extensions.rb

Overview

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)

Instance Method Details

- (Hash) livefire_profile(user, values = {})

Attempt to generate valid Livefire profile dump from the passed user record by guessing at field names.

Parameters:

  • user

    The user record to generate data from. Assumes it's ActiveModel-ish.

  • values (Hash) (defaults to: {})

    of values to force values for, rather than guessing at.

Returns:

  • (Hash)

    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

- (Object) livefyre_login(id, display_name)

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 (id, display_name)
  cookie = (Livefyre.config[:cookie_options] || {}).clone || {:path => "/", :expires => Time.now + 1.year}
  expiry = cookie.delete(:expires) || (Time.now + 1.year)

  token = {
    :domain  => Livefyre.client.host,
    :user_id => id,
    :expires => expiry.to_i,
    :display_name => display_name
  }

  name = cookie.delete(:name) || "livefyre_utoken"
  cookies[name] = cookie.merge(:value => JWT.encode(token, Livefyre.client.key), :expires => expiry)
end

- (Object) livefyre_logout

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"
  cookies.delete(name)
end

- (Array<Activity>) parse_livefyre_postback

Used in your postback handler to parse the Livefyre postback body into an Activity stream for processing

Returns:

  • (Array<Activity>)

    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

- (Object) validate_livefyre_request!

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