Module: Useless::Doc::Serialization::Load

Defined in:
lib/useless/doc/serialization/load.rb

Class Method Summary collapse

Class Method Details

.api(json) ⇒ Core::API

Converts a JSON represntation to an instance of Core::API

Parameters:

  • json (String, Hash)

    the JSON representation to be converted to an API.

Returns:

  • (Core::API)

    the API corresponding to the specified JSON.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/useless/doc/serialization/load.rb', line 69

def self.api(json)
  hash = json_to_hash json

  resources = (hash['resources'] || []).map do |json|
    resource json
  end

  Useless::Doc::Core::API.new \
    name:         hash['name'], 
    url:          hash['url'],
    description:  hash['description'],
    resources:    resources
end

.body(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/useless/doc/serialization/load.rb', line 175

def self.body(json)
  hash = json_to_hash json

  attributes = (hash['attributes'] || []).map do |json|
    body_attribute json
  end

  Useless::Doc::Core::Body.new \
    content_type: hash['content_type'],
    attributes:   attributes
end

.body_attribute(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



188
189
190
191
192
193
194
195
196
197
# File 'lib/useless/doc/serialization/load.rb', line 188

def self.body_attribute(json)
  hash = json_to_hash json

  Useless::Doc::Core::Body::Attribute.new \
    key:          hash['key'],
    type:         hash['type'],
    required:     hash['required'],
    default:      hash['default'],
    description:  hash['description']
end

.domain(json) ⇒ Core::Domain

Converts a JSON represntation to an instance of Core::Domain

Parameters:

  • json (String, Hash)

    the JSON representation to be converted to a domain.

Returns:

  • (Core::Domain)

    the domain corresponding to the specified JSON.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/useless/doc/serialization/load.rb', line 47

def self.domain(json)
  hash = json_to_hash json

  apis = (hash['apis'] || []).map do |json|
    api json
  end

  Useless::Doc::Core::Domain.new \
    name:         hash['name'], 
    url:          hash['url'],
    description:  hash['description'],
    apis:         apis
end

.header(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



166
167
168
169
170
171
172
# File 'lib/useless/doc/serialization/load.rb', line 166

def self.header(json)
  hash = json_to_hash json

  Useless::Doc::Core::Header.new \
    key:          hash['key'],
    description:  hash['description']
end

.json_to_hash(json) ⇒ Hash

Converts a JSON representation to a hash.

Parameters:

  • json (String, Hash)

    the JSON representation to be converted.

Returns:

  • (Hash)

    a hash corresponding to the specified JSON representation.

Raises:

  • (ArgumentError)

    if json is not a Hash, String or IO.



23
24
25
# File 'lib/useless/doc/serialization/load.rb', line 23

def self.json_to_hash(json)
  json.is_a?(Hash) ? json : Oj.load(json)
end

.load(json) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/useless/doc/serialization/load.rb', line 27

def self.load(json)
  hash = json_to_hash json

  if hash['apis']
    self.domain(hash)
  elsif hash['url']
    self.api(hash)
  elsif hash['path']
    self.resource(hash)
  end
end

.request(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/useless/doc/serialization/load.rb', line 105

def self.request(json)
  hash = json_to_hash json

  parameters = (hash['parameters'] || []).map do |json|
    request_parameter json
  end

  headers = (hash['headers'] || []).map do |json|
    header json
  end

  if hash['body']
    body = body hash['body']
  end

  responses = (hash['responses'] || []).map do |json|
    response json
  end

  Useless::Doc::Core::Request.new \
    method:                   hash['method'],
    description:              hash['description'],
    authentication_required:  hash['authentication_required'],
    parameters:               parameters,
    headers:                  headers,
    body:                     body,
    responses:                responses
end

.request_parameter(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
157
158
159
160
161
162
163
# File 'lib/useless/doc/serialization/load.rb', line 154

def self.request_parameter(json)
  hash = json_to_hash json

  Useless::Doc::Core::Request::Parameter.new \
    type:         hash['type'],
    key:          hash['key'],
    required:     hash['required'],
    default:      hash['default'],
    description:  hash['description']
end

.resource(json) ⇒ Core::Resource

Converts a JSON represntation to an instance of Core::Resource

Parameters:

  • json (String, Hash)

    the JSON representation to be converted to a resource.

Returns:

  • (Core::Resource)

    the resource corresponding to the specified JSON.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/useless/doc/serialization/load.rb', line 91

def self.resource(json)
  hash = json_to_hash json

  requests = (hash['requests'] || []).map do |json|
    request json
  end

  Useless::Doc::Core::Resource.new \
    path:         hash['path'],
    description:  hash['description'],
    requests:     requests
end

.response(json) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/useless/doc/serialization/load.rb', line 135

def self.response(json)
  hash = json_to_hash json

  headers = (hash['headers'] || []).map do |json|
    header json
  end

  if hash['body']
    body = body hash['body']
  end

  Useless::Doc::Core::Response.new \
    code:         hash['code'],
    description:  hash['description'], 
    headers:      headers,
    body:         body
end