lib/hphones/response.rb in hphones-ruby-0.0.1 vs lib/hphones/response.rb in hphones-ruby-0.0.2
- old
+ new
@@ -6,22 +6,53 @@
class Hphones
##
# Represents an API response
#
class Response
- def initialize(http_response)
+ STATUS_OK = 'OK'
+
+ RESPONSE_TYPE_JSON = 'json'
+ RESPONSE_TYPE_STATUS = 'status'
+ RESPONSE_TYPE_PATH = 'path'
+
+ def initialize(request, http_response)
@http_response = http_response
+ @request = request
end
def data
- @data ||= parse_json http_response.body
+ @data ||= parse_data http_response.body
end
private
- attr_reader :http_response
+ attr_reader :request, :http_response
- def parse_json(json_str)
- JSON.parse json_str
+ def parse_data(body)
+ case request.response_type
+ when RESPONSE_TYPE_JSON
+ parse_json body
+ when RESPONSE_TYPE_STATUS
+ parse_status body
+ when RESPONSE_TYPE_PATH
+ parse_path body
+ end
+ end
+
+ def parse_json(body)
+ JSON.parse body
+ end
+
+ def parse_status(body)
+ case body
+ when STATUS_OK
+ true
+ else
+ false
+ end
+ end
+
+ def parse_path(body)
+ body
end
end
end