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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ad7fb028a6cea59812f508ffa5eac53ceb4655f6d8fdd1cc27e1536194459ca
4
- data.tar.gz: 67cf8b6c40a3f0b2a4f7d87a4b170e80bc752a243c9ba346cdd0f5b2490b4801
3
+ metadata.gz: 52d949ade3816a9c300a86183d10aadf41052ce9b5532651bbb45824f6e731ca
4
+ data.tar.gz: 0a0b9f5bf1ff5f4f8acbd45a813d0c79251422c133bc11566b40fca97f51ed61
5
5
  SHA512:
6
- metadata.gz: 12f5d02c393428a5d74fb61f1b6c62338c501313760c5eb46de4d5682260c8eac8178e6b49da873987e88dbe485e1be9e53c0ad8e82ce02a235a529295005706
7
- data.tar.gz: 168a92b2190f1d0304ed8e6f4ea21fcb0912fc8f6d2b44c1d1f5a6541deebf14f6d55373733d79d334b637b5cb236c47f0b1a176c41d4bb7588fbb92a7b02292
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(_type = nil, **structure, &block)
94
- @current_resp.body = Responses::Body.new(structure:, response: @current_resp, transform: block)
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.validate!(response.body, args) if resp_structure
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 value.key?(:optional) && value.key?(:type)
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
- "Missing field '#{key}' in response body. Body: #{object}"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Purple
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
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.0
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-01 00:00:00.000000000 Z
11
+ date: 2024-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer