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

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

Class Method Summary collapse

Class Method Details

.action(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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/useless/doc/serialization/load.rb', line 51

def self.action(json)
  hash = json_to_hash json

  if hash['request']
    request = request hash['request']
  end

  if hash['response']
    response = response hash['response']
  end

  Useless::Doc::Action.new \
    description:              hash['description'],
    method:                   hash['method'],
    authentication_required:  hash['authentication_required'],
    request:                  request,
    response:                 response
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.



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

def self.body(json)
  hash = json_to_hash json

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

  Useless::Doc::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.



149
150
151
152
153
154
155
156
157
158
# File 'lib/useless/doc/serialization/load.rb', line 149

def self.body_attribute(json)
  hash = json_to_hash json

  Useless::Doc::Body::Attribute.new \
    key:          hash['key'],
    type:         hash['type'],
    required:     hash['required'],
    default:      hash['default'],
    description:  hash['description']
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.



127
128
129
130
131
132
133
# File 'lib/useless/doc/serialization/load.rb', line 127

def self.header(json)
  hash = json_to_hash json

  Useless::Doc::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.



25
26
27
# File 'lib/useless/doc/serialization/load.rb', line 25

def self.json_to_hash(json)
  json.is_a?(Hash) ? json : Oj.load(json)
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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/useless/doc/serialization/load.rb', line 71

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

  Useless::Doc::Request.new \
    parameters: parameters,
    headers:    headers,
    body:       body
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.



115
116
117
118
119
120
121
122
123
124
# File 'lib/useless/doc/serialization/load.rb', line 115

def self.request_parameter(json)
  hash = json_to_hash json

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

.resource(json) ⇒ Doc::Resource

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

Parameters:

  • json (String, Hash)

    the JSON representation to be converted to a resource.

Returns:

  • (Doc::Resource)

    the resource corresponding to the specified JSON.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/useless/doc/serialization/load.rb', line 37

def self.resource(json)
  hash = json_to_hash json

  actions = (hash['actions'] || []).map do |json|
    action(json)
  end

  Useless::Doc::Resource.new \
    path:         hash['path'],
    description:  hash['description'],
    actions:      actions
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.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/useless/doc/serialization/load.rb', line 93

def self.response(json)
  hash = json_to_hash json

  statuses = (hash['statuses'] || []).map do |json|
    response_statuses json
  end

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

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

  Useless::Doc::Response.new \
    statuses: statuses,
    headers:  headers,
    body:     body
end

.response_statuses(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.



161
162
163
164
165
166
167
# File 'lib/useless/doc/serialization/load.rb', line 161

def self.response_statuses(json)
  hash = json_to_hash json

  Useless::Doc::Response::Status.new \
    code:         hash['code'],
    description:  hash['description']
end