Class: PostProxy::Resources::Posts

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

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Posts

Returns a new instance of Posts.



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

def initialize(client)
  @client = client
end

Instance Method Details

#create(body, profiles:, media: nil, media_files: nil, platforms: nil, thread: nil, scheduled_at: nil, draft: nil, queue_id: nil, queue_priority: nil, profile_group_id: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
105
106
# File 'lib/postproxy/resources/posts.rb', line 31

def create(body, profiles:, media: nil, media_files: nil, platforms: nil,
           thread: nil, scheduled_at: nil, draft: nil, queue_id: nil,
           queue_priority: nil, profile_group_id: nil)
  has_files = media_files && !media_files.empty?
  has_thread_files = thread&.any? { |t| t[:media_files]&.any? }

  if has_files || has_thread_files
    form_data = { "post[body]" => body }
    form_data["post[scheduled_at]"] = format_time(scheduled_at) if scheduled_at
    form_data["post[draft]"] = draft.to_s if !draft.nil?

    files = []

    profiles.each do |p|
      files << ["profiles[]", nil, p, "text/plain"]
    end

    media&.each do |m|
      files << ["media[]", nil, m, "text/plain"]
    end

    if platforms
      params_hash = platforms.is_a?(PlatformParams) ? platforms.to_h : platforms
      params_hash.each do |platform, platform_params|
        platform_params.each do |key, value|
          files << ["platforms[#{platform}][#{key}]", nil, value.to_s, "text/plain"]
        end
      end
    end

    media_files&.each do |path|
      path = path.to_s
      filename = File.basename(path)
      content_type = mime_type_for(filename)
      io = File.open(path, "rb")
      files << ["media[]", filename, io, content_type]
    end

    thread&.each_with_index do |t, i|
      form_data["thread[#{i}][body]"] = t[:body] if t[:body]

      t[:media]&.each do |m|
        files << ["thread[#{i}][media][]", nil, m, "text/plain"]
      end

      t[:media_files]&.each do |path|
        path = path.to_s
        filename = File.basename(path)
        content_type = mime_type_for(filename)
        io = File.open(path, "rb")
        files << ["thread[#{i}][media][]", filename, io, content_type]
      end
    end

    result = @client.request(:post, "/posts",
      data: form_data,
      files: files,
      profile_group_id: profile_group_id
    )
  else
    post_payload = { body: body }
    post_payload[:scheduled_at] = format_time(scheduled_at) if scheduled_at
    post_payload[:draft] = draft unless draft.nil?

    json_body = { post: post_payload, profiles: profiles }
    json_body[:platforms] = platforms.is_a?(PlatformParams) ? platforms.to_h : platforms if platforms
    json_body[:media] = media if media
    json_body[:thread] = thread if thread
    json_body[:queue_id] = queue_id if queue_id
    json_body[:queue_priority] = queue_priority if queue_priority

    result = @client.request(:post, "/posts", json: json_body, profile_group_id: profile_group_id)
  end

  Post.new(**result)
end

#delete(id, profile_group_id: nil) ⇒ Object



208
209
210
211
# File 'lib/postproxy/resources/posts.rb', line 208

def delete(id, profile_group_id: nil)
  result = @client.request(:delete, "/posts/#{id}", profile_group_id: profile_group_id)
  DeleteResponse.new(**result)
end

#get(id, profile_group_id: nil) ⇒ Object



26
27
28
29
# File 'lib/postproxy/resources/posts.rb', line 26

def get(id, profile_group_id: nil)
  result = @client.request(:get, "/posts/#{id}", profile_group_id: profile_group_id)
  Post.new(**result)
end

#list(page: nil, per_page: nil, status: nil, platforms: nil, scheduled_after: nil, profile_group_id: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/postproxy/resources/posts.rb', line 8

def list(page: nil, per_page: nil, status: nil, platforms: nil, scheduled_after: nil, profile_group_id: nil)
  params = {}
  params[:page] = page if page
  params[:per_page] = per_page if per_page
  params[:status] = status if status
  params[:platforms] = platforms.join(",") if platforms
  params[:scheduled_after] = format_time(scheduled_after) if scheduled_after

  result = @client.request(:get, "/posts", params: params, profile_group_id: profile_group_id)
  posts = (result[:data] || []).map { |p| Post.new(**p) }
  PaginatedResponse.new(
    data: posts,
    total: result[:total],
    page: result[:page],
    per_page: result[:per_page]
  )
end

#publish_draft(id, profile_group_id: nil) ⇒ Object



190
191
192
193
# File 'lib/postproxy/resources/posts.rb', line 190

def publish_draft(id, profile_group_id: nil)
  result = @client.request(:post, "/posts/#{id}/publish", profile_group_id: profile_group_id)
  Post.new(**result)
end

#stats(post_ids, profiles: nil, from: nil, to: nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/postproxy/resources/posts.rb', line 195

def stats(post_ids, profiles: nil, from: nil, to: nil)
  params = { post_ids: post_ids.is_a?(Array) ? post_ids.join(",") : post_ids }
  params[:profiles] = profiles.is_a?(Array) ? profiles.join(",") : profiles if profiles
  params[:from] = format_time(from) if from
  params[:to] = format_time(to) if to

  result = @client.request(:get, "/posts/stats", params: params)
  posts = (result[:data] || {}).each_with_object({}) do |(post_id, post_data), hash|
    hash[post_id.to_s] = PostStats.new(**post_data.transform_keys(&:to_sym))
  end
  StatsResponse.new(data: posts)
end

#update(id, body: nil, profiles: nil, media: nil, media_files: nil, platforms: nil, thread: nil, scheduled_at: nil, draft: nil, queue_id: nil, queue_priority: nil, profile_group_id: nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/postproxy/resources/posts.rb', line 108

def update(id, body: nil, profiles: nil, media: nil, media_files: nil, platforms: nil,
           thread: nil, scheduled_at: nil, draft: nil, queue_id: nil,
           queue_priority: nil, profile_group_id: nil)
  has_files = media_files && !media_files.empty?
  has_thread_files = thread&.any? { |t| t[:media_files]&.any? }

  if has_files || has_thread_files
    form_data = {}
    form_data["post[body]"] = body if body
    form_data["post[scheduled_at]"] = format_time(scheduled_at) if scheduled_at
    form_data["post[draft]"] = draft.to_s if !draft.nil?

    files = []

    profiles&.each do |p|
      files << ["profiles[]", nil, p, "text/plain"]
    end

    media&.each do |m|
      files << ["media[]", nil, m, "text/plain"]
    end

    if platforms
      params_hash = platforms.is_a?(PlatformParams) ? platforms.to_h : platforms
      params_hash.each do |platform, platform_params|
        platform_params.each do |key, value|
          files << ["platforms[#{platform}][#{key}]", nil, value.to_s, "text/plain"]
        end
      end
    end

    media_files&.each do |path|
      path = path.to_s
      filename = File.basename(path)
      content_type = mime_type_for(filename)
      io = File.open(path, "rb")
      files << ["media[]", filename, io, content_type]
    end

    thread&.each_with_index do |t, i|
      form_data["thread[#{i}][body]"] = t[:body] if t[:body]

      t[:media]&.each do |m|
        files << ["thread[#{i}][media][]", nil, m, "text/plain"]
      end

      t[:media_files]&.each do |path|
        path = path.to_s
        filename = File.basename(path)
        content_type = mime_type_for(filename)
        io = File.open(path, "rb")
        files << ["thread[#{i}][media][]", filename, io, content_type]
      end
    end

    result = @client.request(:patch, "/posts/#{id}",
      data: form_data,
      files: files,
      profile_group_id: profile_group_id
    )
  else
    json_body = {}

    post_payload = {}
    post_payload[:body] = body if body
    post_payload[:scheduled_at] = format_time(scheduled_at) if scheduled_at
    post_payload[:draft] = draft unless draft.nil?
    json_body[:post] = post_payload unless post_payload.empty?

    json_body[:profiles] = profiles if profiles
    json_body[:platforms] = platforms.is_a?(PlatformParams) ? platforms.to_h : platforms if platforms
    json_body[:media] = media if media
    json_body[:thread] = thread if thread
    json_body[:queue_id] = queue_id if queue_id
    json_body[:queue_priority] = queue_priority if queue_priority

    result = @client.request(:patch, "/posts/#{id}", json: json_body, profile_group_id: profile_group_id)
  end

  Post.new(**result)
end