Skip to content

Instantly share code, notes, and snippets.

@buurzx
Created April 18, 2016 08:13
Show Gist options
  • Save buurzx/401319762877bb464b581c5185836f8b to your computer and use it in GitHub Desktop.
Save buurzx/401319762877bb464b581c5185836f8b to your computer and use it in GitHub Desktop.
require 'service_client'
class Result < OpenStruct
attr_reader :response, :parsed_body
def initialize(response)
@response = response
@parsed_body = JSON.parse(response.body)
super(parsed_body) if parsed_body.is_a?(Hash)
rescue JSON::ParserError => e
raise ServiceClient::ServiceServerError, 'Invalid JSON format from Service.'
end
def ok?
(parsed_body.blank? || error.blank?) && status != 400
end
def status
response.code
end
def error
(parsed_body.fetch('error', nil) || parsed_body.fetch('errors', []).first) if parsed_body.is_a?(Hash)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment