eac_envs-http 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eac_envs/http/response/body.rb +57 -0
- data/lib/eac_envs/http/response/headers.rb +41 -0
- data/lib/eac_envs/http/response/links.rb +28 -0
- data/lib/eac_envs/http/response/statuses.rb +21 -0
- data/lib/eac_envs/http/response.rb +2 -92
- data/lib/eac_envs/http/version.rb +1 -1
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91ef89fd4cd3ad1bb09137c2c314dbeb1014b473a31d6b3a619afd5a25f49011
|
4
|
+
data.tar.gz: 3d1cef722ed45d04b87ad33c7ed3b4ad14e908a311fbb5d1ce83a363491e8bc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52fc388acde1ab6a9f8b45f4939dc79cfc39c6de87de58cc4f94e6219f1ef70f111db18ed951c86a6f41a8f71008d062f16162d21bc8756a03867ea021111d1b
|
7
|
+
data.tar.gz: 7f38854cabb1bc75efdfe04ed201a4f1a4ef360707e99e822b32c00959c33717103f823c6ea99f5d7871d54776d4eda9346a94238ceb41bb71fcf98f33b8bdcf
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module EacEnvs
|
7
|
+
module Http
|
8
|
+
class Response < ::StandardError
|
9
|
+
module Body
|
10
|
+
delegate :response_body_data_proc, to: :request
|
11
|
+
|
12
|
+
def body_data
|
13
|
+
r = body_data_method_name.if_present(body_str) do |v|
|
14
|
+
respond_to?(v, true) ? send(v) : body_str
|
15
|
+
end
|
16
|
+
r = response_body_data_proc.call(r) if response_body_data_proc.present?
|
17
|
+
r
|
18
|
+
end
|
19
|
+
|
20
|
+
def body_data_or_raise
|
21
|
+
raise_unless_200
|
22
|
+
|
23
|
+
body_data
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [String]
|
27
|
+
def body_str
|
28
|
+
performed.body
|
29
|
+
end
|
30
|
+
|
31
|
+
def body_str_or_raise
|
32
|
+
raise_unless_200
|
33
|
+
|
34
|
+
body_str
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def body_data_from_application_json
|
40
|
+
::JSON.parse(body_str)
|
41
|
+
end
|
42
|
+
|
43
|
+
def body_data_from_application_xml
|
44
|
+
Hash.from_xml(body_str)
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [String]
|
48
|
+
def body_data_method_name
|
49
|
+
content_type.if_present do |v|
|
50
|
+
'body_data_from_' +
|
51
|
+
v.split(';').first.force_encoding(::Encoding::UTF_8).variableize
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module EacEnvs
|
6
|
+
module Http
|
7
|
+
class Response < ::StandardError
|
8
|
+
module Headers
|
9
|
+
COMMON_HEADERS = %w[Content-Type].freeze
|
10
|
+
HEADER_LINE_PARSER = /\A([^:]+):(.*)\z/.to_parser do |m|
|
11
|
+
[m[1].strip, m[2].strip]
|
12
|
+
end
|
13
|
+
|
14
|
+
def header(name)
|
15
|
+
hash_search(headers, name)
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Hash<String, String>]
|
19
|
+
def headers
|
20
|
+
performed.headers.to_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
COMMON_HEADERS.each do |header_key|
|
24
|
+
define_method header_key.underscore do
|
25
|
+
header(header_key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def hash_search(hash, key)
|
32
|
+
key = key.to_s.downcase
|
33
|
+
hash.each do |k, v|
|
34
|
+
return v if k.to_s.downcase == key
|
35
|
+
end
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
module EacEnvs
|
5
|
+
module Http
|
6
|
+
class Response < ::StandardError
|
7
|
+
module Links
|
8
|
+
# https://www.w3.org/wiki/LinkHeader
|
9
|
+
LINKS_HEADER_NAME = 'Link'
|
10
|
+
|
11
|
+
# https://www.w3.org/wiki/LinkHeader
|
12
|
+
LINK_PARSER = /\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m|
|
13
|
+
[m[2], m[1]]
|
14
|
+
end
|
15
|
+
|
16
|
+
def link(rel)
|
17
|
+
hash_search(links, rel)
|
18
|
+
end
|
19
|
+
|
20
|
+
def links
|
21
|
+
header(LINKS_HEADER_NAME).if_present({}) do |v|
|
22
|
+
v.split(',').map { |w| LINK_PARSER.parse!(w.strip) }.to_h
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module EacEnvs
|
6
|
+
module Http
|
7
|
+
class Response < ::StandardError
|
8
|
+
module Statuses
|
9
|
+
def raise_unless_200
|
10
|
+
return nil if status == 200
|
11
|
+
|
12
|
+
raise self
|
13
|
+
end
|
14
|
+
|
15
|
+
def status
|
16
|
+
performed.status.to_i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -9,112 +9,22 @@ require 'json'
|
|
9
9
|
module EacEnvs
|
10
10
|
module Http
|
11
11
|
class Response < ::StandardError
|
12
|
-
COMMON_HEADERS = %w[Content-Type].freeze
|
13
|
-
HEADER_LINE_PARSER = /\A([^:]+):(.*)\z/.to_parser do |m|
|
14
|
-
[m[1].strip, m[2].strip]
|
15
|
-
end
|
16
|
-
|
17
|
-
# https://www.w3.org/wiki/LinkHeader
|
18
|
-
LINKS_HEADER_NAME = 'Link'
|
19
|
-
|
20
|
-
# https://www.w3.org/wiki/LinkHeader
|
21
|
-
LINK_PARSER = /\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m|
|
22
|
-
[m[2], m[1]]
|
23
|
-
end
|
24
|
-
|
25
12
|
common_constructor :request
|
26
|
-
delegate :response_body_data_proc, to: :request
|
27
|
-
|
28
|
-
def body_data
|
29
|
-
r = performed.headers['Accept'].if_present(body_str) do |v|
|
30
|
-
method_name = "body_data_from_#{v.parameterize.underscore}"
|
31
|
-
respond_to?(method_name) ? send(method_name) : body_str
|
32
|
-
end
|
33
|
-
r = response_body_data_proc.call(r) if response_body_data_proc.present?
|
34
|
-
r
|
35
|
-
end
|
36
|
-
|
37
|
-
def body_data_or_raise
|
38
|
-
raise_unless_200
|
39
|
-
|
40
|
-
body_data
|
41
|
-
end
|
42
|
-
|
43
|
-
# @return [String]
|
44
|
-
def body_str
|
45
|
-
performed.body
|
46
|
-
end
|
47
|
-
|
48
|
-
def body_str_or_raise
|
49
|
-
raise_unless_200
|
50
|
-
|
51
|
-
body_str
|
52
|
-
end
|
53
|
-
|
54
|
-
def header(name)
|
55
|
-
hash_search(headers, name)
|
56
|
-
end
|
57
|
-
|
58
|
-
# @return [Hash<String, String>]
|
59
|
-
def headers
|
60
|
-
performed.headers.to_hash
|
61
|
-
end
|
62
|
-
|
63
|
-
def link(rel)
|
64
|
-
hash_search(links, rel)
|
65
|
-
end
|
66
|
-
|
67
|
-
def links
|
68
|
-
header(LINKS_HEADER_NAME).if_present({}) do |v|
|
69
|
-
v.split(',').map { |w| LINK_PARSER.parse!(w.strip) }.to_h
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def raise_unless_200
|
74
|
-
return nil if status == 200
|
75
|
-
|
76
|
-
raise self
|
77
|
-
end
|
78
|
-
|
79
|
-
def status
|
80
|
-
performed.status.to_i
|
81
|
-
end
|
82
|
-
|
83
13
|
delegate :url, to: :request
|
84
14
|
|
85
15
|
def to_s
|
86
16
|
"URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
|
87
17
|
end
|
88
18
|
|
89
|
-
COMMON_HEADERS.each do |header_key|
|
90
|
-
define_method header_key.underscore do
|
91
|
-
header(header_key)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
19
|
private
|
96
20
|
|
97
|
-
def body_data_from_application_json
|
98
|
-
::JSON.parse(body_str)
|
99
|
-
end
|
100
|
-
|
101
|
-
def body_data_from_application_xml
|
102
|
-
Hash.from_xml(body_str)
|
103
|
-
end
|
104
|
-
|
105
|
-
def hash_search(hash, key)
|
106
|
-
key = key.to_s.downcase
|
107
|
-
hash.each do |k, v|
|
108
|
-
return v if k.to_s.downcase == key
|
109
|
-
end
|
110
|
-
nil
|
111
|
-
end
|
112
|
-
|
113
21
|
def performed
|
114
22
|
@performed ||= request.faraday_response
|
115
23
|
rescue ::Faraday::Error
|
116
24
|
raise ::EacEnvs::Http::Error
|
117
25
|
end
|
26
|
+
|
27
|
+
require_sub __FILE__, include_modules: true
|
118
28
|
end
|
119
29
|
end
|
120
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eac_envs-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Put here the authors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eac_fs
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.116'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.116'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: faraday
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -168,6 +168,10 @@ files:
|
|
168
168
|
- lib/eac_envs/http/request/body_fields.rb
|
169
169
|
- lib/eac_envs/http/request/faraday_connection.rb
|
170
170
|
- lib/eac_envs/http/response.rb
|
171
|
+
- lib/eac_envs/http/response/body.rb
|
172
|
+
- lib/eac_envs/http/response/headers.rb
|
173
|
+
- lib/eac_envs/http/response/links.rb
|
174
|
+
- lib/eac_envs/http/response/statuses.rb
|
171
175
|
- lib/eac_envs/http/rspec.rb
|
172
176
|
- lib/eac_envs/http/rspec/echo_server.rb
|
173
177
|
- lib/eac_envs/http/rspec/echo_server/request_processor.rb
|