Class: Livefyre::Conversation

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

Overview

Proxy object for a Livefyre [Conversation] (also called a Collection)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Conversation) initialize(id, article_id)

A new instance of Conversation



5
6
7
8
9
# File 'lib/livefyre/conversation.rb', line 5

def initialize(id, article_id)
  @id = id
  @article_id = article_id
  @client = Livefyre.client
end

Instance Attribute Details

- (Object) article_id



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

def article_id
  @article_id
end

- (Object) id



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

def id
  @id
end

Class Method Details

+ (String) collectionMeta(client, article_id, title, link, tags)

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.

Generate a signed collectionMeta

Parameters:

  • client (Client)

    identifying the site to create the collection on

  • title (String)

    Article title

  • link (String)

    Article link

  • tags (String, Array)

    Article tags

Returns:

  • (String)

    Returns String signed token



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/livefyre/conversation.rb', line 105

def self.collectionMeta(client, article_id, title, link, tags)
  tag_str = case tags
  when Array
    tags.join ","
  when String
    tags
  when nil
    nil
  else
    raise "Invalid value given for tags: must be Array, String, or nil"
  end

  begin
    URI.parse(link)
  rescue URI::InvalidURIError => e
    raise "Invalid value for link: #{e.message}"
  end

   = {
    :title => title,
    :url   => link,
    :articleId => article_id,
    :tags  => tag_str || "",
  }

  JWT.encode(, client.site_key)
end

+ (Conversation) create(client, article_id, title, link, tags = nil)

Create a new collection

Parameters:

  • client (Client)

    identifying the site to create the collection on

  • article_id (String)

    ID to use to identify this article

  • title (String)

    Article title

  • link (String)

    Article link

  • tags (String, Array) (defaults to: nil)

    Article tags

Returns:



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/livefyre/conversation.rb', line 85

def self.create(client, article_id, title, link, tags = nil)
  meta = collectionMeta(client, article_id, title, link, tags)
  response = client.quill.post "/api/v3.0/site/#{client.options[:site_id]}/collection/create", {:collectionMeta => meta, :articleId => article_id}.to_json
  if response.success?
    body = JSON.parse(response.body)
    Conversation.new(body["data"]["collectionId"], article_id)
  else
    error = JSON.parse(response.body)
    raise APIException.new(error["msg"])
  end
end

Instance Method Details

- (Object) comments

TODO:

Not currently working.

Fetch a list of comments from a conversation



13
14
15
16
17
18
19
20
# File 'lib/livefyre/conversation.rb', line 13

def comments
  response = @client.bootstrap.get "/bs3/#{@client.options[:domain]}/#{@client.options[:network]}/#{@client.options[:site_id]}/#{Base64.encode64 @article_id}/init"
  if response.success?
    JSON.parse response.body
  else
    raise APIException.new(response.body)
  end
end

- (Comment) create_comment(user, body)

Create a comment on this conversation

Parameters:

  • user (User)

    to create the comment as

  • body (String)

    body of the content

Returns:



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

def create_comment(user, body)
  Comment.create(@client, user, self, body)
end

- (Boolean) follow_as(user)

Follow this conversation as the passed user

Parameters:

  • user (User)

    to follow the conversation as

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



52
53
54
55
56
57
58
59
# File 'lib/livefyre/conversation.rb', line 52

def follow_as(user)
  response = @client.quill.post "/api/v3.0/collection/10584292/follow/", :lftoken => user.token, :collectionId => @id
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Boolean) unfollow_as(user)

Unfollow this conversation as the passed user

Parameters:

  • user (User)

    to unfollow the conversation as

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



67
68
69
70
71
72
73
74
# File 'lib/livefyre/conversation.rb', line 67

def unfollow_as(user)
  response = @client.quill.post "/api/v3.0/collection/10584292/unfollow/", :lftoken => user.token, :collectionId => @id
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Bool) update(title, link, tags = nil)

Update this collection with new metadata

Returns:

  • (Bool)

    Returns Bool true on success

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/livefyre/conversation.rb', line 26

def update(title, link, tags = nil)
  meta = self.class.collectionMeta(@client, @article_id, title, link, tags)
  response = @client.quill.post "/api/v3.0/site/#{@client.options[:site_id]}/collection/update/", {:collectionMeta => meta, :articleId => @article_id}.to_json
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end