Class: Patchboard::API
- Inherits:
-
Object
- Object
- Patchboard::API
- Defined in:
- lib/patchboard/api.rb
Defined Under Namespace
Classes: ArrayResource, HashResource
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
-
#service_url ⇒ Object
readonly
Returns the value of attribute service_url.
Instance Method Summary collapse
- #decorate(context, schema, data) ⇒ Object
- #find_mapping(schema) ⇒ Object
-
#initialize(definition) ⇒ API
constructor
A new instance of API.
Constructor Details
#initialize(definition) ⇒ API
Returns a new instance of API.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Attribute Details
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
5 6 7 |
# File 'lib/patchboard/api.rb', line 5 def mappings @mappings end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
5 6 7 |
# File 'lib/patchboard/api.rb', line 5 def resources @resources end |
#schemas ⇒ Object (readonly)
Returns the value of attribute schemas.
5 6 7 |
# File 'lib/patchboard/api.rb', line 5 def schemas @schemas end |
#service_url ⇒ Object (readonly)
Returns the value of attribute service_url.
5 6 7 |
# File 'lib/patchboard/api.rb', line 5 def service_url @service_url end |
Instance Method Details
#decorate(context, schema, data) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 |
# 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 |