Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/zammad_api/json_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'json'

module ZammadAPI
module JsonHelper
def safe_json_parse(string)
JSON.parse(string)
rescue JSON::ParserError
{}
end
end
end
5 changes: 4 additions & 1 deletion lib/zammad_api/list_base.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions lib/zammad_api/resources/base.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/zammad_api/resources/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/zammad_api/resources/ticket_article_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/zammad_api/json_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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('<html><body>Bad Gateway</body></html>')).to eq({})
end
end
end