foobara-http-api-command 0.0.2 → 0.0.4

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: eefd62884619d75957db64c7d1516f8295aef58eeb11da24ba666c22b6b8249e
4
- data.tar.gz: 9948d0ea1b6703baa80eb2d7f2892214a306ce74cbbfc156eb1e59ccd7ec7c86
3
+ metadata.gz: 50391d8f44e37644619f61aa1d38eba3104e454d4b652d08561fae9cac5b060b
4
+ data.tar.gz: ed7174a489766ab28a190967be00f60c3b831636c60515c7f4531ce41909ea04
5
5
  SHA512:
6
- metadata.gz: 5b8a838148852a0217bf26563af54a22d1b630d1febe0453b8390ffd2dcbd0a93f7bd257a6c41e873acefc81fc7c732329b4b6af0c87ca312c479cd765b809d9
7
- data.tar.gz: 0e74fc2f8fd361485438df580f3ffdb62ee2b2f77af2d758ade7dcf0d65a63d043457f3f060bf364bc0b0596f20f699b0eac738cafc99c170732f3ce69805e2d
6
+ metadata.gz: 36bd3a6473d63328347e23645aa50a87cbc8862c09516acd98eef502c6aaca1a060fec624cbf6cd696318198c0ef75326c6a4b7c649028805fb06ebe39658719
7
+ data.tar.gz: 7cb9b7b0aecde58a0b4ab81bf26b1670454fffafff66bd61cf61890f106037999fca0a372d3e4ad788b576277a3b814e1c60709b74276ca6478408b876782e85
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [0.0.4] - 2025-02-17
2
+
3
+ - Support separate base_url and path helpers
4
+
5
+ ## [0.0.3] - 2025-02-08
6
+
7
+ - Implement HttpApiPostCommand
8
+
1
9
  ## [0.0.2] - 2025-01-07
2
10
 
3
11
  - Bump Ruby to 3.4.1
@@ -0,0 +1,83 @@
1
+ module Foobara
2
+ module HttpApiCommand
3
+ module Concerns
4
+ module Url
5
+ include Concern
6
+
7
+ def api_url
8
+ @api_url ||= self.class.compute_api_url(self)
9
+ end
10
+
11
+ module ClassMethods
12
+ attr_accessor :foobara_base_url_block, :foobara_base_url, :foobara_path, :foobara_url_block, :foobara_url
13
+
14
+ def base_url(url = nil, &block)
15
+ if block_given?
16
+ unless url.nil?
17
+ # :nocov:
18
+ raise ArgumentError, "Cannot specify both url and block"
19
+ # :nocov:
20
+ end
21
+
22
+ self.foobara_base_url_block = block
23
+ elsif url
24
+ self.foobara_base_url = url
25
+ else
26
+ # :nocov:
27
+ raise ArgumentError, "No base url specified"
28
+ # :nocov:
29
+ end
30
+ end
31
+
32
+ def path(path = nil)
33
+ if path
34
+ self.foobara_path = path
35
+ end
36
+ end
37
+
38
+ def url(uri = nil, &block)
39
+ if block_given?
40
+ unless uri.nil?
41
+ # :nocov:
42
+ raise ArgumentError, "Cannot specify both uri and block"
43
+ # :nocov:
44
+ end
45
+
46
+ self.foobara_url_block = block
47
+ elsif uri
48
+ self.foobara_url = uri
49
+ else
50
+ # :nocov:
51
+ raise ArgumentError, "Must give either a url or a block that returns url"
52
+ # :nocov:
53
+ end
54
+ end
55
+
56
+ def compute_api_url(command)
57
+ if foobara_url
58
+ foobara_url
59
+ elsif foobara_url_block
60
+ command.instance_eval(&foobara_url_block)
61
+ elsif foobara_path
62
+ base = if foobara_base_url
63
+ foobara_base_url
64
+ elsif foobara_base_url_block
65
+ command.instance_eval(&foobara_base_url_block)
66
+ else
67
+ # :nocov:
68
+ raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
69
+ # :nocov:
70
+ end
71
+
72
+ "#{base}#{foobara_path}"
73
+ else
74
+ # :nocov:
75
+ raise "Not able to determine the api url. Did you remember to call .url or .path and .base_url?"
76
+ # :nocov:
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,52 @@
1
+ require "uri"
2
+ require "net/http"
3
+
4
+ module Foobara
5
+ module HttpApiCommand
6
+ include Concern
7
+ include Concerns::Url
8
+
9
+ def execute
10
+ build_request_body
11
+ build_request_headers
12
+ issue_http_request
13
+ parse_response
14
+ build_result
15
+ end
16
+
17
+ attr_accessor :request_body, :request_headers, :response, :response_body
18
+
19
+ def build_request_body
20
+ self.request_body = {}
21
+ end
22
+
23
+ def build_request_headers
24
+ self.request_headers = {
25
+ "Content-Type" => "application/json"
26
+ }
27
+ end
28
+
29
+ def issue_http_request
30
+ # :nocov:
31
+ raise "subclass responsibility"
32
+ # :nocov:
33
+ end
34
+
35
+ def build_result
36
+ response_body
37
+ end
38
+
39
+ def parse_response
40
+ json = if response.is_a?(Net::HTTPSuccess)
41
+ response.body
42
+ else
43
+ # :nocov:
44
+ raise "Could not successfully hit #{api_url}: " \
45
+ "#{response.code} #{response.message}"
46
+ # :nocov:
47
+ end
48
+
49
+ self.response_body = JSON.parse(json)
50
+ end
51
+ end
52
+ end
@@ -1,67 +1,14 @@
1
- require "uri"
2
- require "net/http"
1
+ require_relative "http_api_command"
3
2
 
4
3
  module Foobara
5
4
  module HttpApiGetCommand
6
5
  include Concern
7
-
8
- def execute
9
- build_request_body
10
- build_request_headers
11
- issue_http_request
12
- parse_response
13
- build_result
14
- end
15
-
16
- attr_accessor :request_body, :request_headers, :response, :response_body
17
-
18
- def api_url
19
- @api_url ||= instance_eval(&self.class.foobara_url_block)
20
- end
21
-
22
- def build_request_body
23
- self.request_body = {}
24
- end
25
-
26
- def build_request_headers
27
- self.request_headers = {
28
- "Content-Type" => "application/json"
29
- }
30
- end
6
+ include HttpApiCommand
31
7
 
32
8
  def issue_http_request
33
9
  uri = URI(api_url)
34
10
  uri.query = URI.encode_www_form(request_body)
35
11
  self.response = Net::HTTP.get_response(uri, request_headers)
36
12
  end
37
-
38
- def build_result
39
- response_body
40
- end
41
-
42
- def parse_response
43
- json = if response.is_a?(Net::HTTPSuccess)
44
- response.body
45
- else
46
- # :nocov:
47
- raise "Could not successfully hit #{api_url}: " \
48
- "#{response.code} #{response.message}"
49
- # :nocov:
50
- end
51
-
52
- self.response_body = JSON.parse(json)
53
- end
54
-
55
- module ClassMethods
56
- attr_accessor :foobara_url_block
57
-
58
- def url(string = nil, &block)
59
- self.foobara_url_block = if block_given?
60
- block
61
- else
62
- proc { string }
63
- end
64
- end
65
- end
66
13
  end
67
14
  end
@@ -0,0 +1,13 @@
1
+ require_relative "http_api_command"
2
+
3
+ module Foobara
4
+ module HttpApiPostCommand
5
+ include Concern
6
+ include HttpApiCommand
7
+
8
+ def issue_http_request
9
+ uri = URI.parse(api_url)
10
+ self.response = Net::HTTP.post(uri, JSON.generate(request_body), request_headers)
11
+ end
12
+ end
13
+ end
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.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-07 00:00:00.000000000 Z
10
+ date: 2025-02-17 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: foobara
@@ -34,7 +34,10 @@ files:
34
34
  - LICENSE.txt
35
35
  - README.md
36
36
  - lib/foobara/http_api_command.rb
37
+ - src/foobara/concerns/url.rb
38
+ - src/foobara/http_api_command.rb
37
39
  - src/foobara/http_api_get_command.rb
40
+ - src/foobara/http_api_post_command.rb
38
41
  homepage: https://github.com/foobara/http-api-command
39
42
  licenses:
40
43
  - MPL-2.0
@@ -57,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
60
  - !ruby/object:Gem::Version
58
61
  version: '0'
59
62
  requirements: []
60
- rubygems_version: 3.6.2
63
+ rubygems_version: 3.6.3
61
64
  specification_version: 4
62
65
  summary: No description. Add one.
63
66
  test_files: []