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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24fefd1822417befed8d3b8678ccda2e3f05eb7462617cdfd9bcf1d63302cded
4
- data.tar.gz: 057236242425accea1ed1696b370b42cfbeedcf4cfce124734e3f6f0cac75552
3
+ metadata.gz: 8ada98b18f896449e5d7218ca1dcdfef49a4e107fab06800ed65e5af0c7f4ad3
4
+ data.tar.gz: 8ed7fdc8fa62f6198c0d3109b1d143e10dc4f115adb907b7dbdd06b7525a2621
5
5
  SHA512:
6
- metadata.gz: 046e40f75c5a8bee027928d7b2af0da0fe00f6bd9d8d8a7732a959e643dbb93a373f7de43c479c4cb98e054eba7cd685a54796f1b55e628f6ab914c7f64f5d38
7
- data.tar.gz: 1d95889786e06951548c92680e7a8795d97225839ba6a52aeb89723ac1bf0f8b6fb04fe82f022d1ac293783df6599483323f8b635c46032c20ab7d4503f07291
6
+ metadata.gz: 4400ebd14bb0079c7d0f67569fe444e042e4f741b26490030b7f8b72561e35e33e16f41a5d6570cbea6d62b2b485d74eb8e904b68beb1435900541d1a522bed8
7
+ data.tar.gz: 2862fc3e80b1c02a046306fda88b75ad1efb5c4738591a66520acb56f054a16d1d9576bd063026ad44f46fef07f0fe2e1e74b98f90893faaa11a6f06a9e52525
data/VERSION.semver CHANGED
@@ -1 +1 @@
1
- 0.2.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
- include Virtus.model(strict: true)
9
+ def self.load(hash)
10
+ raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
10
11
 
11
- attribute :GET, Option, required: false
12
- attribute :PATCH, Option, required: false
13
- attribute :PUT, Option, required: false
14
- attribute :POST, Option, required: false
15
- attribute :DELETE, Option, required: false
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(@GET),
20
- PATCH: maybe_to_h(@PATCH),
21
- PUT: maybe_to_h(@PUT),
22
- POST: maybe_to_h(@POST),
23
- DELETE: maybe_to_h(@DELETE)
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
- if object.nil?
31
- nil
32
- else
33
- object.to_h
34
- end
72
+ object&.to_h
35
73
  end
36
74
  end
37
75
  end
@@ -5,19 +5,44 @@ require_relative 'response'
5
5
 
6
6
  module Opushon
7
7
  class Option
8
- include Virtus.model(strict: true)
8
+ def self.load(hash)
9
+ raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
9
10
 
10
- attribute :title, String, default: '', required: true
11
- attribute :description, String, default: '', required: true
12
- attribute :request, Request, default: Request.new, required: true
13
- attribute :response, Response, default: Response.new, required: true
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: @title,
18
- description: @description,
19
- request: @request.to_h,
20
- response: @response.to_h
42
+ title: title,
43
+ description: description,
44
+ request: request.to_h,
45
+ response: response.to_h
21
46
  }
22
47
  end
23
48
  end
@@ -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
- include Virtus.model(strict: true)
10
-
11
- attribute :title, String, default: '', required: true
12
- attribute :description, String, default: '', required: true
13
- attribute :type, String, default: 'string', required: true
14
- attribute :nullifiable, Boolean, default: true, required: true
15
- attribute :restricted_values, Array[RestrictedValue], required: false
16
- attribute :example, BasicObject, required: false
17
- attribute :minlen, Integer, required: false
18
- attribute :maxlen, Integer, required: false
19
- attribute :pattern, Regexp, required: false
20
- attribute :min, Integer, required: false
21
- attribute :max, Integer, required: false
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: @title,
26
- description: @description,
27
- type: @type,
28
- nullifiable: @nullifiable,
29
- restricted_values: maybe_to_a(@restricted_values),
30
- example: @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 @type.eql?('string')
34
- h.update(
35
- minlen: @minlen,
36
- maxlen: @maxlen,
37
- pattern: @pattern&.to_s
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 @type.eql?('number')
42
- h.update(
43
- min: @min,
44
- max: @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 maybe_to_a(object)
120
+ def maybe_to_a_of_h(object)
54
121
  return if object.nil?
55
122
 
56
123
  object.map(&:to_h)
@@ -4,17 +4,39 @@ require_relative 'parameter'
4
4
 
5
5
  module Opushon
6
6
  class Request
7
- include Virtus.model(strict: true)
7
+ def self.load(hash)
8
+ raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
8
9
 
9
- attribute :headers, Hash[Symbol => Parameter], default: {}, required: true
10
- attribute :query_string, Hash[Symbol => Parameter], default: {}, required: true
11
- attribute :body, Hash[Symbol => Parameter], default: {}, required: true
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: @headers.map { |k, v| [k, v.to_h] }.to_h,
16
- query_string: @query_string.map { |k, v| [k, v.to_h] }.to_h,
17
- body: @body.map { |k, v| [k, v.to_h] }.to_h
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
@@ -4,15 +4,34 @@ require_relative 'parameter'
4
4
 
5
5
  module Opushon
6
6
  class Response
7
- include Virtus.model(strict: true)
7
+ def self.load(hash)
8
+ raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
8
9
 
9
- attribute :headers, Hash[Symbol => Parameter], default: {}, required: true
10
- attribute :body, Hash[Symbol => Parameter], default: {}, required: true
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: @headers.map { |k, v| [k, v.to_h] }.to_h,
15
- body: @body.map { |k, v| [k, v.to_h] }.to_h
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
- include Virtus.model(strict: true)
7
+ def self.load(hash)
8
+ raise ArgumentError, "hash #{hash.inspect}" unless hash.is_a?(Hash)
8
9
 
9
- attribute :title, String, default: '', required: true
10
- attribute :description, String, default: '', required: true
11
- attribute :value, BasicObject, required: true
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: @title,
16
- description: @description,
17
- value: @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.new(opushon_hash)
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 = 'An Opushon\'s body parser and emitter.'
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.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