diff --git a/lib/zammad_api/json_helper.rb b/lib/zammad_api/json_helper.rb new file mode 100644 index 0000000..fd8ca4c --- /dev/null +++ b/lib/zammad_api/json_helper.rb @@ -0,0 +1,11 @@ +require 'json' + +module ZammadAPI + module JsonHelper + def safe_json_parse(string) + JSON.parse(string) + rescue JSON::ParserError + {} + end + end +end diff --git a/lib/zammad_api/list_base.rb b/lib/zammad_api/list_base.rb index 7ac483a..6b59114 100644 --- a/lib/zammad_api/list_base.rb +++ b/lib/zammad_api/list_base.rb @@ -1,6 +1,9 @@ +require 'zammad_api/json_helper' + module ZammadAPI class ListBase include Enumerable + include ZammadAPI::JsonHelper def initialize(resource, transport, parameter = {}) @resource = resource @@ -59,7 +62,7 @@ def request(request, url, parameter) }.join('&') response = @transport.get(url: url) - data = JSON.parse(response.body) + data = safe_json_parse(response.body) if response.status != 200 raise "Can't get .#{request} of object (#{@resource.class.name}): #{data['error']}" end diff --git a/lib/zammad_api/resources/base.rb b/lib/zammad_api/resources/base.rb index f329bfc..c894401 100644 --- a/lib/zammad_api/resources/base.rb +++ b/lib/zammad_api/resources/base.rb @@ -1,10 +1,13 @@ require 'cgi' -require 'json' +require 'zammad_api/json_helper' require 'zammad_api/transport' module ZammadAPI module Resources class Base + include ZammadAPI::JsonHelper + extend ZammadAPI::JsonHelper + attr_accessor :new_instance, :url, :attributes attr_reader :changes @@ -41,7 +44,7 @@ def changed? def destroy response = @transport.delete(url: "#{@url}/#{@attributes[:id]}") if response.body.to_s != '' && response.body.to_s != ' ' - data = JSON.parse(response.body) + data = safe_json_parse(response.body) end return true if response.status == 200 @@ -76,7 +79,7 @@ def self.search(transport, parameter) def self.find(transport, id) response = transport.get(url: "#{@url}/#{id}?expand=true") - data = JSON.parse(response.body) + data = safe_json_parse(response.body) if response.status != 200 raise "Can't find object (#{self.class.name}): #{data['error']}" end @@ -108,7 +111,7 @@ def saved_attributes def save_new response = @transport.post(url: "#{@url}?expand=true", params: @attributes) - attributes = JSON.parse(response.body) + attributes = safe_json_parse(response.body) return attributes if response.status == 201 save_error(attributes) @@ -120,7 +123,7 @@ def save_existing attributes_to_post[name] = values[1] end response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post) - attributes = JSON.parse(response.body) + attributes = safe_json_parse(response.body) return attributes if response.status == 200 diff --git a/lib/zammad_api/resources/ticket.rb b/lib/zammad_api/resources/ticket.rb index 8894ced..dfb9c12 100644 --- a/lib/zammad_api/resources/ticket.rb +++ b/lib/zammad_api/resources/ticket.rb @@ -3,7 +3,7 @@ class ZammadAPI::Resources::Ticket < ZammadAPI::Resources::Base def articles response = @transport.get(url: "/api/v1/ticket_articles/by_ticket/#{id}?expand=true") - data = JSON.parse(response.body) + data = safe_json_parse(response.body) if response.status != 200 raise "Can't get articles (#{self.class.name}): #{data['error']}" end diff --git a/lib/zammad_api/resources/ticket_article_attachment.rb b/lib/zammad_api/resources/ticket_article_attachment.rb index f0abb2e..a445f60 100644 --- a/lib/zammad_api/resources/ticket_article_attachment.rb +++ b/lib/zammad_api/resources/ticket_article_attachment.rb @@ -13,7 +13,7 @@ def download response = @transport.get(url: "/api/v1/ticket_attachment/#{ticket_id}/#{article_id}/#{id}") return response.body if response.status == 200 - data = JSON.parse(response.body) + data = safe_json_parse(response.body) raise "Can't get articles (#{self.class.name}): #{data['error']}" end end diff --git a/spec/zammad_api/json_helper_spec.rb b/spec/zammad_api/json_helper_spec.rb new file mode 100644 index 0000000..29eb956 --- /dev/null +++ b/spec/zammad_api/json_helper_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe ZammadAPI::JsonHelper do + subject(:helper) do + Class.new { include ZammadAPI::JsonHelper }.new + end + + describe '#safe_json_parse' do + it 'parses valid JSON' do + expect(helper.safe_json_parse('{"key":"value"}')).to eq('key' => 'value') + end + + it 'returns empty hash for invalid JSON' do + expect(helper.safe_json_parse('not json')).to eq({}) + end + + it 'returns empty hash for HTML responses' do + expect(helper.safe_json_parse('Bad Gateway')).to eq({}) + end + end +end