eac_rest 0.7.0 → 0.7.2

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: f713ae65fe3703fbdb4ab49b6244e1bf874d7329d2d1712354cdc8908938617b
4
- data.tar.gz: 4209a19b37acc8cd8a57375e27f746fe41f2ae6a989f8f1f5c16151659b0bd19
3
+ metadata.gz: 877e75934091f0d5f8dd8384c81c9cf426e9612302e3d4d2c6c0ebe152cfe055
4
+ data.tar.gz: 9dccadaff0e6213f94316cf029f2bc45983842d3403c3f43f48eaeadce52f5fd
5
5
  SHA512:
6
- metadata.gz: 3c375d852ae036155fb9b04371d8e4125613ac5fd1bd1dee6499cf84fb408c24ab83bffbe51f7f020e4b48cd01923dcdbcafdcb463382cdaa8466e842a44950c
7
- data.tar.gz: 522c1d8e8b5f602758784d44b2705fed4436256969221d4e4140ee625dee94d793a631cfa98cbd4cfce23a4c008e49b8097b534de1efbe29a9d924a094731b89
6
+ metadata.gz: 4676621a795a38f56dc60dd6f1792312e75ae8893f4590d4cf23a67395dca945179d0c99601b9e0cdb9fab3fbd7071a92714878481afef512643bc854c47253b
7
+ data.tar.gz: b84074c9d236764a9e275c130392ef170145072b41c0a64cb361ff5db6eb83f9183c6923305e8104427d1bb7489c038deda25e942fe17a9b8c9d89abbe8a25a7
@@ -1,84 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'eac_envs/http/request'
3
4
  require 'eac_rest/response'
4
5
  require 'eac_ruby_utils/core_ext'
5
- require 'faraday'
6
- require 'faraday/multipart'
7
6
 
8
7
  module EacRest
9
8
  class Request
10
- BOOLEAN_MODIFIERS = %w[ssl_verify].freeze
11
- COMMON_MODIFIERS = %w[auth body_data verb].freeze
12
- HASH_MODIFIERS = %w[header].freeze
13
- MODIFIERS = COMMON_MODIFIERS + BOOLEAN_MODIFIERS + HASH_MODIFIERS.map(&:pluralize)
14
-
15
- enable_immutable
16
- immutable_accessor(*BOOLEAN_MODIFIERS, type: :boolean)
17
- immutable_accessor(*COMMON_MODIFIERS, type: :common)
18
- immutable_accessor(*HASH_MODIFIERS, type: :hash)
19
-
20
- enable_listable
21
- lists.add_symbol :verb, :get, :delete, :options, :post, :put
22
-
23
- common_constructor :url, :body_data_proc, default: [nil]
24
-
25
- def autenticate(username, password)
26
- auth(::Struct.new(:username, :password).new(username, password))
27
- end
28
-
29
- # @return [Faraday::Connection]
30
- def faraday_connection
31
- ::Faraday.default_connection_options[:headers] = {}
32
- ::Faraday::Connection.new(faraday_connection_options) do |conn|
33
- conn.request(body_with_file? ? :multipart : :url_encoded)
34
- auth.if_present { |v| conn.request :authorization, :basic, v.username, v.password }
9
+ attr_reader :internal_request
10
+
11
+ # @param url [EacEnvs::Http::Request, String]
12
+ # @param response_body_data_proc [Proc, nil]
13
+ def initialize(url, response_body_data_proc = nil)
14
+ if url.is_a?(::EacEnvs::Http::Request)
15
+ @internal_request = url
16
+ else
17
+ @internal_request = ::EacEnvs::Http::Request.new.url(url)
18
+ response_body_data_proc.if_present { |v| @internal_request.response_body_data_proc = v }
35
19
  end
36
20
  end
37
21
 
38
- # @return [Hash]
39
- def faraday_connection_options
40
- {
41
- request: { params_encoder: Faraday::FlatParamsEncoder }, ssl: { verify: ssl_verify? }
42
- }
43
- end
22
+ delegate :sanitized_verb, to: :internal_request
44
23
 
45
- # @return [Faraday::Response]
46
- def faraday_response
47
- conn = faraday_connection
48
- conn.send(sanitized_verb, url) do |req|
49
- req.headers = conn.headers.merge(headers)
50
- sanitized_body_data.if_present { |v| req.body = v }
24
+ {
25
+ auth: 0, body_data: 0, header: 1, headers: 0, ssl_verify: 0, verb: 0
26
+ }.each do |method_name, read_args_count|
27
+ define_method method_name do |*args|
28
+ if args.count <= read_args_count
29
+ internal_request.send(method_name, *args)
30
+ else
31
+ self.class.new(internal_request.send(method_name, *args))
32
+ end
51
33
  end
52
34
  end
53
35
 
54
- def immutable_constructor_args
55
- [url, body_data_proc]
36
+ # @return [EacRest::Request]
37
+ def autenticate(username, password)
38
+ self.class.new(internal_request.basic_auth(username, password))
56
39
  end
57
40
 
41
+ # @return [EacRest::Response]
58
42
  def response
59
43
  ::EacRest::Response.new(self)
60
44
  end
61
-
62
- # @return [Symbol]
63
- def sanitized_verb
64
- verb.if_present(VERB_GET) { |v| self.class.lists.verb.value_validate!(v) }
65
- end
66
-
67
- private
68
-
69
- def body_fields
70
- @body_fields ||= ::EacRest::Request::BodyFields.new(body_data)
71
- end
72
-
73
- # @return [Boolean]
74
- def body_with_file?
75
- body_fields.with_file?
76
- end
77
-
78
- def sanitized_body_data
79
- body_fields.to_h || body_data
80
- end
81
-
82
- require_sub __FILE__
83
45
  end
84
46
  end
@@ -1,111 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/core_ext/hash/conversions'
4
- require 'eac_rest'
3
+ require 'eac_rest/error'
4
+ require 'eac_envs/http/error'
5
5
  require 'eac_ruby_utils/core_ext'
6
- require 'json'
7
6
 
8
7
  module EacRest
9
8
  class Response < ::StandardError
10
- HEADER_LINE_PARSER = /\A([^:]+):(.*)\z/.to_parser do |m|
11
- [m[1].strip, m[2].strip]
12
- end
13
-
14
- # https://www.w3.org/wiki/LinkHeader
15
- LINKS_HEADER_NAME = 'Link'
16
-
17
- # https://www.w3.org/wiki/LinkHeader
18
- LINK_PARSER = /\A\<(.+)\>\s*;\s*rel\s*=\s*\"(.*)\"\z/.to_parser do |m|
19
- [m[2], m[1]]
20
- end
21
-
22
9
  common_constructor :request
23
10
  delegate :body_data_proc, to: :request
24
11
 
25
- def body_data
26
- r = performed.headers['Accept'].if_present(body_str) do |v|
27
- method_name = "body_data_from_#{v.parameterize.underscore}"
28
- respond_to?(method_name) ? send(method_name) : body_str
29
- end
30
- r = body_data_proc.call(r) if body_data_proc.present?
31
- r
32
- end
33
-
34
- def body_data_or_raise
35
- raise_unless_200
36
-
37
- body_data
38
- end
39
-
40
- # @return [String]
41
- def body_str
42
- performed.body
43
- end
44
-
45
- def body_str_or_raise
46
- raise_unless_200
47
-
48
- body_str
49
- end
50
-
51
- def header(name)
52
- hash_search(headers, name)
53
- end
54
-
55
- def headers
56
- performed.header_str.each_line.map(&:strip)[1..-1].reject(&:blank?)
57
- .map { |header_line| HEADER_LINE_PARSER.parse!(header_line) }
58
- .to_h
59
- end
60
-
61
- def link(rel)
62
- hash_search(links, rel)
12
+ def internal_response
13
+ @internal_response ||= request.internal_request.response
63
14
  end
64
15
 
65
- def links
66
- header(LINKS_HEADER_NAME).if_present({}) do |v|
67
- v.split(',').map { |w| LINK_PARSER.parse!(w.strip) }.to_h
16
+ %i[body_data body_data_or_raise body_str body_str_or_raise header headers
17
+ link links raise_unless_200 status to_s url].each do |method_name|
18
+ define_method method_name do |*args, &block|
19
+ internal_response.send(method_name, *args, &block)
20
+ rescue ::EacEnvs::Http::Error
21
+ raise ::EacRest::Error
68
22
  end
69
23
  end
70
-
71
- def raise_unless_200
72
- return nil if status == 200
73
-
74
- raise self
75
- end
76
-
77
- def status
78
- performed.status.to_i
79
- end
80
-
81
- delegate :url, to: :curl
82
-
83
- def to_s
84
- "URL: #{url}\nStatus: #{status}\nBody:\n\n#{body_str}"
85
- end
86
-
87
- private
88
-
89
- def body_data_from_application_json
90
- ::JSON.parse(body_str)
91
- end
92
-
93
- def body_data_from_application_xml
94
- Hash.from_xml(body_str)
95
- end
96
-
97
- def hash_search(hash, key)
98
- key = key.to_s.downcase
99
- hash.each do |k, v|
100
- return v if k.to_s.downcase == key
101
- end
102
- nil
103
- end
104
-
105
- def performed
106
- @performed_curl ||= request.faraday_response
107
- rescue ::Faraday::Error
108
- raise ::EacRest::Error
109
- end
110
24
  end
111
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRest
4
- VERSION = '0.7.0'
4
+ VERSION = '0.7.2'
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.2
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: 2023-01-31 00:00:00.000000000 Z
11
+ date: 2023-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eac_envs-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: eac_fs
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -131,9 +145,6 @@ files:
131
145
  - lib/eac_rest/entity.rb
132
146
  - lib/eac_rest/error.rb
133
147
  - 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
137
148
  - lib/eac_rest/response.rb
138
149
  - lib/eac_rest/version.rb
139
150
  homepage:
@@ -1,49 +0,0 @@
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
@@ -1,30 +0,0 @@
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
@@ -1,37 +0,0 @@
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