Skip to content

Commit 4b38722

Browse files
HarisDelalicharis_delalic
andauthored
🐛 fix NoMethodError in JobWebhook when error is nil (#222)
Co-authored-by: haris_delalic <haris.delalic@datamolino.com>
1 parent d89185a commit 4b38722

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

lib/mindee/parsing/v2/job_webhook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(server_response)
2323
@id = server_response['id']
2424
@created_at = parse_date(server_response['created_at'])
2525
@status = server_response['status']
26-
@error = ErrorResponse.new(server_response['error']) if server_response.key?('error')
26+
@error = ErrorResponse.new(server_response['error']) unless server_response['error'].nil?
2727
end
2828

2929
# RST display.

sig/mindee/parsing/v2/job_webhook.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Mindee
44
module V2
55
class JobWebhook
66
attr_reader created_at: DateTime?
7-
attr_reader error: ErrorResponse
7+
attr_reader error: ErrorResponse?
88
attr_reader id: String
99
attr_reader status: String
1010

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
require 'mindee'
4+
5+
RSpec.describe Mindee::Parsing::V2::JobWebhook do
6+
describe '#initialize' do
7+
context 'when error key is present but value is nil' do
8+
it 'does not raise an error and sets @error to nil' do
9+
server_response = {
10+
'id' => '12345678-1234-1234-1234-123456789012',
11+
'status' => 'Processing',
12+
'error' => nil,
13+
}
14+
15+
webhook = described_class.new(server_response)
16+
17+
expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012')
18+
expect(webhook.status).to eq('Processing')
19+
expect(webhook.error).to be_nil
20+
end
21+
end
22+
23+
context 'when error key is absent' do
24+
it 'does not raise an error and sets @error to nil' do
25+
server_response = {
26+
'id' => '12345678-1234-1234-1234-123456789012',
27+
'status' => 'Processing',
28+
}
29+
30+
webhook = described_class.new(server_response)
31+
32+
expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012')
33+
expect(webhook.status).to eq('Processing')
34+
expect(webhook.error).to be_nil
35+
end
36+
end
37+
38+
context 'when error key is present with valid error data' do
39+
it 'creates an ErrorResponse object' do
40+
server_response = {
41+
'id' => '12345678-1234-1234-1234-123456789012',
42+
'status' => 'Failed',
43+
'error' => {
44+
'status' => 500,
45+
'detail' => 'Internal server error',
46+
'title' => 'Server Error',
47+
'code' => 'INTERNAL_ERROR',
48+
},
49+
}
50+
51+
webhook = described_class.new(server_response)
52+
53+
expect(webhook.id).to eq('12345678-1234-1234-1234-123456789012')
54+
expect(webhook.status).to eq('Failed')
55+
expect(webhook.error).to be_a(Mindee::Parsing::V2::ErrorResponse)
56+
expect(webhook.error.status).to eq(500)
57+
expect(webhook.error.detail).to eq('Internal server error')
58+
end
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)