snapchat_api 0.1.5 → 0.1.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f835d1d5764f58f10ea61e25aa7bec86ba1ff18670ec3e62461968385d3e0c08
4
- data.tar.gz: edc4635f63d1811a567a8e382c395b54fb5bc19d9a3e05344c4ed2a15bcad10b
3
+ metadata.gz: 610ba62897a08a9423272ac199a3c0bb3d9a2e4784d7bf7eb7f3f1122b696773
4
+ data.tar.gz: 3cdb587109ab2340f7a765f2f0e41c83fa1e228ffbc2147ae29aea1c7036ff72
5
5
  SHA512:
6
- metadata.gz: a50dcef0a90a743017da6405e26fbc4f46b551d50e7012aed4ecd7c40efe5eefcd77ee0c31193ade4d8aaab1921d753051589a68891def9a6bbc7c6a986a042d
7
- data.tar.gz: b7d88ee3f394e8d0e869b7a28691fe94a2fe92414147fde541e2a1a100a02bdc6475b2f0d13804c77a34562337542d3b2c80cd337e118a910204c0c79670e82e
6
+ metadata.gz: 375ea46e5c1a6d95ae99655e0da9959245e62bea8294bba8c752db0cd60e41d71125003bc1f8e883ce35190d6ed2ffeacde620592816e4f0e98a34ee8123bd79
7
+ data.tar.gz: b4ee14a9e0207b32573609d5673b25365890fe0c2ff756f007cc82c83439aa11292fbf1d998e8a97ad5ea82e13c313c19d64cee05efd402778d14a76c7136448
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.6](https://github.com/k0va1/snapchat_api/compare/v0.1.5...v0.1.6) (2025-12-30)
4
+
5
+
6
+ ### Features
7
+
8
+ * add error handling for API responses with request_status ERROR ([554d4c5](https://github.com/k0va1/snapchat_api/commit/554d4c5f8ec3545f632110875cd436de84faaac2))
9
+ * add error handling for API responses with request_status ERROR ([6ebe619](https://github.com/k0va1/snapchat_api/commit/6ebe6190b4283678dbec9104dee051ccc25bd80f))
10
+
3
11
  ## [0.1.5](https://github.com/k0va1/snapchat_api/compare/v0.1.4...v0.1.5) (2025-08-25)
4
12
 
5
13
 
@@ -135,10 +135,17 @@ module SnapchatApi
135
135
  private
136
136
 
137
137
  def handle_response(response)
138
+ body = response.body
139
+ status = response.status
140
+
141
+ # Check for request-level errors (API returns 200 but request_status is ERROR)
142
+ if response.success? && body.is_a?(Hash)
143
+ check_request_status_error(body, status)
144
+ return response
145
+ end
146
+
138
147
  return response if response.success?
139
148
 
140
- status = response.status
141
- body = response.body
142
149
  error_message = body.is_a?(Hash) ? body&.dig("message") : body
143
150
 
144
151
  klass = case status
@@ -158,5 +165,47 @@ module SnapchatApi
158
165
 
159
166
  raise klass.new(error_message || "HTTP #{status}", status, body)
160
167
  end
168
+
169
+ def check_request_status_error(body, status)
170
+ return unless body["request_status"] == "ERROR"
171
+
172
+ request_id = body["request_id"]
173
+ sub_errors = extract_sub_errors(body)
174
+ error_message = build_error_message(body, sub_errors)
175
+
176
+ raise SnapchatApi::RequestError.new(error_message, status, body, request_id, sub_errors)
177
+ end
178
+
179
+ def extract_sub_errors(body)
180
+ sub_errors = []
181
+
182
+ # Look for sub-request errors in various response keys
183
+ # The API may return errors under different keys depending on the endpoint
184
+ possible_keys = %w[creatives ads campaigns ad_squads media adaccounts]
185
+
186
+ possible_keys.each do |key|
187
+ next unless body[key].is_a?(Array)
188
+
189
+ body[key].each do |item|
190
+ if item.is_a?(Hash) && item["sub_request_status"] == "ERROR"
191
+ sub_errors << {
192
+ reason: item["sub_request_error_reason"],
193
+ status: item["sub_request_status"]
194
+ }
195
+ end
196
+ end
197
+ end
198
+
199
+ sub_errors
200
+ end
201
+
202
+ def build_error_message(body, sub_errors)
203
+ if sub_errors.any?
204
+ messages = sub_errors.map { |e| e[:reason] }.compact
205
+ messages.join("; ")
206
+ else
207
+ body["debug_message"] || body["display_message"] || "Request failed with status ERROR"
208
+ end
209
+ end
161
210
  end
162
211
  end
@@ -2,10 +2,12 @@ module SnapchatApi
2
2
  class Error < StandardError
3
3
  attr_reader :status_code
4
4
  attr_reader :body
5
+ attr_reader :request_id
5
6
 
6
- def initialize(message = nil, status_code = nil, body = nil)
7
+ def initialize(message = nil, status_code = nil, body = nil, request_id = nil)
7
8
  @status_code = status_code
8
9
  @body = body
10
+ @request_id = request_id
9
11
  super(message)
10
12
  end
11
13
  end
@@ -19,4 +21,14 @@ module SnapchatApi
19
21
  class ApiError < Error; end
20
22
 
21
23
  class RateLimitError < Error; end
24
+
25
+ # Raised when API returns HTTP 200 but request_status is ERROR
26
+ class RequestError < Error
27
+ attr_reader :sub_errors
28
+
29
+ def initialize(message = nil, status_code = nil, body = nil, request_id = nil, sub_errors = [])
30
+ @sub_errors = sub_errors
31
+ super(message, status_code, body, request_id)
32
+ end
33
+ end
22
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SnapchatApi
4
- VERSION = "0.1.5"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapchat_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Koval