Class: Livefyre::User

Inherits:
Object
  • Object
show all
Defined in:
lib/livefyre/user.rb

Overview

Interface for dealing with Livefyre users by User ID.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (User) initialize(id, client = nil, display_name = nil, args = {})

Create a new Livefyre User proxy.

Parameters:

  • id (String)

    ID of the user to proxy

  • client (Livefyre::Client) (defaults to: nil)

    an instance of Livefyre::Client. If nil, the default client is used.

  • display_name (String) (defaults to: nil)

    The display name for this user (optional)



11
12
13
14
15
16
# File 'lib/livefyre/user.rb', line 11

def initialize(id, client = nil, display_name = nil, args = {})
  @id = id
  @client = client || Livefyre.client
  @display_name = display_name
  @options = args
end

Instance Attribute Details

- (Object) display_name



4
5
6
# File 'lib/livefyre/user.rb', line 4

def display_name
  @display_name
end

- (Object) id



4
5
6
# File 'lib/livefyre/user.rb', line 4

def id
  @id
end

Class Method Details

+ (User) get_user(userish, client)

Fetch a Livefyre::User from a user record or ID

Parameters:

  • userish (String/User/Int)

    A User or user ID

  • client (Livefyre::Client)

    Client to bind to the User record

Returns:

  • (User)

    Returns



145
146
147
148
149
150
151
152
153
# File 'lib/livefyre/user.rb', line 145

def self.get_user(userish, client)
  case userish
  when User
    userish.client = client
    userish
  else
    new get_user_id(userish), client
  end
end

+ (String) get_user_id(userish)

Coerce a string or [User] into a user ID

Parameters:

  • userish (String/User/Int)

    A [User] or user ID

Returns:

  • (String)

    Returns String User ID

Raises:

  • Exception when value can't be coerced



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/livefyre/user.rb', line 116

def self.get_user_id(userish)
  case userish
  when String
    userish.split("@", 2).first
  when Fixnum
    userish
  when User
    userish.id
  else
    raise "Invalid user ID"
  end
end

+ (Bool) refresh(id)

Convenience method to refresh a user by ID

Parameters:

  • id

    A Livefyre user ID to refresh

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



135
136
137
# File 'lib/livefyre/user.rb', line 135

def self.refresh(id)
  new(id).refresh
end

Instance Method Details

- (Object) client=(client)

Setter for the client to associate with this user



37
38
39
# File 'lib/livefyre/user.rb', line 37

def client=(client)
  @client = client
end

- (Boolean) follow_conversation(conversation)

Follow the given conversation

Parameters:

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



97
98
99
# File 'lib/livefyre/user.rb', line 97

def follow_conversation(conversation)
  conversation.follow_as self
end

- (String) jid

Internal - Fetch an internal Jabber-style ID for this user

Returns:

  • (String)

    Returns String representation of this user



44
45
46
# File 'lib/livefyre/user.rb', line 44

def jid
  "#{id}@#{@client.host}"
end

- (Hash) profile

Retrieve user information and recent comments for this user from Livefyre

Returns:

  • (Hash)

    Returns Hash of profile data

Raises:

  • (JSON::ParserError)

    if the returned data cannot be parsed

  • (APIException)

    if the API does not return a valid response



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/livefyre/user.rb', line 23

def profile
  response = @client.get "/profile/#{id}/", {:actor_token => token}
  if response.success?
    begin
      JSON.parse(response.body)["data"]
    rescue JSON::ParserError => e
      raise APIException.new("Parse error: #{e.message}")
    end
  else
    raise APIException.new(result.body)
  end
end

- (Bool) push(data)

Update this user's profile on Livefyre

Parameters:

  • data (Hash)

    A hash of user data as defined by the Livefyre user profile schema

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



70
71
72
73
74
75
76
77
# File 'lib/livefyre/user.rb', line 70

def push(data)
  result = @client.post "/profiles/?actor_token=#{CGI.escape @client.system_token}&id=#{id}", {:data => data.to_json}
  if result.success?
    true
  else
    raise APIException.new(result.body)
  end
end

- (Bool) refresh

Invoke Livefyre ping-to-pull to refresh this user's data

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



83
84
85
86
87
88
89
90
# File 'lib/livefyre/user.rb', line 83

def refresh
  result = @client.post "/api/v3_0/user/#{id}/refresh", {:lftoken => @client.system_token}
  if result.success?
    true
  else
    raise APIException.new(result.body)
  end
end

- (String) to_s

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a cleaner string representation of this object

Returns:

  • (String)

    Returns String representation of this class



158
159
160
# File 'lib/livefyre/user.rb', line 158

def to_s
  "#<#{self.class.name}:0x#{object_id.to_s(16).rjust(14, "0")} id='#{id}' display_name='#{display_name}'>"
end

- (String) token(max_age = 86400)

Creates a signed JWT token for this user

Parameters:

  • max_age (Integer) (defaults to: 86400)

    Expiry time for this token in seconds (default: 86400)

Returns:

  • (String)

    Returns String token



53
54
55
56
57
58
59
60
61
62
# File 'lib/livefyre/user.rb', line 53

def token(max_age = 86400)
  data = {
    :domain => @client.host,
    :user_id => id,
    :expires => Time.now.to_i + max_age
  }.tap do |opts|
    opts[:display_name] = @display_name unless @display_name.nil?
  end
  JWT.encode(data, @client.key)
end

- (Boolean) unfollow_conversation(conversation)

Unfollow the given conversation

Parameters:

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



106
107
108
# File 'lib/livefyre/user.rb', line 106

def unfollow_conversation(conversation)
  conversation.unfollow_as self
end