Class: Patchboard::API

Inherits:
Object
  • Object
show all
Defined in:
lib/patchboard/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#mappingsObject (readonly)

Returns the value of attribute mappings.



5
6
7
# File 'lib/patchboard/api.rb', line 5

def mappings
  @mappings
end

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/patchboard/api.rb', line 5

def resources
  @resources
end

#schemasObject (readonly)

Returns the value of attribute schemas.



5
6
7
# File 'lib/patchboard/api.rb', line 5

def schemas
  @schemas
end

#service_urlObject (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



31
32
33
34
35
36
37
38
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
# File 'lib/patchboard/api.rb', line 31

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.
    case schema[:type]
    when "array"
      # TODO: handle the case where schema.items is an array, which
      # signifies a tuple.  schema.additionalItems then becomes important.
      data.map! do |item|
        self.decorate(context, schema[:items], item)
      end

    when "object"
      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
      # TODO: handle schema.patternProperties
      # TODO: consider alternative to iterating over all keys.
      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
      data = Hashie::Mash.new data
    end
  end
  data
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