foobara-http-api-command 0.0.3 → 0.0.5

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: 61615cf5f9d004cfc8525fd42c7536da93ca8c8fd55413ea56bf0080fa0002b4
4
- data.tar.gz: a303d0d85894a7ad1f17476480e73a91fdb5827e01469f025f9574150f6ebeb2
3
+ metadata.gz: 47344f134791c7562826026ebe261a8e89152295bb9c65fd8bc7d3a3e8bad97f
4
+ data.tar.gz: 3db4128c29680b07b59c66a76e8a53daccac99fc589d418ad0e0141c28069d97
5
5
  SHA512:
6
- metadata.gz: 639a44361ec0e66d04a03f850c828be566c6c914fcbce3cd3a268b53d8ddb83ac971376a8079f65d2f40794f6e546f91696fb2377f7a9937a2f129a94a46c358
7
- data.tar.gz: 4ad1211996483b99d7f3f4a416d6c05540dac98e98a94a4518a31bb1472256aa95c80f8baecb6cf05b0112f99298f8f973a5e1fe4e1f53c481fe14b72acfdf25
6
+ metadata.gz: 5ceca930f5086d7fd0a1814de7cc6915bbace3a0d6c85ba57e19f120004901d1ee823da5d8221c11d1d67a6e3bf82702ea959cce60246aef49b86b423a3b4744
7
+ data.tar.gz: 2ceaa7be546bde7d013c72dda00829c8f06b52267999c2e98024887d7664d81385ece7536cd3f66b04442d123edb3058d68d824ea27adc78a87e91fb62ed4374
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.0.5] - 2025-02-17
2
+
3
+ - Support separate base_url and path helpers
4
+ - Eliminate concept of separate mixins for different HTTP methods
5
+
1
6
  ## [0.0.3] - 2025-02-08
2
7
 
3
8
  - Implement HttpApiPostCommand
@@ -0,0 +1,96 @@
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,
13
+ :foobara_base_url,
14
+ :foobara_path,
15
+ :foobara_url_block,
16
+ :foobara_url,
17
+ :foobara_http_method
18
+
19
+ def http_method(method = nil)
20
+ if method
21
+ self.foobara_http_method = method
22
+ else
23
+ foobara_http_method || :get
24
+ end
25
+ end
26
+
27
+ def base_url(url = nil, &block)
28
+ if block_given?
29
+ unless url.nil?
30
+ # :nocov:
31
+ raise ArgumentError, "Cannot specify both url and block"
32
+ # :nocov:
33
+ end
34
+
35
+ self.foobara_base_url_block = block
36
+ elsif url
37
+ self.foobara_base_url = url
38
+ else
39
+ # :nocov:
40
+ raise ArgumentError, "No base url specified"
41
+ # :nocov:
42
+ end
43
+ end
44
+
45
+ def path(path = nil)
46
+ if path
47
+ self.foobara_path = path
48
+ end
49
+ end
50
+
51
+ def url(uri = nil, &block)
52
+ if block_given?
53
+ unless uri.nil?
54
+ # :nocov:
55
+ raise ArgumentError, "Cannot specify both uri and block"
56
+ # :nocov:
57
+ end
58
+
59
+ self.foobara_url_block = block
60
+ elsif uri
61
+ self.foobara_url = uri
62
+ else
63
+ # :nocov:
64
+ raise ArgumentError, "Must give either a url or a block that returns url"
65
+ # :nocov:
66
+ end
67
+ end
68
+
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}"
86
+ 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:
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -4,6 +4,7 @@ require "net/http"
4
4
  module Foobara
5
5
  module HttpApiCommand
6
6
  include Concern
7
+ include Concerns::Url
7
8
 
8
9
  def execute
9
10
  build_request_body
@@ -15,10 +16,6 @@ module Foobara
15
16
 
16
17
  attr_accessor :request_body, :request_headers, :response, :response_body
17
18
 
18
- def api_url
19
- @api_url ||= instance_eval(&self.class.foobara_url_block)
20
- end
21
-
22
19
  def build_request_body
23
20
  self.request_body = {}
24
21
  end
@@ -30,9 +27,19 @@ module Foobara
30
27
  end
31
28
 
32
29
  def issue_http_request
33
- # :nocov:
34
- raise "subclass responsibility"
35
- # :nocov:
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
36
43
  end
37
44
 
38
45
  def build_result
@@ -51,17 +58,5 @@ module Foobara
51
58
 
52
59
  self.response_body = JSON.parse(json)
53
60
  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
61
  end
67
62
  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.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-08 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,9 +34,8 @@ files:
34
34
  - LICENSE.txt
35
35
  - README.md
36
36
  - lib/foobara/http_api_command.rb
37
+ - src/foobara/concerns/url.rb
37
38
  - src/foobara/http_api_command.rb
38
- - src/foobara/http_api_get_command.rb
39
- - src/foobara/http_api_post_command.rb
40
39
  homepage: https://github.com/foobara/http-api-command
41
40
  licenses:
42
41
  - MPL-2.0
@@ -1,14 +0,0 @@
1
- require_relative "http_api_command"
2
-
3
- module Foobara
4
- module HttpApiGetCommand
5
- include Concern
6
- include HttpApiCommand
7
-
8
- def issue_http_request
9
- uri = URI(api_url)
10
- uri.query = URI.encode_www_form(request_body)
11
- self.response = Net::HTTP.get_response(uri, request_headers)
12
- end
13
- end
14
- end
@@ -1,13 +0,0 @@
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