Class: Livefyre::Client

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

Overview

Primary interface to the Livefyre API

Constant Summary

ROLES =

Valid roles for #set_user_role

%w(admin member none outcast owner)
SCOPES =

Valid scopes for #set_user_role

%w(domain site conv)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(options = {})

Create a new Livefyre client.

Parameters:

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

    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

- (Object) bootstrap



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

def bootstrap
  @bootstrap
end

- (Object) host



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

def host
  @host
end

- (Object) http_client



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

def http_client
  @http_client
end

- (Object) key



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

def key
  @key
end

- (Object) options



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

def options
  @options
end

- (Object) quill



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

def quill
  @quill
end

- (Object) search



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

def search
  @search
end

- (Object) site_key



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

def site_key
  @site_key
end

- (Object) stream



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

def stream
  @stream
end

- (Object) system_token



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

def system_token
  @system_token
end

Instance Method Details

- (Object) identifier

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.

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

- (String) jid(id)

Transform the given ID into a jid

Parameters:

  • id

    a string value to compose the JID with

Returns:

  • (String)

    Returns String JID



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

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

- (Bool) set_user_role(user_id, role, scope = 'domain', scope_id = nil)

Sets a user's role (affiliation) in a given scope.

Examples:

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

Parameters:

  • user_id

    The user ID (without the host) to set roles for

  • role

    The role to set.

  • scope (defaults to: 'domain')

    The scope for which to set this role.

  • scope_id (defaults to: nil)

    In the case that the given scope requires identification, specifies which scope to operate on.

Returns:

  • (Bool)

    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

- (String) sign(data)

Sign a data structure with this client's network key.

Returns:

  • (String)

    Returns String A signed JWT token



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

def sign(data)
  JWT.encode(data, @key)
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



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

- (Livefyre::User) user(uid, display_name = nil)

Create a User with this client's credentials.

Parameters:

  • 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 (defaults to: nil)

    the displayed name for this user. Optional.

Returns:



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

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

- (Hash) validate(data)

Validates and decodes a JWT token

Returns:

  • (Hash)

    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