Class: Livefyre::User
- Inherits:
-
Object
- Object
- Livefyre::User
- Defined in:
- lib/livefyre/user.rb
Overview
Public: Interface for dealing with Livefyre users by User ID.
Instance Attribute Summary collapse
-
#display_name ⇒ Object
Returns the value of attribute display_name.
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.get_user(userish, client) ⇒ Object
Public: Fetch a Livefyre::User from a user record or ID.
-
.get_user_id(userish) ⇒ Object
Public: Coerce a string or [User] into a user ID.
-
.refresh(id) ⇒ Object
Public: Convenience method to refresh a user by ID.
Instance Method Summary collapse
-
#client=(client) ⇒ Object
Public: Setter for the client to associate with this user.
-
#follow_conversation(conversation) ⇒ Object
Public: Follow the given conversation.
-
#initialize(id, client = nil, display_name = nil, args = {}) ⇒ User
constructor
Public: Create a new Livefyre User proxy.
-
#jid ⇒ Object
Internal - Fetch an internal Jabber-style ID for this user.
-
#profile ⇒ Object
Public: Retrieve user information and recent comments for this user from Livefyre.
-
#push(data) ⇒ Object
Public: Update this user’s profile on Livefyre.
-
#refresh ⇒ Object
Public: Invoke Livefyre ping-to-pull to refresh this user’s data.
-
#to_s ⇒ Object
Internal: Returns a cleaner string representation of this object.
-
#token(max_age = 86400) ⇒ Object
Public: Creates a signed JWT token for this user.
-
#unfollow_conversation(conversation) ⇒ Object
Public: Unfollow the given conversation.
Constructor Details
#initialize(id, client = nil, display_name = nil, args = {}) ⇒ User
Public: Create a new Livefyre User proxy.
id - [String] ID of the user to proxy client - [Livefyre::Client] an instance of Livefyre::Client. If nil, the default client is used. display_name - [String] 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
#display_name ⇒ Object
Returns the value of attribute display_name.
4 5 6 |
# File 'lib/livefyre/user.rb', line 4 def display_name @display_name end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/livefyre/user.rb', line 4 def id @id end |
Class Method Details
.get_user(userish, client) ⇒ Object
Public: Fetch a Livefyre::User from a user record or ID
userish - [String/User/Int] A User or user ID client - [Livefyre::Client] Client to bind to the User record
Returns [User]
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 |
.get_user_id(userish) ⇒ Object
Public: Coerce a string or [User] into a user ID
userish - [String/User/Int]A [User] or user ID
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 |
.refresh(id) ⇒ Object
Public: Convenience method to refresh a user by ID
id - A Livefyre user ID to refresh
Returns [Bool] true on success Raises [APIException] if the request failed
135 136 137 |
# File 'lib/livefyre/user.rb', line 135 def self.refresh(id) new(id).refresh end |
Instance Method Details
#client=(client) ⇒ Object
Public: 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 |
#follow_conversation(conversation) ⇒ Object
Public: Follow the given conversation
conversation - [Conversation] to follow Returns [Boolean] true on success Raises [APIException] on failure
97 98 99 |
# File 'lib/livefyre/user.rb', line 97 def follow_conversation(conversation) conversation.follow_as self end |
#jid ⇒ Object
Internal - Fetch an internal Jabber-style ID for this user
Returns [String] representation of this user
44 45 46 |
# File 'lib/livefyre/user.rb', line 44 def jid "#{id}@#{@client.host}" end |
#profile ⇒ Object
Public: Retrieve user information and recent comments for this user from Livefyre
Returns [Hash] of profile data Raises [JSON::ParserError] if the returned data cannot be parsed Raises [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.}") end else raise APIException.new(result.body) end end |
#push(data) ⇒ Object
Public: Update this user’s profile on Livefyre
data - [Hash] A hash of user data as defined by the Livefyre user profile schema
Returns [Bool] true on success Raises [APIException] if the request failed
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 |
#refresh ⇒ Object
Public: Invoke Livefyre ping-to-pull to refresh this user’s data
Returns [Bool] true on success Raises [APIException] if the request failed
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 |
#to_s ⇒ Object
Internal: Returns a cleaner string representation of this object
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 |
#token(max_age = 86400) ⇒ Object
Public: Creates a signed JWT token for this user
max_age - [Integer] Expiry time for this token in seconds (default: 86400)
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 |
#unfollow_conversation(conversation) ⇒ Object
Public: Unfollow the given conversation
conversation - [Conversation] to unfollow Returns [Boolean] true on success Raises [APIException] on failure
106 107 108 |
# File 'lib/livefyre/user.rb', line 106 def unfollow_conversation(conversation) conversation.unfollow_as self end |