eac_rest 0.6.1 → 0.7.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: 8d61c6dce47e5da585855c8ca4c3e6900a7890ac9c4943031dc852b4d6338ec7
4
- data.tar.gz: a775b53cec23e33c7d54816fdb10066f27988e3c4c44ee3a11b34de5274b89bc
3
+ metadata.gz: 66c3020339ccd5df2e47dfd97c85114a10aaa67da61bacf21ca43ac834bbfc24
4
+ data.tar.gz: d2e34918c150896f372a686dc093905adf54b6f30db300622f07f6b187368af6
5
5
  SHA512:
6
- metadata.gz: 4202d66186cefa25da7be74981c64caae9a906a843b32e0ee840e46f7c904d269247445080d351279118c91bfa7406d3cd32b5231df30033a9e4a48b613aadc4
7
- data.tar.gz: 2e924d32a06b09eb7867b3ea5736c92848907d3aba416a85eb2ca0d60c6a134aee328319af740b0ed6b68248d601ec138b38510b3565fce6d1f8818dba0e0511
6
+ metadata.gz: 6fc42e19e74493f10ffd0e30473dd5b173c96317946b8950e021f996c211c8e2769d81785bdb4b60a471156f085fd0cc20c32747b78dc63b549e1bc964f2e818
7
+ data.tar.gz: fa104d01ea1de4bb202c40b5c602de8754300bd16cb358b9744e01575a77ce91f7206cfafb48f691850e84b61d31f379e096b6dedd50af38cd6aa2227efdeb00
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRest
4
+ class Error < ::RuntimeError
5
+ end
6
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_rest/request/body_field_value'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'faraday/multipart/file_part'
6
+
7
+ module EacRest
8
+ class Request
9
+ class BodyField
10
+ class << self
11
+ # @return [Array<EacRest::Request::BodyField>]
12
+ def list_from_enumerable(enum)
13
+ hash = {}
14
+ enum.each do |v|
15
+ hash[v[0]] ||= []
16
+ hash[v[0]] << v[1]
17
+ end
18
+ list_from_hash(hash)
19
+ end
20
+
21
+ # @return [Array<EacRest::Request::BodyField>]
22
+ def list_from_hash(hash)
23
+ hash.map { |k, v| new(k, v) }
24
+ end
25
+ end
26
+
27
+ common_constructor :key, :values do
28
+ self.key = key.to_s
29
+ self.values = (values.is_a?(::Array) ? values.to_a : [values])
30
+ .map { |v| ::EacRest::Request::BodyFieldValue.new(v) }
31
+ end
32
+
33
+ # @return [String]
34
+ def hash_key
35
+ key
36
+ end
37
+
38
+ # @return [Array]
39
+ def hash_value
40
+ values.map(&:to_faraday)
41
+ end
42
+
43
+ # @return [Boolean]
44
+ def with_file?
45
+ values.any?(&:file?)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
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 EacRest
9
+ class Request
10
+ class BodyFieldValue
11
+ common_constructor :value
12
+
13
+ def to_faraday
14
+ return value unless file?
15
+
16
+ ::Faraday::Multipart::FilePart.new(value, file_mime_type)
17
+ end
18
+
19
+ # @return [Boolean]
20
+ def file?
21
+ value.is_a?(::File)
22
+ end
23
+
24
+ # @return [String]
25
+ def file_mime_type
26
+ ::EacFs::FileInfo.new(value.path).mime_type
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_rest/request/body_field'
5
+
6
+ module EacRest
7
+ class Request
8
+ class BodyFields
9
+ common_constructor :source_body
10
+
11
+ # @return [Hash, nil]
12
+ def to_h
13
+ fields.if_present do |v|
14
+ v.each_with_object({}) { |e, a| a[e.hash_key] = e.hash_value }
15
+ end
16
+ end
17
+
18
+ # @return [Array<EacRest::Request::BodyField>, nil]
19
+ def fields
20
+ source_body.if_present do |v|
21
+ next nil unless v.is_a?(::Enumerable)
22
+
23
+ if v.is_a?(::Hash)
24
+ ::EacRest::Request::BodyField.list_from_hash(v)
25
+ else
26
+ ::EacRest::Request::BodyField.list_from_enumerable(v)
27
+ end
28
+ end
29
+ end
30
+
31
+ # @return [Boolean]
32
+ def with_file?
33
+ fields.if_present(false) { |v| v.any?(&:with_file?) }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'curb'
4
3
  require 'eac_rest/response'
5
4
  require 'eac_ruby_utils/core_ext'
6
- require 'ostruct'
5
+ require 'faraday'
6
+ require 'faraday/multipart'
7
7
 
8
8
  module EacRest
9
9
  class Request
10
- BOOLEAN_MODIFIERS = %w[ssl_verify ssl_verify_peer ssl_verify_host].freeze
10
+ BOOLEAN_MODIFIERS = %w[ssl_verify].freeze
11
11
  COMMON_MODIFIERS = %w[auth body_data verb].freeze
12
12
  HASH_MODIFIERS = %w[header].freeze
13
13
  MODIFIERS = COMMON_MODIFIERS + BOOLEAN_MODIFIERS + HASH_MODIFIERS.map(&:pluralize)
@@ -23,73 +23,66 @@ module EacRest
23
23
  common_constructor :url, :body_data_proc, default: [nil]
24
24
 
25
25
  def autenticate(username, password)
26
- auth(::OpenStruct.new(username: username, password: password))
26
+ auth(::Struct.new(:username, :password).new(username, password))
27
27
  end
28
28
 
29
- def immutable_constructor_args
30
- [url, body_data_proc]
31
- end
32
-
33
- def response
34
- ::EacRest::Response.new(build_curl, body_data_proc)
29
+ # @return [Faraday::Connection]
30
+ def faraday_connection
31
+ ::Faraday.default_connection_options[:headers] = {}
32
+ ::Faraday::Connection.new(faraday_connection_options) do |conn|
33
+ if body_with_file?
34
+ conn.request :multipart, flat_encode: true
35
+ else
36
+ conn.request :url_encoded
37
+ end
38
+ auth.if_present { |v| conn.request :authorization, :basic, v.username, v.password }
39
+ end
35
40
  end
36
41
 
37
- private
38
-
39
- def build_curl
40
- r = ::Curl::Easy.new(url)
41
- MODIFIERS.each { |suffix| send("build_curl_#{suffix}", r) }
42
- r
42
+ # @return [Hash]
43
+ def faraday_connection_options
44
+ {
45
+ request: { params_encoder: Faraday::FlatParamsEncoder }, ssl: { verify: ssl_verify? }
46
+ }
43
47
  end
44
48
 
45
- def build_curl_auth(curl)
46
- auth.if_present do |a|
47
- curl.http_auth_types = :basic
48
- curl.username = a.username
49
- curl.password = a.password
49
+ # @return [Faraday::Response]
50
+ def faraday_response
51
+ conn = faraday_connection
52
+ conn.send(sanitized_verb, url) do |req|
53
+ req.headers = conn.headers.merge(headers)
54
+ sanitized_body_data.if_present { |v| req.body = v }
50
55
  end
51
56
  end
52
57
 
53
- def build_curl_body_data(curl)
54
- www_form_body_data_encoded.if_present { |v| curl.post_body = v }
58
+ def immutable_constructor_args
59
+ [url, body_data_proc]
55
60
  end
56
61
 
57
- def build_curl_headers(curl)
58
- curl.headers.merge!(headers)
62
+ def response
63
+ ::EacRest::Response.new(self)
59
64
  end
60
65
 
61
- def build_curl_ssl_verify(curl)
62
- return if ssl_verify?.nil?
63
-
64
- curl.ssl_verify_host = ssl_verify?
65
- curl.ssl_verify_peer = ssl_verify?
66
+ # @return [Symbol]
67
+ def sanitized_verb
68
+ verb.if_present(VERB_GET) { |v| self.class.lists.verb.value_validate!(v) }
66
69
  end
67
70
 
68
- def build_curl_ssl_verify_peer(curl)
69
- return if ssl_verify_peer?.nil?
71
+ private
70
72
 
71
- curl.ssl_verify_peer = ssl_verify_peer?
73
+ def body_fields
74
+ @body_fields ||= ::EacRest::Request::BodyFields.new(body_data)
72
75
  end
73
76
 
74
- def build_curl_ssl_verify_host(curl)
75
- return if ssl_verify_host?.nil?
76
-
77
- curl.ssl_verify_peer = ssl_verify_host?
77
+ # @return [Boolean]
78
+ def body_with_file?
79
+ body_fields.with_file?
78
80
  end
79
81
 
80
- def build_curl_verb(curl)
81
- curl.set(
82
- :customrequest,
83
- verb.if_present(VERB_GET) { |v| self.class.lists.verb.value_validate!(v) }.to_s.upcase
84
- )
82
+ def sanitized_body_data
83
+ body_fields.to_h || body_data
85
84
  end
86
85
 
87
- def www_form_body_data_encoded
88
- body_data.if_present do |v|
89
- v = v.map { |k, vv| [k, vv] } if v.is_a?(::Hash)
90
- v = URI.encode_www_form(v) if v.is_a?(::Array)
91
- v.to_s
92
- end
93
- end
86
+ require_sub __FILE__
94
87
  end
95
88
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/core_ext/hash/conversions'
4
+ require 'eac_rest'
4
5
  require 'eac_ruby_utils/core_ext'
5
6
  require 'json'
6
7
 
@@ -18,10 +19,11 @@ module EacRest
18
19
  [m[2], m[1]]
19
20
  end
20
21
 
21
- common_constructor :curl, :body_data_proc
22
+ common_constructor :request
23
+ delegate :body_data_proc, to: :request
22
24
 
23
25
  def body_data
24
- r = performed_curl.headers['Accept'].if_present(body_str) do |v|
26
+ r = performed.headers['Accept'].if_present(body_str) do |v|
25
27
  method_name = "body_data_from_#{v.parameterize.underscore}"
26
28
  respond_to?(method_name) ? send(method_name) : body_str
27
29
  end
@@ -35,7 +37,10 @@ module EacRest
35
37
  body_data
36
38
  end
37
39
 
38
- delegate :body_str, to: :performed_curl
40
+ # @return [String]
41
+ def body_str
42
+ performed.body
43
+ end
39
44
 
40
45
  def body_str_or_raise
41
46
  raise_unless_200
@@ -48,7 +53,7 @@ module EacRest
48
53
  end
49
54
 
50
55
  def headers
51
- performed_curl.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?)
56
+ performed.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?)
52
57
  .map { |header_line| HEADER_LINE_PARSER.parse!(header_line) }
53
58
  .to_h
54
59
  end
@@ -70,10 +75,10 @@ module EacRest
70
75
  end
71
76
 
72
77
  def status
73
- performed_curl.status.to_i
78
+ performed.status.to_i
74
79
  end
75
80
 
76
- delegate :url, to: :curl
81
+ delegate :url, to: :request
77
82
 
78
83
  def to_s
79
84
  "URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
@@ -97,16 +102,10 @@ module EacRest
97
102
  nil
98
103
  end
99
104
 
100
- def perform
101
- @perform ||= begin
102
- curl.perform || raise("CURL perform failed for #{url}")
103
- true
104
- end
105
- end
106
-
107
- def performed_curl
108
- perform
109
- curl
105
+ def performed
106
+ @performed ||= request.faraday_response
107
+ rescue ::Faraday::Error
108
+ raise ::EacRest::Error
110
109
  end
111
110
  end
112
111
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRest
4
- VERSION = '0.6.1'
4
+ VERSION = '0.7.1'
5
5
  end
metadata CHANGED
@@ -1,29 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eduardo H. Bogoni
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-04 00:00:00.000000000 Z
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: eac_ruby_utils
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '0.70'
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
20
51
  type: :runtime
21
52
  prerelease: false
22
53
  version_requirements: !ruby/object:Gem::Requirement
23
54
  requirements:
24
55
  - - "~>"
25
56
  - !ruby/object:Gem::Version
26
- version: '0.70'
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.15'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0.15'
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'
27
109
  - !ruby/object:Gem::Dependency
28
110
  name: eac_ruby_gem_support
29
111
  requirement: !ruby/object:Gem::Requirement
@@ -47,7 +129,11 @@ files:
47
129
  - lib/eac_rest.rb
48
130
  - lib/eac_rest/api.rb
49
131
  - lib/eac_rest/entity.rb
132
+ - lib/eac_rest/error.rb
50
133
  - lib/eac_rest/request.rb
134
+ - lib/eac_rest/request/body_field.rb
135
+ - lib/eac_rest/request/body_field_value.rb
136
+ - lib/eac_rest/request/body_fields.rb
51
137
  - lib/eac_rest/response.rb
52
138
  - lib/eac_rest/version.rb
53
139
  homepage: