Created
April 18, 2016 08:13
-
-
Save buurzx/401319762877bb464b581c5185836f8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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