Class: Livefyre::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/livefyre/client.rb

Overview

Public: Primary interface to the Livefyre API

Constant Summary collapse

ROLES =

Public: Valid roles for #set_user_role

%w(admin member none outcast owner)
SCOPES =

Public: Valid scopes for #set_user_role

%w(domain site conv)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Public: Create a new Livefyre client.

options - [Hash] array of options to pass to the client for initialization :host - your Livefyre network_host :key - your Livefyre network_key :system_token - your Livefyre long-lived system user key



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/livefyre/client.rb', line 21

def initialize(options = {})
  @options = options.clone
  @host = options.delete(:network) || options.delete(:host)
  raise "Invalid host" if @host.nil?
  @http_client = Faraday.new(:url => "http://#{@host}")
  @quill = Faraday.new(:url => "http://quill.#{@host}")
  @stream = Faraday.new(:url => "http://stream.#{@host}")
  @search = Faraday.new(:url => "http://search.#{@host}")
  @bootstrap = Faraday.new(:url => "http://bootstrap.#{@host}")
  @site_key = options[:site_key]

  @key = options.delete(:secret) || options.delete(:key) || options.delete(:network_key)
  raise "Invalid secret key" if @key.nil?

  @system_token = options.delete(:system_token)
  raise "Invalid system token" if @system_token.nil?
end

Instance Attribute Details

#bootstrapObject

Returns the value of attribute bootstrap.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def bootstrap
  @bootstrap
end

#hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def host
  @host
end

#http_clientObject

Returns the value of attribute http_client.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def http_client
  @http_client
end

#keyObject

Returns the value of attribute key.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def key
  @key
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def options
  @options
end

#quillObject

Returns the value of attribute quill.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def quill
  @quill
end

#searchObject

Returns the value of attribute search.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def search
  @search
end

#site_keyObject

Returns the value of attribute site_key.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def site_key
  @site_key
end

#streamObject

Returns the value of attribute stream.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def stream
  @stream
end

#system_tokenObject

Returns the value of attribute system_token.



11
12
13
# File 'lib/livefyre/client.rb', line 11

def system_token
  @system_token
end

Instance Method Details

#identifierObject

Internal: Identifier to use to uniquely identify this client.

Returns string ID



118
119
120
# File 'lib/livefyre/client.rb', line 118

def identifier
  @identifier ||= "RubyLib-#{Process.pid}-#{local_ip}-#{object_id}"
end

#jid(id) ⇒ Object

Public: Transform the given ID into a jid

id - a string value to compose the JID with

Returns [String] JID



111
112
113
# File 'lib/livefyre/client.rb', line 111

def jid(id)
  "%s@%s" % [id, host]
end

#set_user_role(user_id, role, scope = 'domain', scope_id = nil) ⇒ Object

Public: Sets a user’s role (affiliation) in a given scope.

user_id - The user ID (without the host) to set roles for role - The role to set. scope - The scope for which to set this role. scope_id - In the case that the given scope requires identification, specifies which scope to operate on.

Examples

set_user_role(1234, "owner", "domain")
set_user_role(1234, "moderator", "site", site_id)
set_user_role(1234, "moderator", "conv", conversation_id)

Returns [Bool] true on success Raises APIException if the request failed



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/livefyre/client.rb', line 80

def set_user_role(user_id, role, scope = 'domain', scope_id = nil)
  raise "Invalid scope" unless SCOPES.include? scope
  raise "Invalid role" unless ROLES.include? role

  post_data = {
    :affiliation => role,
    :lftoken => system_token,
  }
  case scope
  when "domain"
    post_data[:domain_wide] = 1
  when "conv"
    raise "Invalid scope_id" if scope_id.nil?
    post_data[:conv_id] = scope_id
  when "site"
    raise "Invalid scope_id" if scope_id.nil?
    post_data[:site_id] = scope_id
  end
  result = post "/api/v1.1/private/management/user/#{jid(user_id)}/role/", post_data
  if result.success?
    true
  else
    raise APIException.new(result.body)
  end
end

#sign(data) ⇒ Object

Public: Sign a data structure with this client’s network key.

Returns [String] A signed JWT token



42
43
44
# File 'lib/livefyre/client.rb', line 42

def sign(data)
  JWT.encode(data, @key)
end

#to_sObject

Internal: Returns a cleaner string representation of this object

Returns [String] representation of this class



125
126
127
# File 'lib/livefyre/client.rb', line 125

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

#user(uid, display_name = nil) ⇒ Object

Public: Create a User with this client’s credentials.

uid - the user ID to create a Livefyre user for. This should be the ID used to reference this user in Livefyre’s system. display_name - the displayed name for this user. Optional.

Returns [Livefyre::User]



60
61
62
# File 'lib/livefyre/client.rb', line 60

def user(uid, display_name = nil)
  User.new(uid, self, display_name)
end

#validate(data) ⇒ Object

Public: Validates and decodes a JWT token

Returns [Hash] A hash of data passed from the token Raises [JWT::DecodeError] if invalid token contents or signature



50
51
52
# File 'lib/livefyre/client.rb', line 50

def validate(data)
  JWT.decode(data, @key)
end