foobara-http-api-command 0.0.6 → 0.0.7

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: 9e260c28660b274db261a8cb7516ba3763ab534f7155b5f3dff42f6e14d735c2
4
- data.tar.gz: 126c8c76cd2a14d54d5a3da3f44e79f2da110d253b8e7d387d002eb3b5c17e59
3
+ metadata.gz: 893bd02f32c789c89339a9937281364dbdaeac7fc1973e2cb11f36b8cac9dcee
4
+ data.tar.gz: 3db537e096b056d4975630774242e5e96e2f1ce83e0013303b1f378763220081
5
5
  SHA512:
6
- metadata.gz: 1f5afa79b0cc20bbd8e1a056cac98e62e9490af23f085f3387a68f139eb30fc5ff31d24900ce249baa6910c7ad73dfd8e229aa7afcce9673c394dad4522cd77d
7
- data.tar.gz: 2d892b329fa23240b6da945db808eac5f518d1a0cca1ff68f9be084d95e4fde0699bbbbb0529897bb9a6925ce6ec27faad40df4ebc986745671d067ead60d0ee
6
+ metadata.gz: ded6cf9c019a53a292d8d396cf92e01397146ca8a41b8c1f3e6ea1c193b9b8346d857533597de7bf08ffc99140010356072714f67b200e7f7a252d0c503c3264
7
+ data.tar.gz: f80656df8158413a399ff111d31a6dc2b009e3d2873a89a8d1eab1558acceee5c084764e4c6bf11fbe0190e1ab5a3e12ce771f6cf65bf1624a9710416618f6b7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.0.7] - 2025-02-27
2
+
3
+ - Add http_timeout option
4
+ - Bump Ruby to 3.4.2
5
+
1
6
  ## [0.0.6] - 2025-02-17
2
7
 
3
8
  - Support separate base_url and path helpers
@@ -4,16 +4,26 @@ module Foobara
4
4
  module Url
5
5
  include Concern
6
6
 
7
+ def net_http
8
+ @net_http ||= self.class.compute_http(self)
9
+ end
10
+
7
11
  def api_url
8
12
  @api_url ||= self.class.compute_api_url(self)
9
13
  end
10
14
 
15
+ def api_uri_object
16
+ @api_uri_object ||= self.class.compute_uri_object(self)
17
+ end
18
+
11
19
  inherited_overridable_class_attr_accessor :foobara_base_url_block,
12
20
  :foobara_base_url,
13
21
  :foobara_path,
22
+ :foobara_path_block,
14
23
  :foobara_url_block,
15
24
  :foobara_url,
16
- :foobara_http_method
25
+ :foobara_http_method,
26
+ :foobara_http_timeout
17
27
 
18
28
  module ClassMethods
19
29
  def http_method(method = nil)
@@ -42,9 +52,21 @@ module Foobara
42
52
  end
43
53
  end
44
54
 
45
- def path(path = nil)
46
- if path
55
+ def path(path = nil, &block)
56
+ if block_given?
57
+ unless path.nil?
58
+ # :nocov:
59
+ raise ArgumentError, "Cannot specify both path and block"
60
+ # :nocov:
61
+ end
62
+
63
+ self.foobara_path_block = block
64
+ elsif path
47
65
  self.foobara_path = path
66
+ else
67
+ # :nocov:
68
+ raise ArgumentError, "No path specified"
69
+ # :nocov:
48
70
  end
49
71
  end
50
72
 
@@ -66,28 +88,77 @@ module Foobara
66
88
  end
67
89
  end
68
90
 
69
- def compute_api_url(command)
70
- if foobara_url
71
- foobara_url
72
- elsif foobara_url_block
73
- command.instance_eval(&foobara_url_block)
74
- elsif foobara_path
75
- base = if foobara_base_url
76
- foobara_base_url
77
- elsif foobara_base_url_block
78
- command.instance_eval(&foobara_base_url_block)
79
- else
80
- # :nocov:
81
- raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
82
- # :nocov:
83
- end
84
-
85
- "#{base}#{foobara_path}"
91
+ def http_timeout(timeout = nil)
92
+ if timeout
93
+ self.foobara_http_timeout = timeout
86
94
  else
87
- # :nocov:
88
- raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
89
- # :nocov:
95
+ foobara_http_timeout
96
+ end
97
+ end
98
+
99
+ def compute_uri_object(command)
100
+ return @compute_uri_object if @compute_uri_object
101
+
102
+ uri = URI(compute_api_url(command))
103
+
104
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
105
+ @compute_uri_object = uri
106
+ end
107
+
108
+ uri
109
+ end
110
+
111
+ def compute_api_url(command)
112
+ return @compute_api_url if @compute_api_url
113
+
114
+ url = if foobara_url
115
+ foobara_url
116
+ elsif foobara_url_block
117
+ command.instance_eval(&foobara_url_block)
118
+ else
119
+ path = if foobara_path
120
+ foobara_path
121
+ elsif foobara_path_block
122
+ command.instance_eval(&foobara_path_block)
123
+ end
124
+
125
+ base = if foobara_base_url
126
+ foobara_base_url
127
+ elsif foobara_base_url_block
128
+ command.instance_eval(&foobara_base_url_block)
129
+ else
130
+ # :nocov:
131
+ raise "Not able to determine the api url. " \
132
+ "Did you remember to call .url or .path and .base_url?"
133
+ # :nocov:
134
+ end
135
+
136
+ "#{base}#{path}"
137
+ end
138
+
139
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
140
+ @compute_api_url = url
90
141
  end
142
+
143
+ url
144
+ end
145
+
146
+ def compute_http(command)
147
+ return @net_http if @net_http
148
+
149
+ uri = URI(command.api_url)
150
+ computed_http = Net::HTTP.new(uri.host, uri.port).tap do |http|
151
+ http.use_ssl = uri.scheme == "https"
152
+ if http_timeout
153
+ http.read_timeout = http_timeout
154
+ end
155
+ end
156
+
157
+ unless foobara_base_url_block || foobara_path_block || foobara_url_block
158
+ @net_http = computed_http
159
+ end
160
+
161
+ computed_http
91
162
  end
92
163
  end
93
164
  end
@@ -27,19 +27,28 @@ module Foobara
27
27
  end
28
28
 
29
29
  def issue_http_request
30
- case self.class.http_method
31
- when :get
32
- uri = URI(api_url)
33
- uri.query = URI.encode_www_form(request_body)
34
- self.response = Net::HTTP.get_response(uri, request_headers)
35
- when :post
36
- uri = URI.parse(api_url)
37
- self.response = Net::HTTP.post(uri, JSON.generate(request_body), request_headers)
38
- else
39
- # :nocov:
40
- raise "Unknown http method #{self.class.http_method}"
41
- # :nocov:
42
- end
30
+ request = case self.class.http_method
31
+ when :get
32
+ uri = if request_body.empty?
33
+ api_uri_object
34
+ else
35
+ api_uri_object.dup.tap do |new_uri|
36
+ new_uri.query = URI.encode_www_form(request_body)
37
+ end
38
+ end
39
+
40
+ Net::HTTP::Get.new(uri.request_uri, request_headers)
41
+ when :post
42
+ Net::HTTP::Post.new(api_uri_object.request_uri, request_headers).tap do |post|
43
+ post.body = JSON.generate(request_body)
44
+ end
45
+ else
46
+ # :nocov:
47
+ raise "Unknown http method #{self.class.http_method}"
48
+ # :nocov:
49
+ end
50
+
51
+ self.response = net_http.request(request)
43
52
  end
44
53
 
45
54
  def build_result
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-http-api-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-17 00:00:00.000000000 Z
10
+ date: 2025-02-27 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: foobara