postia 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +84 -0
  4. data/lib/postia/client.rb +321 -0
  5. data/lib/postia.rb +49 -0
  6. metadata +50 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 135eec155752adfda30a8e097d49142e77f6f98e9662307b4715b2f1f472da91
4
+ data.tar.gz: 485beaab0c63c65aae3838d19ab59c8bb1d35376e63f4a32407988501db0de81
5
+ SHA512:
6
+ metadata.gz: 61d33c7d5499074f6b614f6fce3ceeb2af612d16d4dacd091dd813735653c0b20e91c70954166d7a099d67fe6f479903271992fb92ce9170343d5580cf78789f
7
+ data.tar.gz: fc9c82ce8d8d71b9942c5f6e1a69a6c6f56c183b80b17ac57996ead00d6368a8ffd117f44e0ef2a7daf10fee5c193d55d18b8927d9925a574d2f9dfb6b91db0f
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Postia
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Postia Ruby SDK
2
+
3
+ [![Official Postia SDK](https://img.shields.io/badge/Postia-Official%20SDK-000000?style=flat-square)](https://developers.postia.app/)
4
+
5
+ Official Ruby client for the [Postia API](https://developers.postia.app/api/) using Ruby's standard `net/http` with zero runtime dependencies.
6
+
7
+ ## Requirements
8
+
9
+ - Ruby 3.0 or higher
10
+
11
+ ## Installation
12
+
13
+ Add to your `Gemfile`:
14
+
15
+ ```ruby
16
+ gem "postia", git: "https://github.com/Postia-SDK/postia-ruby.git"
17
+ ```
18
+
19
+ Or install from source:
20
+
21
+ ```bash
22
+ gem build postia.gemspec
23
+ gem install postia-1.0.0.gem
24
+ ```
25
+
26
+ ## Quick Start
27
+
28
+ ```ruby
29
+ require "postia"
30
+
31
+ client = Postia::Client.new(
32
+ api_key: ENV.fetch("POSTIA_API_KEY"),
33
+ base_url: "https://pub.postia.app" # optional, default
34
+ )
35
+
36
+ # Check caller and credits
37
+ me = client.get_me
38
+ puts "Credits remaining: #{me["credits"]}"
39
+
40
+ # List connected channels
41
+ channels = client.list_channels["data"]
42
+ channel_id = channels.first["channel_id"]
43
+
44
+ # Create and schedule a post with an idempotency key
45
+ post = client.create_post(
46
+ text: "Hello from Ruby!",
47
+ channel_ids: [channel_id],
48
+ schedule_at: "2026-12-24T09:00:00Z",
49
+ idempotency_key: "post-hello-1"
50
+ )
51
+ puts "Scheduled post: #{post["post_id"]}"
52
+ ```
53
+
54
+ ## Error Handling
55
+
56
+ All 4xx/5xx responses raise `Postia::Error` parsed from RFC 9457 `application/problem+json`:
57
+
58
+ ```ruby
59
+ begin
60
+ client.create_post(text: "")
61
+ rescue Postia::Error => e
62
+ warn "Status: #{e.status}"
63
+ warn "Type: #{e.type}"
64
+ warn "Title: #{e.title}"
65
+ warn "Detail: #{e.detail}"
66
+ warn "Request ID: #{e.request_id}"
67
+ warn "Retry-After: #{e.retry_after}"
68
+ e.errors.each do |err|
69
+ warn " #{err["field"]}: #{err["message"]}"
70
+ end
71
+ end
72
+ ```
73
+
74
+ ## Endpoints Covered
75
+
76
+ - **Account**: `get_me`, `list_channels`
77
+ - **Posts**: `list_posts`, `create_post`, `get_post`, `update_post`, `delete_post`, `publish_post`, `schedule_post`
78
+ - **Media**: `start_upload`, `complete_upload`, `add_media_from_url`
79
+ - **Analytics**: `get_analytics`
80
+ - **Webhooks**: `list_webhooks`, `create_webhook`, `get_webhook`, `update_webhook`, `delete_webhook`, `rotate_webhook_secret`, `test_webhook`, `list_webhook_deliveries`, `redeliver_webhook`
81
+
82
+ ## License
83
+
84
+ MIT
@@ -0,0 +1,321 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+
7
+ module Postia
8
+ class Client
9
+ DEFAULT_BASE_URL = "https://pub.postia.app"
10
+ UNSET = Object.new.freeze
11
+
12
+ attr_reader :base_url
13
+
14
+ def initialize(api_key:, base_url: DEFAULT_BASE_URL, open_timeout: 10, read_timeout: 30)
15
+ raise ArgumentError, "api_key must not be blank" if api_key.nil? || api_key.to_s.strip.empty?
16
+
17
+ @api_key = api_key.to_s
18
+ @base_url = normalize_base_url(base_url)
19
+ @open_timeout = open_timeout
20
+ @read_timeout = read_timeout
21
+ end
22
+
23
+ # Account
24
+
25
+ # GET /v1/me
26
+ def get_me
27
+ request(:get, "/v1/me")
28
+ end
29
+
30
+ # GET /v1/channels
31
+ def list_channels
32
+ request(:get, "/v1/channels")
33
+ end
34
+
35
+ # Posts
36
+
37
+ # GET /v1/posts
38
+ def list_posts(limit: nil, cursor: nil, status: nil, q: nil)
39
+ query = compact_query(limit: limit, cursor: cursor, status: status, q: q)
40
+ request(:get, "/v1/posts", query: query)
41
+ end
42
+
43
+ # POST /v1/posts
44
+ def create_post(params = {}, idempotency_key: nil, **kwargs)
45
+ payload = merge_payload(params, kwargs)
46
+ request(:post, "/v1/posts", body: payload, idempotency_key: idempotency_key)
47
+ end
48
+
49
+ # GET /v1/posts/{post_id}
50
+ def get_post(post_id)
51
+ request(:get, "/v1/posts/#{encode_segment(post_id)}")
52
+ end
53
+
54
+ # PATCH /v1/posts/{post_id}
55
+ def update_post(post_id, params = {}, idempotency_key: nil, **kwargs)
56
+ payload = merge_payload(params, kwargs)
57
+ request(:patch, "/v1/posts/#{encode_segment(post_id)}", body: payload, idempotency_key: idempotency_key)
58
+ end
59
+
60
+ # DELETE /v1/posts/{post_id}
61
+ def delete_post(post_id)
62
+ request(:delete, "/v1/posts/#{encode_segment(post_id)}")
63
+ end
64
+
65
+ # POST /v1/posts/{post_id}/publish
66
+ def publish_post(post_id, idempotency_key: nil)
67
+ request(:post, "/v1/posts/#{encode_segment(post_id)}/publish", idempotency_key: idempotency_key)
68
+ end
69
+
70
+ # POST /v1/posts/{post_id}/schedule
71
+ def schedule_post(post_id, schedule_at:, idempotency_key: nil)
72
+ request(
73
+ :post,
74
+ "/v1/posts/#{encode_segment(post_id)}/schedule",
75
+ body: { schedule_at: format_instant(schedule_at) },
76
+ idempotency_key: idempotency_key
77
+ )
78
+ end
79
+
80
+ # Media
81
+
82
+ # POST /v1/media/uploads
83
+ def start_upload(post_id:, file_name:, byte_size: nil, as: nil, idempotency_key: nil)
84
+ payload = { post_id: post_id, file_name: file_name }
85
+ payload[:byte_size] = byte_size unless byte_size.nil?
86
+ payload[:as] = as unless as.nil?
87
+ request(:post, "/v1/media/uploads", body: payload, idempotency_key: idempotency_key)
88
+ end
89
+
90
+ # POST /v1/media/uploads/{upload_id}/complete
91
+ def complete_upload(upload_id, post_id:, as: nil, alt: UNSET, file_name: nil, idempotency_key: nil)
92
+ payload = { post_id: post_id }
93
+ payload[:as] = as unless as.nil?
94
+ payload[:alt] = alt unless alt.equal?(UNSET)
95
+ payload[:file_name] = file_name unless file_name.nil?
96
+ request(
97
+ :post,
98
+ "/v1/media/uploads/#{encode_segment(upload_id)}/complete",
99
+ body: payload,
100
+ idempotency_key: idempotency_key
101
+ )
102
+ end
103
+
104
+ # POST /v1/media/from_url
105
+ def add_media_from_url(post_id:, url:, alt: UNSET, idempotency_key: nil)
106
+ payload = { post_id: post_id, url: url }
107
+ payload[:alt] = alt unless alt.equal?(UNSET)
108
+ request(:post, "/v1/media/from_url", body: payload, idempotency_key: idempotency_key)
109
+ end
110
+
111
+ # Analytics
112
+
113
+ # GET /v1/analytics
114
+ #
115
+ # Filter by period (from:/to:, ISO 8601 String, Date or Time; from:
116
+ # overrides days:), by network and by channel. Arrays go comma-separated.
117
+ def get_analytics(days: nil, channel_id: nil, network: nil, from: nil, to: nil)
118
+ csv = ->(v) { v.is_a?(Array) ? v.join(",") : v }
119
+ iso = ->(v) { v.respond_to?(:iso8601) ? v.iso8601 : v }
120
+ query = compact_query(
121
+ days: days,
122
+ channel_id: csv.call(channel_id),
123
+ network: csv.call(network),
124
+ from: iso.call(from),
125
+ to: iso.call(to)
126
+ )
127
+ request(:get, "/v1/analytics", query: query)
128
+ end
129
+
130
+ # Webhooks
131
+
132
+ # GET /v1/webhooks
133
+ def list_webhooks
134
+ request(:get, "/v1/webhooks")
135
+ end
136
+
137
+ # POST /v1/webhooks
138
+ def create_webhook(url:, events:, idempotency_key: nil)
139
+ request(
140
+ :post,
141
+ "/v1/webhooks",
142
+ body: { url: url, events: events },
143
+ idempotency_key: idempotency_key
144
+ )
145
+ end
146
+
147
+ # GET /v1/webhooks/{webhook_id}
148
+ def get_webhook(webhook_id)
149
+ request(:get, "/v1/webhooks/#{encode_segment(webhook_id)}")
150
+ end
151
+
152
+ # PATCH /v1/webhooks/{webhook_id}
153
+ def update_webhook(webhook_id, params = {}, idempotency_key: nil, **kwargs)
154
+ payload = merge_payload(params, kwargs)
155
+ request(:patch, "/v1/webhooks/#{encode_segment(webhook_id)}", body: payload, idempotency_key: idempotency_key)
156
+ end
157
+
158
+ # DELETE /v1/webhooks/{webhook_id}
159
+ def delete_webhook(webhook_id)
160
+ request(:delete, "/v1/webhooks/#{encode_segment(webhook_id)}")
161
+ end
162
+
163
+ # POST /v1/webhooks/{webhook_id}/rotate_secret
164
+ def rotate_webhook_secret(webhook_id, idempotency_key: nil)
165
+ request(:post, "/v1/webhooks/#{encode_segment(webhook_id)}/rotate_secret", idempotency_key: idempotency_key)
166
+ end
167
+
168
+ # POST /v1/webhooks/{webhook_id}/test
169
+ def test_webhook(webhook_id, idempotency_key: nil)
170
+ request(:post, "/v1/webhooks/#{encode_segment(webhook_id)}/test", idempotency_key: idempotency_key)
171
+ end
172
+
173
+ # GET /v1/webhooks/{webhook_id}/deliveries
174
+ def list_webhook_deliveries(webhook_id, limit: nil, cursor: nil)
175
+ query = compact_query(limit: limit, cursor: cursor)
176
+ request(:get, "/v1/webhooks/#{encode_segment(webhook_id)}/deliveries", query: query)
177
+ end
178
+
179
+ # POST /v1/webhooks/{webhook_id}/deliveries/{delivery_id}/redeliver
180
+ def redeliver_webhook(webhook_id, delivery_id, idempotency_key: nil)
181
+ request(
182
+ :post,
183
+ "/v1/webhooks/#{encode_segment(webhook_id)}/deliveries/#{encode_segment(delivery_id)}/redeliver",
184
+ idempotency_key: idempotency_key
185
+ )
186
+ end
187
+
188
+ private
189
+
190
+ HTTP_CLASSES = {
191
+ get: Net::HTTP::Get,
192
+ post: Net::HTTP::Post,
193
+ patch: Net::HTTP::Patch,
194
+ delete: Net::HTTP::Delete
195
+ }.freeze
196
+
197
+ def request(method, path, query: nil, body: nil, idempotency_key: nil)
198
+ uri = build_uri(path, query)
199
+ req_class = HTTP_CLASSES.fetch(method)
200
+ req = req_class.new(uri)
201
+ req["Authorization"] = "Bearer #{@api_key}"
202
+ req["Accept"] = "application/json, application/problem+json"
203
+ req["User-Agent"] = "postia-ruby/#{Postia::VERSION}"
204
+
205
+ if !idempotency_key.nil? && !idempotency_key.to_s.empty? && (method == :post || method == :patch)
206
+ req["Idempotency-Key"] = idempotency_key.to_s
207
+ end
208
+
209
+ unless body.nil?
210
+ req["Content-Type"] = "application/json"
211
+ req.body = JSON.generate(body)
212
+ end
213
+
214
+ response = Net::HTTP.start(
215
+ uri.host,
216
+ uri.port,
217
+ use_ssl: uri.scheme == "https",
218
+ open_timeout: @open_timeout,
219
+ read_timeout: @read_timeout
220
+ ) do |http|
221
+ http.request(req)
222
+ end
223
+
224
+ handle_response(response)
225
+ end
226
+
227
+ def handle_response(response)
228
+ status = response.code.to_i
229
+ raw_body = response.body.to_s
230
+
231
+ if status >= 200 && status < 300
232
+ return {} if raw_body.strip.empty?
233
+
234
+ return JSON.parse(raw_body)
235
+ end
236
+
237
+ raise build_error(status, response, raw_body)
238
+ end
239
+
240
+ def build_error(status, response, raw_body)
241
+ problem = parse_json_safe(raw_body)
242
+ retry_after = response["Retry-After"]&.to_i
243
+ header_request_id = response["X-Request-Id"]
244
+
245
+ if problem.is_a?(Hash)
246
+ Error.new(
247
+ status: problem["status"] || status,
248
+ type: problem["type"],
249
+ title: problem["title"],
250
+ detail: problem["detail"],
251
+ request_id: problem["request_id"] || header_request_id,
252
+ errors: problem["errors"],
253
+ retry_after: retry_after,
254
+ raw_body: raw_body
255
+ )
256
+ else
257
+ Error.new(
258
+ "HTTP #{status}: #{raw_body.empty? ? response.message : raw_body}",
259
+ status: status,
260
+ request_id: header_request_id,
261
+ retry_after: retry_after,
262
+ raw_body: raw_body
263
+ )
264
+ end
265
+ end
266
+
267
+ def parse_json_safe(text)
268
+ return nil if text.strip.empty?
269
+
270
+ JSON.parse(text)
271
+ rescue JSON::ParserError
272
+ nil
273
+ end
274
+
275
+ def build_uri(path, query)
276
+ base = URI.parse(@base_url)
277
+ base_path = base.path.sub(%r{/+$}, "")
278
+ uri = base.dup
279
+ uri.path = "#{base_path}#{path}"
280
+ if query && !query.empty?
281
+ encoded = URI.encode_www_form(query)
282
+ uri.query = encoded
283
+ else
284
+ uri.query = nil
285
+ end
286
+ uri
287
+ end
288
+
289
+ def normalize_base_url(url)
290
+ value = (url || DEFAULT_BASE_URL).to_s.strip.sub(%r{/+$}, "")
291
+ raise ArgumentError, "base_url must not be blank" if value.empty?
292
+
293
+ value
294
+ end
295
+
296
+ def encode_segment(value)
297
+ raise ArgumentError, "path parameter must not be blank" if value.nil? || value.to_s.empty?
298
+
299
+ URI.encode_www_form_component(value.to_s).gsub("+", "%20")
300
+ end
301
+
302
+ def compact_query(hash)
303
+ result = {}
304
+ hash.each do |k, v|
305
+ result[k.to_s] = v unless v.nil?
306
+ end
307
+ result
308
+ end
309
+
310
+ def merge_payload(params, kwargs)
311
+ merged = {}
312
+ params.each { |k, v| merged[k.to_s] = v } if params
313
+ kwargs.each { |k, v| merged[k.to_s] = v } unless kwargs.empty?
314
+ merged
315
+ end
316
+
317
+ def format_instant(value)
318
+ value.respond_to?(:iso8601) ? value.iso8601 : value.to_s
319
+ end
320
+ end
321
+ end
data/lib/postia.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "postia/client"
4
+
5
+ module Postia
6
+ VERSION = "1.1.0"
7
+
8
+ # Raised when the Postia API returns a 4xx or 5xx response (RFC 9457 Problem Details).
9
+ class Error < StandardError
10
+ attr_reader :status, :type, :title, :detail, :request_id, :errors, :retry_after, :raw_body
11
+
12
+ def initialize(
13
+ message = nil,
14
+ status: nil,
15
+ type: nil,
16
+ title: nil,
17
+ detail: nil,
18
+ request_id: nil,
19
+ errors: nil,
20
+ retry_after: nil,
21
+ raw_body: nil
22
+ )
23
+ @status = status
24
+ @type = type
25
+ @title = title
26
+ @detail = detail
27
+ @request_id = request_id
28
+ @errors = errors || []
29
+ @retry_after = retry_after
30
+ @raw_body = raw_body
31
+ super(message || build_message)
32
+ end
33
+
34
+ private
35
+
36
+ def build_message
37
+ parts = []
38
+ parts << "[#{status}]" if status
39
+ parts << (title || detail || "Postia API request failed")
40
+ parts << "- #{detail}" if title && detail && detail != title
41
+ parts << "(request_id: #{request_id})" if request_id
42
+ if errors.is_a?(Array) && !errors.empty?
43
+ formatted = errors.map { |e| e.is_a?(Hash) ? "#{e["field"]}: #{e["message"]}" : e.to_s }
44
+ parts << "[#{formatted.join("; ")}]"
45
+ end
46
+ parts.join(" ")
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postia
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Postia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Write, schedule, and publish social media posts using the Postia API.
14
+ email:
15
+ - support@postia.app
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/postia.rb
23
+ - lib/postia/client.rb
24
+ homepage: https://developers.postia.app
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://developers.postia.app
29
+ source_code_uri: https://github.com/Postia-SDK/postia-ruby
30
+ documentation_uri: https://developers.postia.app/api/
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.0.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.5.22
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Official Ruby SDK for the Postia API
50
+ test_files: []