purple-client 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/purple/client.rb +7 -2
- data/lib/purple/path.rb +9 -1
- data/lib/purple/responses/body.rb +11 -2
- data/lib/purple/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52d949ade3816a9c300a86183d10aadf41052ce9b5532651bbb45824f6e731ca
|
4
|
+
data.tar.gz: 0a0b9f5bf1ff5f4f8acbd45a813d0c79251422c133bc11566b40fca97f51ed61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2b509535d9dac1de4611d17a8eee8a51d16c6621762d59212fbde9aa1c0b99197349e2fed2b829f3335f04e1da88485618730f922aef7be8b086bfc38ded3e2
|
7
|
+
data.tar.gz: 24ef0a418979210df30bd2fc3c8e33bc5d6b3d9899b6d2dd72c44683689553b0a1b1e59dba8a77645b57295455d9cc71cb937ac4f2bb0ef9d7867d2f779a0885
|
data/lib/purple/client.rb
CHANGED
@@ -90,8 +90,13 @@ module Purple
|
|
90
90
|
yield if block_given?
|
91
91
|
end
|
92
92
|
|
93
|
-
def body(
|
94
|
-
|
93
|
+
def body(type = nil, **structure, &block)
|
94
|
+
case type
|
95
|
+
when :default
|
96
|
+
@current_resp.body = :default
|
97
|
+
else
|
98
|
+
@current_resp.body = Responses::Body.new(structure:, response: @current_resp, transform: block)
|
99
|
+
end
|
95
100
|
end
|
96
101
|
|
97
102
|
def method_missing(method_name, *args, &)
|
data/lib/purple/path.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'dry-initializer'
|
4
|
+
|
3
5
|
module Purple
|
4
6
|
class Path
|
5
7
|
extend Dry::Initializer[undefined: false]
|
@@ -51,7 +53,13 @@ module Purple
|
|
51
53
|
|
52
54
|
resp_structure = responses.find { |resp| resp.status_code == response.status }
|
53
55
|
|
54
|
-
object = resp_structure.body.
|
56
|
+
object = if resp_structure.body.is_a?(Purple::Responses::Body)
|
57
|
+
resp_structure.body.validate!(response.body, args)
|
58
|
+
elsif resp_structure.body == :default
|
59
|
+
response.body
|
60
|
+
else
|
61
|
+
{}
|
62
|
+
end
|
55
63
|
|
56
64
|
client.callback&.call(url, params, headers, JSON.parse(response.body))
|
57
65
|
|
@@ -69,13 +69,18 @@ class Purple::Responses::Body
|
|
69
69
|
def check_structure!(object, substructure = structure)
|
70
70
|
substructure.each do |key, value|
|
71
71
|
if value.is_a?(Hash)
|
72
|
-
if
|
72
|
+
if options?(value)
|
73
73
|
next if value[:optional]
|
74
|
+
next if value[:allow_blank] && object[key].blank?
|
74
75
|
|
75
76
|
check_type!(object, key, value[:type])
|
76
77
|
else
|
77
78
|
check_structure!(object[key], substructure[key])
|
78
79
|
end
|
80
|
+
elsif value.is_a?(Array)
|
81
|
+
object[key].each do |item|
|
82
|
+
check_structure!(item, value[0])
|
83
|
+
end
|
79
84
|
else
|
80
85
|
check_type!(object, key, value)
|
81
86
|
end
|
@@ -85,11 +90,15 @@ class Purple::Responses::Body
|
|
85
90
|
def check_type!(object, key, expected_type)
|
86
91
|
unless object.key?(key)
|
87
92
|
raise BodyStructureMismatchError.new(key, expected_type, object[key], object),
|
88
|
-
|
93
|
+
"Missing field '#{key}' in response body. Body: #{object}"
|
89
94
|
end
|
90
95
|
|
91
96
|
return if object[key].is_a?(expected_type)
|
92
97
|
|
93
98
|
raise BodyStructureMismatchError.new(key, expected_type, object[key], object)
|
94
99
|
end
|
100
|
+
|
101
|
+
def options?(hash)
|
102
|
+
hash.key?(:type) && (hash.key?(:optional) || hash.key?(:allow_blank))
|
103
|
+
end
|
95
104
|
end
|
data/lib/purple/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: purple-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-initializer
|