lib/render/schema.rb in render-0.0.4 vs lib/render/schema.rb in render-0.0.5

- old
+ new

@@ -1,15 +1,11 @@ -# The Schema defines a collection of properties. -# It is responsible for returning its properties' values back to its Graph. - require "net/http" require "json" require "render" -require "render/attribute" -require "render/array_attribute" -require "render/hash_attribute" -require "render/dottable_hash" +require "render/attributes/array_attribute" +require "render/attributes/hash_attribute" +require "render/extensions/dottable_hash" module Render class Schema DEFAULT_TITLE = "untitled".freeze @@ -21,18 +17,17 @@ :universal_title, :raw_data, :serialized_data, :rendered_data - # TODO When given { ids: [1,2] }, parental_mapping { ids: id } means to make 2 calls def initialize(definition_or_title) Render.logger.debug("Loading #{definition_or_title}") self.definition = determine_definition(definition_or_title) title_or_default = definition.fetch(:title, DEFAULT_TITLE) self.title = title_or_default.to_sym - self.type = Render.parse_type(definition[:type]) + self.type = Type.parse!(definition[:type]) self.universal_title = definition.fetch(:universal_title, nil) if definition.keys.include?(:items) self.array_attribute = ArrayAttribute.new(definition) else @@ -59,20 +54,20 @@ def render!(explicit_data = nil, endpoint = nil) self.raw_data = Render.live ? request(endpoint) : explicit_data serialize!(raw_data) yield serialized_data if block_given? - self.rendered_data = DottableHash.new(hash_with_title_prefixes(serialized_data)) + self.rendered_data = Extensions::DottableHash.new(hash_with_title_prefixes(serialized_data)) end private def determine_definition(definition_or_title) if (definition_or_title.is_a?(Hash) && !definition_or_title.empty?) definition_or_title else - Render.definition(definition_or_title) + Definition.find(definition_or_title) end end def hash_with_title_prefixes(data) if universal_title @@ -87,10 +82,15 @@ end def default_request(endpoint) response = Net::HTTP.get_response(URI(endpoint)) if response.kind_of?(Net::HTTPSuccess) - JSON.parse(response.body).recursive_symbolize_keys! + response = JSON.parse(response.body.to_s) + if response.is_a?(Array) + Extensions::SymbolizableArray.new(response).recursively_symbolize_keys! + else + Extensions::DottableHash.new(response).recursively_symbolize_keys! + end else raise Errors::Schema::RequestError.new(endpoint, response) end rescue JSON::ParserError => error raise Errors::Schema::InvalidResponse.new(endpoint, response.body)