purple-client 0.1.0.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 066bed26b96e48e935797201e73754b1e04db86788fdca81935c412daed48e8f
4
- data.tar.gz: 47106e21ab8fc4607a84a4ea89e85653c83c5c182aa42192228ff6164e5eaef3
3
+ metadata.gz: 9d18a27dc35385412674a327de6cb947d1b4d8efe4e4032acc9edc1513ce24da
4
+ data.tar.gz: 3611ed61c25f22b6f9f458894ca52bf611b10874051073485e9e67317529c3b5
5
5
  SHA512:
6
- metadata.gz: 3e188539cd06534badf64c79abb833756e410ff42576804e7f1f86f34c6b44332766804496d903b9ba7cb25f6cb6f2e690e07eda1ee0f03dcb8d2af39d4ae436
7
- data.tar.gz: c461496af7c52405b2c2251201b3ad57a5add4c06e2f94fd0be2d80951933a942e753e06d03b8be5d4dcca15741d4c72b11a7f72bc58b21bc815f03f14f00ac6
6
+ metadata.gz: 5e5bc2479a7b5b259113e76ca91d1578a180afa3b792c12608bdb03d0c23b28e5ddfd1aa3ee3ecf96b32700fd140ed84dcb6330b19da3fd908ea97fa14600f2d
7
+ data.tar.gz: 30e8ca1120a40557ffda8a51c531ee06c34807e3696ff424c056bf237819765b6f0171c115da9089234088e4a4ae9b32d366a67143149112a401c7862ea1bad0
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
@@ -53,14 +53,24 @@ module Purple
53
53
 
54
54
  resp_structure = responses.find { |resp| resp.status_code == response.status }
55
55
 
56
- object = resp_structure.body.validate!(response.body, args) if resp_structure
56
+ if resp_structure.nil?
57
+ raise "#{client.domain}/#{full_path} returns #{response.status}, but it is not defined in the client"
58
+ else
59
+ object = if resp_structure.body.is_a?(Purple::Responses::Body)
60
+ resp_structure.body.validate!(response.body, args)
61
+ elsif resp_structure.body == :default
62
+ response.body
63
+ else
64
+ {}
65
+ end
57
66
 
58
- client.callback&.call(url, params, headers, JSON.parse(response.body))
67
+ client.callback&.call(url, params, headers, JSON.parse(response.body))
59
68
 
60
- if block_given?
61
- yield(resp_structure.status, object)
62
- else
63
- object
69
+ if block_given?
70
+ yield(resp_structure.status, object)
71
+ else
72
+ object
73
+ end
64
74
  end
65
75
  end
66
76
  end
@@ -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.1"
4
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-01 00:00:00.000000000 Z
11
+ date: 2025-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -55,7 +55,7 @@ metadata:
55
55
  homepage_uri: https://github.com/purple-magic/purple-client
56
56
  source_code_uri: https://github.com/purple-magic/purple-client
57
57
  changelog_uri: https://github.com/Purple-Magic/purple-client/releases
58
- post_install_message:
58
+ post_install_message:
59
59
  rdoc_options: []
60
60
  require_paths:
61
61
  - lib
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  version: '0'
72
72
  requirements: []
73
73
  rubygems_version: 3.4.6
74
- signing_key:
74
+ signing_key:
75
75
  specification_version: 4
76
76
  summary: Build API wrappers faster
77
77
  test_files: []