opushon 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION.semver +1 -1
- data/lib/opushon/body.rb +55 -17
- data/lib/opushon/option.rb +34 -9
- data/lib/opushon/parameter.rb +96 -29
- data/lib/opushon/request.rb +29 -7
- data/lib/opushon/response.rb +24 -5
- data/lib/opushon/restricted_value.rb +29 -7
- data/lib/opushon.rb +1 -2
- data/opushon.gemspec +1 -3
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ada98b18f896449e5d7218ca1dcdfef49a4e107fab06800ed65e5af0c7f4ad3
|
4
|
+
data.tar.gz: 8ed7fdc8fa62f6198c0d3109b1d143e10dc4f115adb907b7dbdd06b7525a2621
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4400ebd14bb0079c7d0f67569fe444e042e4f741b26490030b7f8b72561e35e33e16f41a5d6570cbea6d62b2b485d74eb8e904b68beb1435900541d1a522bed8
|
7
|
+
data.tar.gz: 2862fc3e80b1c02a046306fda88b75ad1efb5c4738591a66520acb56f054a16d1d9576bd063026ad44f46fef07f0fe2e1e74b98f90893faaa11a6f06a9e52525
|
data/VERSION.semver
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/opushon/body.rb
CHANGED
@@ -6,32 +6,70 @@ module Opushon
|
|
6
6
|
# The body structure is a hash where each key is a HTTP method and each value
|
7
7
|
# is a sub-hash, called an option object.
|
8
8
|
class Body
|
9
|
-
|
9
|
+
def self.load(hash)
|
10
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
get = hash.fetch('GET', nil)
|
13
|
+
patch = hash.fetch('PATCH', nil)
|
14
|
+
put = hash.fetch('PUT', nil)
|
15
|
+
post = hash.fetch('POST', nil)
|
16
|
+
delete = hash.fetch('DELETE', nil)
|
17
|
+
|
18
|
+
hash = {
|
19
|
+
get: get,
|
20
|
+
patch: patch,
|
21
|
+
put: put,
|
22
|
+
post: post,
|
23
|
+
delete: delete
|
24
|
+
}.compact
|
25
|
+
|
26
|
+
new(**hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :get, :patch, :put, :post, :delete
|
30
|
+
|
31
|
+
def initialize(get: nil, patch: nil, put: nil, post: nil, delete: nil)
|
32
|
+
unless get.nil?
|
33
|
+
raise ArgumentError, "get #{get.inspect}" unless get.is_a?(Hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
unless patch.nil?
|
37
|
+
raise ArgumentError, "patch #{patch.inspect}" unless patch.is_a?(Hash)
|
38
|
+
end
|
39
|
+
|
40
|
+
unless put.nil?
|
41
|
+
raise ArgumentError, "put #{put.inspect}" unless put.is_a?(Hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
unless post.nil?
|
45
|
+
raise ArgumentError, "post #{post.inspect}" unless post.is_a?(Hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
unless delete.nil?
|
49
|
+
raise ArgumentError, "delete #{delete.inspect}" unless delete.is_a?(Hash)
|
50
|
+
end
|
51
|
+
|
52
|
+
@get = Option.load(get) unless get.nil?
|
53
|
+
@patch = Option.load(patch) unless patch.nil?
|
54
|
+
@put = Option.load(put) unless put.nil?
|
55
|
+
@post = Option.load(post) unless post.nil?
|
56
|
+
@delete = Option.load(delete) unless delete.nil?
|
57
|
+
end
|
16
58
|
|
17
59
|
def to_h
|
18
60
|
{
|
19
|
-
GET: maybe_to_h(
|
20
|
-
PATCH: maybe_to_h(
|
21
|
-
PUT: maybe_to_h(
|
22
|
-
POST: maybe_to_h(
|
23
|
-
DELETE: maybe_to_h(
|
24
|
-
}
|
61
|
+
GET: maybe_to_h(get),
|
62
|
+
PATCH: maybe_to_h(patch),
|
63
|
+
PUT: maybe_to_h(put),
|
64
|
+
POST: maybe_to_h(post),
|
65
|
+
DELETE: maybe_to_h(delete)
|
66
|
+
}.compact
|
25
67
|
end
|
26
68
|
|
27
69
|
private
|
28
70
|
|
29
71
|
def maybe_to_h(object)
|
30
|
-
|
31
|
-
nil
|
32
|
-
else
|
33
|
-
object.to_h
|
34
|
-
end
|
72
|
+
object&.to_h
|
35
73
|
end
|
36
74
|
end
|
37
75
|
end
|
data/lib/opushon/option.rb
CHANGED
@@ -5,19 +5,44 @@ require_relative 'response'
|
|
5
5
|
|
6
6
|
module Opushon
|
7
7
|
class Option
|
8
|
-
|
8
|
+
def self.load(hash)
|
9
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
title = hash.fetch('title', nil)
|
12
|
+
description = hash.fetch('description', nil)
|
13
|
+
request = hash.fetch('request', nil)
|
14
|
+
response = hash.fetch('response', nil)
|
15
|
+
|
16
|
+
hash = {
|
17
|
+
title: title,
|
18
|
+
description: description,
|
19
|
+
request: request,
|
20
|
+
response: response
|
21
|
+
}.compact
|
22
|
+
|
23
|
+
new(**hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :title, :description, :request, :response
|
27
|
+
|
28
|
+
def initialize(title: '', description: '', request: {}, response: {})
|
29
|
+
raise ArgumentError, "title #{title.inspect}" unless title.is_a?(String)
|
30
|
+
raise ArgumentError, "description #{description.inspect}" unless description.is_a?(String)
|
31
|
+
raise ArgumentError, "request #{request.inspect}" unless request.is_a?(Hash)
|
32
|
+
raise ArgumentError, "response #{response.inspect}" unless response.is_a?(Hash)
|
33
|
+
|
34
|
+
@title = title
|
35
|
+
@description = description
|
36
|
+
@request = Request.load(request)
|
37
|
+
@response = Response.load(response)
|
38
|
+
end
|
14
39
|
|
15
40
|
def to_h
|
16
41
|
{
|
17
|
-
title:
|
18
|
-
description:
|
19
|
-
request:
|
20
|
-
response:
|
42
|
+
title: title,
|
43
|
+
description: description,
|
44
|
+
request: request.to_h,
|
45
|
+
response: response.to_h
|
21
46
|
}
|
22
47
|
end
|
23
48
|
end
|
data/lib/opushon/parameter.rb
CHANGED
@@ -6,42 +6,109 @@ module Opushon
|
|
6
6
|
# The content of headers, query string and body params MUST be described with
|
7
7
|
# the keys below. When a key is missing, its default value is assigned.
|
8
8
|
class Parameter
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
9
|
+
def self.load(hash)
|
10
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
11
|
+
|
12
|
+
title = hash.fetch('title', nil)
|
13
|
+
description = hash.fetch('description', nil)
|
14
|
+
type = hash.fetch('type', nil)
|
15
|
+
nullifiable = hash.fetch('nullifiable', nil)
|
16
|
+
restricted_values = hash.fetch('restricted_values', nil)
|
17
|
+
example = hash.fetch('example', nil)
|
18
|
+
minlen = hash.fetch('minlen', nil)
|
19
|
+
maxlen = hash.fetch('maxlen', nil)
|
20
|
+
pattern = hash.fetch('pattern', nil)
|
21
|
+
min = hash.fetch('min', nil)
|
22
|
+
max = hash.fetch('max', nil)
|
23
|
+
|
24
|
+
hash = {
|
25
|
+
title: title,
|
26
|
+
description: description,
|
27
|
+
type: type,
|
28
|
+
nullifiable: nullifiable,
|
29
|
+
restricted_values: restricted_values,
|
30
|
+
example: example,
|
31
|
+
minlen: minlen,
|
32
|
+
maxlen: maxlen,
|
33
|
+
pattern: pattern,
|
34
|
+
min: min,
|
35
|
+
max: max
|
36
|
+
}.compact
|
37
|
+
|
38
|
+
new(**hash)
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_reader :title, :description, :type, :nullifiable, :restricted_values, :example, :minlen, :maxlen, :pattern, :min, :max
|
42
|
+
|
43
|
+
def initialize(title: '', description: '', type: 'string', nullifiable: true, restricted_values: nil, example: nil, minlen: nil, maxlen: nil, pattern: nil, min: nil, max: nil)
|
44
|
+
raise ArgumentError, "title #{title.inspect}" unless title.is_a?(String)
|
45
|
+
raise ArgumentError, "description #{description.inspect}" unless description.is_a?(String)
|
46
|
+
raise ArgumentError, "type #{type.inspect}" unless type.is_a?(String)
|
47
|
+
raise ArgumentError, "nullifiable #{nullifiable.inspect}" unless [false, true].include?(nullifiable)
|
48
|
+
|
49
|
+
unless restricted_values.nil?
|
50
|
+
raise ArgumentError, "restricted_values #{restricted_values.inspect}" unless restricted_values.is_a?(Array)
|
51
|
+
end
|
52
|
+
|
53
|
+
unless example.nil?
|
54
|
+
raise ArgumentError, "example #{example.inspect}" unless example.is_a?(BasicObject)
|
55
|
+
end
|
56
|
+
|
57
|
+
unless minlen.nil?
|
58
|
+
raise ArgumentError, "minlen #{minlen.inspect}" unless minlen.is_a?(Integer)
|
59
|
+
end
|
60
|
+
|
61
|
+
unless maxlen.nil?
|
62
|
+
raise ArgumentError, "maxlen #{maxlen.inspect}" unless maxlen.is_a?(Integer)
|
63
|
+
end
|
64
|
+
|
65
|
+
unless pattern.nil?
|
66
|
+
raise ArgumentError, "pattern #{pattern.inspect}" unless pattern.is_a?(String)
|
67
|
+
end
|
68
|
+
|
69
|
+
unless min.nil?
|
70
|
+
raise ArgumentError, "min #{min.inspect}" unless min.is_a?(Integer)
|
71
|
+
end
|
72
|
+
|
73
|
+
unless max.nil?
|
74
|
+
raise ArgumentError, "max #{max.inspect}" unless max.is_a?(Integer)
|
75
|
+
end
|
76
|
+
|
77
|
+
@title = title
|
78
|
+
@description = description
|
79
|
+
@type = type
|
80
|
+
@nullifiable = nullifiable
|
81
|
+
@restricted_values = restricted_values.map { |restricted_value| RestrictedValue.load(restricted_value) } unless restricted_values.nil?
|
82
|
+
@example = example
|
83
|
+
@minlen = minlen
|
84
|
+
@maxlen = maxlen
|
85
|
+
@pattern = Regexp.new(pattern) unless pattern.nil?
|
86
|
+
@min = min
|
87
|
+
@max = max
|
88
|
+
end
|
22
89
|
|
23
90
|
def to_h
|
24
91
|
h = {
|
25
|
-
title:
|
26
|
-
description:
|
27
|
-
type:
|
28
|
-
nullifiable:
|
29
|
-
restricted_values:
|
30
|
-
example:
|
92
|
+
title: title,
|
93
|
+
description: description,
|
94
|
+
type: type,
|
95
|
+
nullifiable: nullifiable,
|
96
|
+
restricted_values: maybe_to_a_of_h(restricted_values),
|
97
|
+
example: example
|
31
98
|
}
|
32
99
|
|
33
|
-
if
|
34
|
-
h.
|
35
|
-
minlen:
|
36
|
-
maxlen:
|
37
|
-
pattern:
|
100
|
+
if type.eql?('string')
|
101
|
+
return h.merge(
|
102
|
+
minlen: minlen,
|
103
|
+
maxlen: maxlen,
|
104
|
+
pattern: pattern&.to_s
|
38
105
|
)
|
39
106
|
end
|
40
107
|
|
41
|
-
if
|
42
|
-
h.
|
43
|
-
min:
|
44
|
-
max:
|
108
|
+
if type.eql?('number')
|
109
|
+
return h.merge(
|
110
|
+
min: min,
|
111
|
+
max: max
|
45
112
|
)
|
46
113
|
end
|
47
114
|
|
@@ -50,7 +117,7 @@ module Opushon
|
|
50
117
|
|
51
118
|
private
|
52
119
|
|
53
|
-
def
|
120
|
+
def maybe_to_a_of_h(object)
|
54
121
|
return if object.nil?
|
55
122
|
|
56
123
|
object.map(&:to_h)
|
data/lib/opushon/request.rb
CHANGED
@@ -4,17 +4,39 @@ require_relative 'parameter'
|
|
4
4
|
|
5
5
|
module Opushon
|
6
6
|
class Request
|
7
|
-
|
7
|
+
def self.load(hash)
|
8
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
headers = hash.fetch('headers', nil)
|
11
|
+
query_string = hash.fetch('query_string', nil)
|
12
|
+
body = hash.fetch('body', nil)
|
13
|
+
|
14
|
+
hash = {
|
15
|
+
headers: headers,
|
16
|
+
query_string: query_string,
|
17
|
+
body: body
|
18
|
+
}.compact
|
19
|
+
|
20
|
+
new(**hash)
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :headers, :query_string, :body
|
24
|
+
|
25
|
+
def initialize(headers: {}, query_string: {}, body: {})
|
26
|
+
raise ArgumentError, "headers #{headers.inspect}" unless headers.is_a?(Hash)
|
27
|
+
raise ArgumentError, "query_string #{query_string.inspect}" unless query_string.is_a?(Hash)
|
28
|
+
raise ArgumentError, "body #{body.inspect}" unless body.is_a?(Hash)
|
29
|
+
|
30
|
+
@headers = headers.map { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
|
31
|
+
@query_string = query_string.map { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
|
32
|
+
@body = body.map { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
|
33
|
+
end
|
12
34
|
|
13
35
|
def to_h
|
14
36
|
{
|
15
|
-
headers:
|
16
|
-
query_string:
|
17
|
-
body:
|
37
|
+
headers: headers.map { |k, v| [k, v.to_h] }.to_h,
|
38
|
+
query_string: query_string.map { |k, v| [k, v.to_h] }.to_h,
|
39
|
+
body: body.map { |k, v| [k, v.to_h] }.to_h
|
18
40
|
}
|
19
41
|
end
|
20
42
|
end
|
data/lib/opushon/response.rb
CHANGED
@@ -4,15 +4,34 @@ require_relative 'parameter'
|
|
4
4
|
|
5
5
|
module Opushon
|
6
6
|
class Response
|
7
|
-
|
7
|
+
def self.load(hash)
|
8
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
headers = hash.fetch('headers', nil)
|
11
|
+
body = hash.fetch('body', nil)
|
12
|
+
|
13
|
+
hash = {
|
14
|
+
headers: headers,
|
15
|
+
body: body
|
16
|
+
}.compact
|
17
|
+
|
18
|
+
new(**hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :headers, :body
|
22
|
+
|
23
|
+
def initialize(headers: {}, body: {})
|
24
|
+
raise ArgumentError, "headers #{headers.inspect}" unless headers.is_a?(Hash)
|
25
|
+
raise ArgumentError, "body #{body.inspect}" unless body.is_a?(Hash)
|
26
|
+
|
27
|
+
@headers = headers.map { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
|
28
|
+
@body = body.map { |k, v| [k.to_sym, Parameter.load(v)] }.to_h
|
29
|
+
end
|
11
30
|
|
12
31
|
def to_h
|
13
32
|
{
|
14
|
-
headers:
|
15
|
-
body:
|
33
|
+
headers: headers.map { |k, v| [k, v.to_h] }.to_h,
|
34
|
+
body: body.map { |k, v| [k, v.to_h] }.to_h
|
16
35
|
}
|
17
36
|
end
|
18
37
|
end
|
@@ -4,17 +4,39 @@ module Opushon
|
|
4
4
|
# If present, the value of restricted_values is an array where each item
|
5
5
|
# contains a hash.
|
6
6
|
class RestrictedValue
|
7
|
-
|
7
|
+
def self.load(hash)
|
8
|
+
raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
title = hash.fetch('title', nil)
|
11
|
+
description = hash.fetch('description', nil)
|
12
|
+
value = hash.fetch('value', nil)
|
13
|
+
|
14
|
+
hash = {
|
15
|
+
title: title,
|
16
|
+
description: description,
|
17
|
+
value: value
|
18
|
+
}.compact
|
19
|
+
|
20
|
+
new(**hash)
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :title, :description, :value
|
24
|
+
|
25
|
+
def initialize(title: '', description: '', value:)
|
26
|
+
raise ArgumentError, "title #{title.inspect}" unless title.is_a?(String)
|
27
|
+
raise ArgumentError, "description #{description.inspect}" unless description.is_a?(String)
|
28
|
+
raise ArgumentError, "value #{value.inspect}" unless value.is_a?(BasicObject)
|
29
|
+
|
30
|
+
@title = title
|
31
|
+
@description = description
|
32
|
+
@value = value
|
33
|
+
end
|
12
34
|
|
13
35
|
def to_h
|
14
36
|
{
|
15
|
-
title:
|
16
|
-
description:
|
17
|
-
value:
|
37
|
+
title: title,
|
38
|
+
description: description,
|
39
|
+
value: value
|
18
40
|
}
|
19
41
|
end
|
20
42
|
end
|
data/lib/opushon.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'virtus'
|
4
3
|
require 'json'
|
5
4
|
|
6
5
|
# Namespace for the Opushon library.
|
@@ -20,7 +19,7 @@ module Opushon
|
|
20
19
|
# @return [Body] the Ruby data structure
|
21
20
|
def self.load(opushon_string)
|
22
21
|
opushon_hash = JSON.parse(opushon_string)
|
23
|
-
Body.
|
22
|
+
Body.load(opushon_hash)
|
24
23
|
end
|
25
24
|
|
26
25
|
# Dump Ruby object to a Opushon string.
|
data/opushon.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.email = ['contact@cyril.email']
|
8
8
|
|
9
9
|
spec.summary = 'A HTTP Opushon parser and emitter'
|
10
|
-
spec.description =
|
10
|
+
spec.description = "An Opushon's body parser and emitter."
|
11
11
|
spec.homepage = 'https://github.com/cyril/opushon.rb'
|
12
12
|
spec.license = 'MIT'
|
13
13
|
|
@@ -16,8 +16,6 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
17
17
|
spec.require_paths = ['lib']
|
18
18
|
|
19
|
-
spec.add_dependency 'virtus', '~> 1.0.5'
|
20
|
-
|
21
19
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
22
20
|
spec.add_development_dependency 'rake', '~> 12.3'
|
23
21
|
spec.add_development_dependency 'simplecov', '~> 0.17'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: opushon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cyril Kato
|
@@ -10,20 +10,6 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2019-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: virtus
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.5
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.5
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|