class Patchboard::API
Attributes
mappings[R]
resources[R]
schemas[R]
service_url[R]
Public Class Methods
new(definition)
click to toggle source
# File lib/patchboard/api.rb, line 7 def initialize(definition) @service_url = definition[:service_url] @resources = Hashie::Mash.new definition[:resources] @resources.each do |name, definition| definition.name = name end @schemas = definition[:schemas] @mappings = {} definition[:mappings].each do |name, mapping| @mappings[name] = Mapping.new(self, name, mapping) end end
Public Instance Methods
decorate(context, schema, data)
click to toggle source
# File lib/patchboard/api.rb, line 39 def decorate(context, schema, data) unless schema return Hashie::Mash.new(data) end if mapping = self.find_mapping(schema) # when we have a resource class, instantiate it using the input data. data = mapping.klass.new context, data else # Otherwise traverse the schema in search of subschemas that have # resource classes available. if schema[:items] # TODO: handle the case where schema.items is an array, which # signifies a tuple. schema.additionalItems then becomes important. array = data.map! do |item| self.decorate(context, schema[:items], item) end data = ArrayResource.new(array) end if schema[:properties] schema[:properties].each do |key, prop_schema| if value = data[key] data[key] = self.decorate(context, prop_schema, value) end end end if schema[:additionalProperties] data.each do |key, value| next if schema[:properties] && schema[:properties][key] data[key] = self.decorate(context, schema[:additionalProperties], value) end end if data.is_a? Hash data = Hashie::Mash.new data end data end end
find_mapping(schema)
click to toggle source
# File lib/patchboard/api.rb, line 22 def find_mapping(schema) # TODO: stitch this into the actual schemas. # ex: schema.mapping if id = (schema[:id] || schema[:$ref]) name = id.split("#").last @mappings[name.to_sym] end end