Class: Vk::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/vk/schema.rb,
lib/vk/schema/method.rb,
lib/vk/schema/object.rb,
lib/vk/schema/response.rb,
lib/vk/schema/generator.rb,
lib/vk/schema/namespace.rb,
lib/vk/schema/definition.rb,
lib/vk/schema/generator/type.rb,
lib/vk/schema/definition/type.rb,
lib/vk/schema/generator/types.rb,
lib/vk/schema/definition/error.rb,
lib/vk/schema/generator/client.rb,
lib/vk/schema/generator/common.rb,
lib/vk/schema/generator/errors.rb,
lib/vk/schema/generator/method.rb,
lib/vk/schema/generator/object.rb,
lib/vk/schema/definition/method.rb,
lib/vk/schema/definition/object.rb,
lib/vk/schema/generator/methods.rb,
lib/vk/schema/generator/objects.rb,
lib/vk/schema/generator/response.rb,
lib/vk/schema/definition/constant.rb,
lib/vk/schema/definition/response.rb,
lib/vk/schema/generator/responses.rb,
lib/vk/schema/definition/attribute.rb,
lib/vk/schema/definition/has_attributes.rb

Defined Under Namespace

Classes: Definition, Generator, Method, Namespace, Object, Response

objects.json collapse

OBJECTS_DEFINITION =
%r{\A(objects\.json)?#/definitions/}
RESPONSES_DEFINITION =
%r{\Aresponses\.json?#/definitions/}

Instance Attribute Summary collapse

objects.json collapse

methods.json collapse

responses.json collapse

schema.json collapse

Client collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: 'data/schema') ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • path (String) (defaults to: 'data/schema')


16
17
18
19
20
21
22
# File 'lib/vk/schema.rb', line 16

def initialize(path: 'data/schema')
  @path = Pathname.new(path)
  @methods = JSON.parse(File.read(@path / 'methods.json'))
  @objects = JSON.parse(File.read(@path / 'objects.json'))
  @responses = JSON.parse(File.read(@path / 'responses.json'))
  @schema = JSON.parse(File.read(@path / 'responses.json'))
end

Instance Attribute Details

#pathPathname (readonly)

Returns:

  • (Pathname)


25
26
27
# File 'lib/vk/schema.rb', line 25

def path
  @path
end

Instance Method Details

#definition_of(ref) ⇒ Definition

Parameters:

  • ref (String)

Returns:



66
67
68
69
70
71
72
73
# File 'lib/vk/schema.rb', line 66

def definition_of(ref)
  case ref
  when OBJECTS_DEFINITION
    objects_reference[ref.sub(OBJECTS_DEFINITION, '')]
  else
    raise "Unknown reference: #{ref.inspect}"
  end
end

#errors<Vk::Definition::Error>

Returns:

  • (<Vk::Definition::Error>)


92
93
94
95
96
# File 'lib/vk/schema.rb', line 92

def errors
  @methods['errors'].map do |error|
    Vk::Schema::Definition::Error.new(error['name'], error, self)
  end
end

#methods<Vk::Definition::Error>

Returns:

  • (<Vk::Definition::Error>)


99
100
101
102
103
# File 'lib/vk/schema.rb', line 99

def methods
  @methods['methods'].map do |method|
    Definition::Method.new(method['name'], method, self)
  end
end

#namespaced_methods{String => Definition::methods}

Returns:

  • ({String => Definition::methods})


106
107
108
109
110
111
112
113
# File 'lib/vk/schema.rb', line 106

def namespaced_methods
  @namespaced_methods ||=
    methods.each_with_object({}) do |value, memo|
      next unless value.is_a?(Definition::Method)
      memo[value.namespace] ||= []
      memo[value.namespace] << value
    end
end

#namespaced_objects{String => Definition::Object}

Returns:



44
45
46
47
48
49
50
# File 'lib/vk/schema.rb', line 44

def namespaced_objects
  @namespaced_objects ||= objects_reference.values.each_with_object({}) do |value, memo|
    next unless value.is_a?(Definition::Object)
    memo[value.namespace] ||= []
    memo[value.namespace] << value
  end
end

#namespaced_types{String => Definition::Type}

Returns:



53
54
55
56
57
58
59
# File 'lib/vk/schema.rb', line 53

def namespaced_types
  @namespaced_types ||= objects_reference.values.each_with_object({}) do |value, memo|
    next unless value.is_a?(Definition::Type)
    memo[value.namespace] ||= []
    memo[value.namespace] << value
  end
end

#namespaces<String>

Returns:

  • (<String>)


136
137
138
139
140
141
142
# File 'lib/vk/schema.rb', line 136

def namespaces
  @namespaces ||= (
    namespaced_methods.keys |
      namespaced_objects.keys |
      namespaced_types.keys
  ).map(&:underscore)
end

#objects<Definition::Object>

Returns:



30
31
32
33
34
# File 'lib/vk/schema.rb', line 30

def objects
  objects_reference.values.select do |value|
    value.is_a?(Definition::Object)
  end
end

#objects_reference{String => Definition::Object, Definition::Type}

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vk/schema.rb', line 76

def objects_reference
  @objects_reference ||=
    @objects['definitions'].each_with_object({}) do |(name, definition), memo|
      memo[name] = if object?(definition)
                     Definition::Object.new(name, definition, self)
                   elsif definition['type']
                     Definition::Type.new(name, definition, self)
                   else
                     raise "What is #{name}, #{definition.inspect}?"
                   end
    end
end

#responses<Definition::Error>

Returns:



118
119
120
121
122
# File 'lib/vk/schema.rb', line 118

def responses
  @responses['definitions'].map do |name, response|
    Definition::Response.new(name, response, self)
  end
end

#schemas<Definition::Error>

Returns:



127
128
129
130
131
# File 'lib/vk/schema.rb', line 127

def schemas
  @schema['definitions'].map do |name, response|
    Definition::Response.new(name, response, self)
  end
end

#types<Definition::Type>

Returns:



37
38
39
40
41
# File 'lib/vk/schema.rb', line 37

def types
  objects_reference.values.select do |value|
    value.is_a?(Definition::Type)
  end
end