Class: PostProxy::Resources::Comments

Inherits:
Object
  • Object
show all
Defined in:
lib/postproxy/resources/comments.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Comments

Returns a new instance of Comments.



4
5
6
# File 'lib/postproxy/resources/comments.rb', line 4

def initialize(client)
  @client = client
end

Instance Method Details

#create(post_id, text, profile_id:, parent_id: nil, idempotency_key: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/postproxy/resources/comments.rb', line 60

def create(post_id, text, profile_id:, parent_id: nil, idempotency_key: nil)
  json_body = { text: text }
  json_body[:parent_id] = parent_id if parent_id

  result = @client.request(:post, "/posts/#{post_id}/comments",
    params: { profile_id: profile_id },
    json: json_body,
    idempotency_key: idempotency_key
  )
  Comment.new(**result)
end

#delete(post_id, comment_id, profile_id:, idempotency_key: nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/postproxy/resources/comments.rb', line 72

def delete(post_id, comment_id, profile_id:, idempotency_key: nil)
  result = @client.request(:delete, "/posts/#{post_id}/comments/#{comment_id}",
    params: { profile_id: profile_id },
    idempotency_key: idempotency_key
  )
  AcceptedResponse.new(**result)
end

#get(post_id, comment_id, profile_id:) ⇒ Object



55
56
57
58
# File 'lib/postproxy/resources/comments.rb', line 55

def get(post_id, comment_id, profile_id:)
  result = @client.request(:get, "/posts/#{post_id}/comments/#{comment_id}", params: { profile_id: profile_id })
  Comment.new(**result)
end

#hide(post_id, comment_id, profile_id:, idempotency_key: nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/postproxy/resources/comments.rb', line 80

def hide(post_id, comment_id, profile_id:, idempotency_key: nil)
  result = @client.request(:post, "/posts/#{post_id}/comments/#{comment_id}/hide",
    params: { profile_id: profile_id },
    idempotency_key: idempotency_key
  )
  AcceptedResponse.new(**result)
end

#like(post_id, comment_id, profile_id:, idempotency_key: nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/postproxy/resources/comments.rb', line 96

def like(post_id, comment_id, profile_id:, idempotency_key: nil)
  result = @client.request(:post, "/posts/#{post_id}/comments/#{comment_id}/like",
    params: { profile_id: profile_id },
    idempotency_key: idempotency_key
  )
  AcceptedResponse.new(**result)
end

#list(post_id, profile_id:, page: nil, per_page: nil, from: nil, to: nil) ⇒ Object

from and to filter on when PostProxy received the comment (created_at), not the platform's posted_at. They apply to top-level comments — one in range brings its full replies array with it.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/postproxy/resources/comments.rb', line 11

def list(post_id, profile_id:, page: nil, per_page: nil, from: nil, to: nil)
  params = { profile_id: profile_id }
  params[:page] = page if page
  params[:per_page] = per_page if per_page
  params[:from] = from if from
  params[:to] = to if to

  result = @client.request(:get, "/posts/#{post_id}/comments", params: params)
  comments = (result[:data] || []).map { |c| Comment.new(**c) }
  PaginatedResponse.new(
    data: comments,
    total: result[:total],
    page: result[:page],
    per_page: result[:per_page]
  )
end

#list_all(post_ids: nil, profiles: nil, from: nil, to: nil, page: nil, per_page: nil, profile_group_id: nil) ⇒ Object

Comments across every post in the profile group. Flat: replies come back as their own entries linked by parent_external_id, so total counts every comment. profiles takes profile IDs or network names, mixed.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/postproxy/resources/comments.rb', line 32

def list_all(post_ids: nil, profiles: nil, from: nil, to: nil, page: nil, per_page: nil,
  profile_group_id: nil)
  params = {}
  params[:post_ids] = Array(post_ids).join(",") if post_ids
  params[:profiles] = Array(profiles).join(",") if profiles
  params[:from] = from if from
  params[:to] = to if to
  params[:page] = page if page
  params[:per_page] = per_page if per_page

  result = @client.request(:get, "/comments",
    params: params.empty? ? nil : params,
    profile_group_id: profile_group_id
  )
  comments = (result[:data] || []).map { |c| BulkComment.new(**c) }
  PaginatedResponse.new(
    data: comments,
    total: result[:total],
    page: result[:page],
    per_page: result[:per_page]
  )
end

#private_reply(post_id, comment_id, profile_id:, text:, idempotency_key: nil) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/postproxy/resources/comments.rb', line 112

def private_reply(post_id, comment_id, profile_id:, text:, idempotency_key: nil)
  result = @client.request(:post, "/posts/#{post_id}/comments/#{comment_id}/private_reply",
    params: { profile_id: profile_id },
    json: { text: text },
    idempotency_key: idempotency_key
  )
  Message.new(**result)
end

#unhide(post_id, comment_id, profile_id:, idempotency_key: nil) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/postproxy/resources/comments.rb', line 88

def unhide(post_id, comment_id, profile_id:, idempotency_key: nil)
  result = @client.request(:post, "/posts/#{post_id}/comments/#{comment_id}/unhide",
    params: { profile_id: profile_id },
    idempotency_key: idempotency_key
  )
  AcceptedResponse.new(**result)
end

#unlike(post_id, comment_id, profile_id:, idempotency_key: nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/postproxy/resources/comments.rb', line 104

def unlike(post_id, comment_id, profile_id:, idempotency_key: nil)
  result = @client.request(:post, "/posts/#{post_id}/comments/#{comment_id}/unlike",
    params: { profile_id: profile_id },
    idempotency_key: idempotency_key
  )
  AcceptedResponse.new(**result)
end