eac_envs-http 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d855da8f92deffe9686e6f6f388334522c3e79f87c9239bd32e488e72c9e6259
4
+ data.tar.gz: cf49900f5908f11527516110dc0f6d325c290002a514c23008006fa68d36c939
5
+ SHA512:
6
+ metadata.gz: e36804df28315e55d42c9c98cfb19a535cf6ba37d601029edde78ea377c077ba4f93ecc48ca02bae4e1e264c052503b79ac47e508404db4deaee1ca0ef0b6496
7
+ data.tar.gz: 1f556e7b101ece361ed3f06ae54c223bd7d99bd137779ea0b58e22af9c7e5f37fbb7d22f99500f86f4784cdc100907c2f4a7cec21de2c450a239b2859c1f77ce
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacEnvs
4
+ module Http
5
+ class Error < ::RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_envs/http/request/body_field_value'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'faraday/multipart/file_part'
6
+
7
+ module EacEnvs
8
+ module Http
9
+ class Request
10
+ class BodyField
11
+ class << self
12
+ # @return [Array<EacEnvs::Http::Request::BodyField>]
13
+ def list_from_enumerable(enum)
14
+ hash = {}
15
+ enum.each do |v|
16
+ hash[v[0]] ||= []
17
+ hash[v[0]] << v[1]
18
+ end
19
+ list_from_hash(hash)
20
+ end
21
+
22
+ # @return [Array<EacEnvs::Http::Request::BodyField>]
23
+ def list_from_hash(hash)
24
+ hash.map { |k, v| new(k, v) }
25
+ end
26
+ end
27
+
28
+ common_constructor :key, :values do
29
+ self.key = key.to_s
30
+ self.values = (values.is_a?(::Array) ? values.to_a : [values])
31
+ .map { |v| ::EacEnvs::Http::Request::BodyFieldValue.new(v) }
32
+ end
33
+
34
+ # @return [String]
35
+ def hash_key
36
+ key
37
+ end
38
+
39
+ # @return [Array]
40
+ def hash_value
41
+ values.map(&:to_faraday)
42
+ end
43
+
44
+ # @return [Boolean]
45
+ def with_file?
46
+ values.any?(&:file?)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_fs/file_info'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'faraday'
6
+ require 'faraday/multipart'
7
+
8
+ module EacEnvs
9
+ module Http
10
+ class Request
11
+ class BodyFieldValue
12
+ common_constructor :value
13
+
14
+ def to_faraday
15
+ return value unless file?
16
+
17
+ ::Faraday::Multipart::FilePart.new(value, file_mime_type)
18
+ end
19
+
20
+ # @return [Boolean]
21
+ def file?
22
+ value.is_a?(::File)
23
+ end
24
+
25
+ # @return [String]
26
+ def file_mime_type
27
+ ::EacFs::FileInfo.new(value.path).mime_type
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_envs/http/request/body_field'
5
+
6
+ module EacEnvs
7
+ module Http
8
+ class Request
9
+ class BodyFields
10
+ common_constructor :source_body
11
+
12
+ # @return [Hash, nil]
13
+ def to_h
14
+ fields.if_present do |v|
15
+ v.each_with_object({}) { |e, a| a[e.hash_key] = e.hash_value }
16
+ end
17
+ end
18
+
19
+ # @return [Array<EacEnvs::Http::Request::BodyField>, nil]
20
+ def fields
21
+ source_body.if_present do |v|
22
+ next nil unless v.is_a?(::Enumerable)
23
+
24
+ if v.is_a?(::Hash)
25
+ ::EacEnvs::Http::Request::BodyField.list_from_hash(v)
26
+ else
27
+ ::EacEnvs::Http::Request::BodyField.list_from_enumerable(v)
28
+ end
29
+ end
30
+ end
31
+
32
+ # @return [Boolean]
33
+ def with_file?
34
+ fields.if_present(false) { |v| v.any?(&:with_file?) }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_envs/http/response'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'faraday'
6
+ require 'faraday/multipart'
7
+
8
+ module EacEnvs
9
+ module Http
10
+ class Request
11
+ BOOLEAN_MODIFIERS = %w[ssl_verify].freeze
12
+ COMMON_MODIFIERS = %w[auth body_data response_body_data_proc url verb].freeze
13
+ HASH_MODIFIERS = %w[header].freeze
14
+ MODIFIERS = COMMON_MODIFIERS + BOOLEAN_MODIFIERS + HASH_MODIFIERS.map(&:pluralize)
15
+
16
+ enable_immutable
17
+ immutable_accessor(*BOOLEAN_MODIFIERS, type: :boolean)
18
+ immutable_accessor(*COMMON_MODIFIERS, type: :common)
19
+ immutable_accessor(*HASH_MODIFIERS, type: :hash)
20
+
21
+ enable_listable
22
+ lists.add_symbol :verb, :get, :delete, :options, :post, :put
23
+
24
+ def basic_auth(username, password)
25
+ auth(::Struct.new(:username, :password).new(username, password))
26
+ end
27
+
28
+ # @return [Faraday::Connection]
29
+ def faraday_connection
30
+ ::Faraday.default_connection_options[:headers] = {}
31
+ ::Faraday::Connection.new(faraday_connection_options) do |conn|
32
+ if body_with_file?
33
+ conn.request :multipart, flat_encode: true
34
+ else
35
+ conn.request :url_encoded
36
+ end
37
+ auth.if_present { |v| conn.request :authorization, :basic, v.username, v.password }
38
+ end
39
+ end
40
+
41
+ # @return [Hash]
42
+ def faraday_connection_options
43
+ {
44
+ request: { params_encoder: Faraday::FlatParamsEncoder }, ssl: { verify: ssl_verify? }
45
+ }
46
+ end
47
+
48
+ # @return [Faraday::Response]
49
+ def faraday_response
50
+ conn = faraday_connection
51
+ conn.send(sanitized_verb, url) do |req|
52
+ req.headers = conn.headers.merge(headers)
53
+ sanitized_body_data.if_present { |v| req.body = v }
54
+ end
55
+ end
56
+
57
+ def response
58
+ ::EacEnvs::Http::Response.new(self)
59
+ end
60
+
61
+ # @return [Symbol]
62
+ def sanitized_verb
63
+ verb.if_present(VERB_GET) { |v| self.class.lists.verb.value_validate!(v) }
64
+ end
65
+
66
+ private
67
+
68
+ def body_fields
69
+ @body_fields ||= ::EacEnvs::Http::Request::BodyFields.new(body_data)
70
+ end
71
+
72
+ # @return [Boolean]
73
+ def body_with_file?
74
+ body_fields.with_file?
75
+ end
76
+
77
+ def sanitized_body_data
78
+ body_fields.to_h || body_data
79
+ end
80
+
81
+ require_sub __FILE__
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/conversions'
4
+ require 'eac_envs/http/error'
5
+ require 'eac_ruby_utils/core_ext'
6
+ require 'faraday'
7
+ require 'json'
8
+
9
+ module EacEnvs
10
+ module Http
11
+ class Response < ::StandardError
12
+ HEADER_LINE_PARSER = /\A([^:]+):(.*)\z/.to_parser do |m|
13
+ [m[1].strip, m[2].strip]
14
+ end
15
+
16
+ # https://www.w3.org/wiki/LinkHeader
17
+ LINKS_HEADER_NAME = 'Link'
18
+
19
+ # https://www.w3.org/wiki/LinkHeader
20
+ LINK_PARSER = /\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m|
21
+ [m[2], m[1]]
22
+ end
23
+
24
+ common_constructor :request
25
+ delegate :body_data_proc, to: :request
26
+
27
+ def body_data
28
+ r = performed.headers['Accept'].if_present(body_str) do |v|
29
+ method_name = "body_data_from_#{v.parameterize.underscore}"
30
+ respond_to?(method_name) ? send(method_name) : body_str
31
+ end
32
+ r = body_data_proc.call(r) if body_data_proc.present?
33
+ r
34
+ end
35
+
36
+ def body_data_or_raise
37
+ raise_unless_200
38
+
39
+ body_data
40
+ end
41
+
42
+ # @return [String]
43
+ def body_str
44
+ performed.body
45
+ end
46
+
47
+ def body_str_or_raise
48
+ raise_unless_200
49
+
50
+ body_str
51
+ end
52
+
53
+ def header(name)
54
+ hash_search(headers, name)
55
+ end
56
+
57
+ def headers
58
+ performed.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?)
59
+ .map { |header_line| HEADER_LINE_PARSER.parse!(header_line) }
60
+ .to_h
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
+ delegate :url, to: :request
84
+
85
+ def to_s
86
+ "URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
87
+ end
88
+
89
+ private
90
+
91
+ def body_data_from_application_json
92
+ ::JSON.parse(body_str)
93
+ end
94
+
95
+ def body_data_from_application_xml
96
+ Hash.from_xml(body_str)
97
+ end
98
+
99
+ def hash_search(hash, key)
100
+ key = key.to_s.downcase
101
+ hash.each do |k, v|
102
+ return v if k.to_s.downcase == key
103
+ end
104
+ nil
105
+ end
106
+
107
+ def performed
108
+ @performed ||= request.faraday_response
109
+ rescue ::Faraday::Error
110
+ raise ::EacEnvs::Http::Error
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacEnvs
4
+ module Http
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacEnvs
6
+ module Http
7
+ require_sub __FILE__
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eac_envs-http
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Put here the authors
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eac_fs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eac_ruby_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.112'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.112'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.7.4
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.7'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.7.4
61
+ - !ruby/object:Gem::Dependency
62
+ name: faraday-multipart
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.0'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.0.4
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.0'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.0.4
81
+ - !ruby/object:Gem::Dependency
82
+ name: aranha-parsers
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0.17'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0.17'
95
+ - !ruby/object:Gem::Dependency
96
+ name: eac_docker
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '0.5'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '0.5'
109
+ - !ruby/object:Gem::Dependency
110
+ name: eac_ruby_gem_support
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '0.5'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 0.5.1
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '0.5'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 0.5.1
129
+ description:
130
+ email:
131
+ executables: []
132
+ extensions: []
133
+ extra_rdoc_files: []
134
+ files:
135
+ - lib/eac_envs/http.rb
136
+ - lib/eac_envs/http/error.rb
137
+ - lib/eac_envs/http/request.rb
138
+ - lib/eac_envs/http/request/body_field.rb
139
+ - lib/eac_envs/http/request/body_field_value.rb
140
+ - lib/eac_envs/http/request/body_fields.rb
141
+ - lib/eac_envs/http/response.rb
142
+ - lib/eac_envs/http/version.rb
143
+ homepage:
144
+ licenses: []
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.1.6
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Put here de description.
165
+ test_files: []