Class: Livefyre::Comment

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

Overview

Proxy object for a [Comment] on a [Livefyre::Conversation]

Returns:

Constant Summary

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Comment) initialize(id, conversation, options = {})

A new instance of Comment



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/livefyre/comment.rb', line 14

def initialize(id, conversation, options = {})
  @id           = id
  @body         = options[:body]
  @user         = options[:user]
  @parent_id    = options[:parent_id]
  @ip           = options[:author_ip]
  @conversation = conversation
  @created_at   = options[:created_at]
  @client       = options[:client] || Livefyre.client
  @options      = options
end

Instance Attribute Details

- (Object) body



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def body
  @body
end

- (Object) conversation



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def conversation
  @conversation
end

- (Object) created_at



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def created_at
  @created_at
end

- (Object) id



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def id
  @id
end

- (Object) ip



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def ip
  @ip
end

- (Object) parent_id



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def parent_id
  @parent_id
end

- (Object) user



13
14
15
# File 'lib/livefyre/comment.rb', line 13

def user
  @user
end

Class Method Details

+ (Comment) create(client, user, conversation, body, reply_to = nil)

create a new comment on a conversation

Parameters:

  • client (Client)

    representing the site to use when creating the conversation

  • user (User)

    to create the comment as

  • conversation (Conversation)

    to create

  • body (String)

    Comment body

Returns:

Raises:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/livefyre/comment.rb', line 159

def self.create(client, user, conversation, body, reply_to = nil)
  response = client.quill.post "/api/v3.0/collection/#{conversation.id}/post/", {:lftoken => user.token, :body => body, :_bi => client.identifier, :parent_id => reply_to}
  if response.success?
    puts JSON.parse(response.body).inspect
    data = JSON.parse(response.body)["data"]

    data["messages"].map do |entry|
      c = entry["content"]
      Comment.new(c["id"], conversation, {
        :body         => c["bodyHtml"],
        :parent_id    => c["parentId"],
        :user         => User.new(c["authorId"], data["authors"].first.last["displayName"], data["authors"].first.last),
        :created_at   => Time.at(c["createdAt"]),
        :source       => entry["source"],
        :visibility   => entry["vis"],
        :client       => client,
        :type         => entry["type"]
      })
    end.first
  else
    raise APIException.new(response.body)
  end
end

Instance Method Details

- (Enum<CONTENT_TYPES>) content_type

Get the comment content type as a string.

Currently only populated when created via ::create

Returns:

  • (Enum<CONTENT_TYPES>)

    Returns



112
113
114
# File 'lib/livefyre/comment.rb', line 112

def content_type
  content_type_id ? CONTENT_TYPES[content_type_id] : nil
end

- (Integer) content_type_id

Get the comment visibility as an integer.

Currently only populated when created via ::create

Returns:

  • (Integer)

    Returns



120
121
122
# File 'lib/livefyre/comment.rb', line 120

def content_type_id
  @options[:type]
end

- (Boolean) delete!

Delete this comment

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



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

def delete!
  response = client.quill.post "/api/v3.0/message/#{id}/delete", {:lftoken => @client.system_token}
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Object) flag(reason, notes, email, user = nil)

Flag a comment

Parameters:

  • reason

    one of [disagree, spam, offensive, off-topic]

  • notes

    String containing the reason for the flag

  • email

    email address of the flagger

  • user (User) (defaults to: nil)

    If set, will include the user token for validation of the flag



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/livefyre/comment.rb', line 32

def flag(reason, notes, email, user = nil)
  raise "invalid reason" unless REASONS.include? reason
  payload = {
    :message_id => @id,
    :collection_id => @conversation.id,
    :flag => reason,
    :notes => notes,
    :email => email
  }
  payload[:lftoken] = user.token if user
  response = client.quill.post "/api/v3.0/message/25818122/flag/#{reason}/", payload.to_json
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Boolean) like!(user)

Likes a comment as the passed user

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



128
129
130
131
132
133
134
135
# File 'lib/livefyre/comment.rb', line 128

def like!(user)
  response = @client.quill.post "/api/v3.0/message/#{id}/like/", {:collection_id => conversation.id, :lftoken => user.token}
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Enum<SOURCES>) source

Get the comment source as a string.

Currently only populated when created via ::create

Returns:

  • (Enum<SOURCES>)

    Returns



80
81
82
# File 'lib/livefyre/comment.rb', line 80

def source
  source_id ? SOURCES[source_id] : nil
end

- (Integer) source_id

Get the comment source as an integer.

Currently only populated when created via ::create

Returns:

  • (Integer)

    Returns



88
89
90
# File 'lib/livefyre/comment.rb', line 88

def source_id
  @options[:source]
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



186
187
188
# File 'lib/livefyre/comment.rb', line 186

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

- (Boolean) unlike!(user)

Unlikes a comment as the passed user

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



141
142
143
144
145
146
147
148
# File 'lib/livefyre/comment.rb', line 141

def unlike!(user)
  response = @client.quill.post "/api/v3.0/message/#{id}/unlike/", {:collection_id => conversation.id, :lftoken => user.token}
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Boolean) update(body)

Update this comment's content

Returns:

  • (Boolean)

    Returns Boolean true on success

Raises:



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

def update(body)
  response = client.quill.post "/api/v3.0/message/#{id}/edit", {:lftoken => @client.system_token, :body => body}
  if response.success?
    true
  else
    raise APIException.new(response.body)
  end
end

- (Enum<VISIBILITIES>) visibility

Get the comment visibility as a string.

Currently only populated when created via ::create

Returns:

  • (Enum<VISIBILITIES>)

    Returns



96
97
98
# File 'lib/livefyre/comment.rb', line 96

def visibility
  visibility_id ? VISIBILITIES[visibility_id] : nil
end

- (Integer) visibility_id

Get the comment visibility as an integer.

Currently only populated when created via ::create

Returns:

  • (Integer)

    Returns



104
105
106
# File 'lib/livefyre/comment.rb', line 104

def visibility_id
  @options[:visibility]
end