Class: TargetIO::TrainCompat::HTTP
- Inherits:
-
Object
- Object
- TargetIO::TrainCompat::HTTP
- Defined in:
- lib/chef/target_io/train/http.rb
Constant Summary collapse
- SUPPORTED_COMMANDS =
%w{curl wget}.freeze
Instance Attribute Summary collapse
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
Instance Method Summary collapse
-
#curl(cmd, method, url, headers, _data) ⇒ Object
Sending data is not yet supported.
-
#delete(path, headers = {}) ⇒ Object
Send an HTTP DELETE request to the path.
-
#get(path, headers = {}) ⇒ Object
Send an HTTP GET request to the path.
-
#head(path, headers = {}) ⇒ Object
Send an HTTP HEAD request to the path.
-
#initialize(url, options = {}) ⇒ HTTP
constructor
A new instance of HTTP.
-
#post(path, json, headers = {}) ⇒ Object
Send an HTTP POST request to the path.
-
#put(path, json, headers = {}) ⇒ Object
Send an HTTP PUT request to the path.
- #request(method, path, headers = {}, data = false) ⇒ Object
-
#streaming_request(path, headers = {}, tempfile = nil) ⇒ Object
Used inside Chef::Provider::RemoteFile::HTTPS.
-
#wget(cmd, method, url, headers, _data) ⇒ Object
Sending data is not yet supported.
-
#which(cmd) ⇒ Object
extend Chef::Mixin::Which.
Constructor Details
#initialize(url, options = {}) ⇒ HTTP
Returns a new instance of HTTP.
8 9 10 11 12 |
# File 'lib/chef/target_io/train/http.rb', line 8 def initialize(url, = {}) @url = url.is_a?(URI) ? url.to_s : url @options = @last_response = "" end |
Instance Attribute Details
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
6 7 8 |
# File 'lib/chef/target_io/train/http.rb', line 6 def last_response @last_response end |
Instance Method Details
#curl(cmd, method, url, headers, _data) ⇒ Object
Sending data is not yet supported
96 97 98 99 100 |
# File 'lib/chef/target_io/train/http.rb', line 96 def curl(cmd, method, url, headers, _data) cmd += headers.map { |name, value| " --header '#{name}: #{value}'" }.join cmd += " --request #{method} " cmd += url end |
#delete(path, headers = {}) ⇒ Object
Send an HTTP DELETE request to the path
Parameters
- path
-
path part of the request URL
50 51 52 |
# File 'lib/chef/target_io/train/http.rb', line 50 def delete(path, headers = {}) request(:DELETE, path, headers) end |
#get(path, headers = {}) ⇒ Object
Send an HTTP GET request to the path
Parameters
- path
-
The path to GET
26 27 28 |
# File 'lib/chef/target_io/train/http.rb', line 26 def get(path, headers = {}) request(:GET, path, headers) end |
#head(path, headers = {}) ⇒ Object
Send an HTTP HEAD request to the path
Parameters
- path
-
path part of the request URL
18 19 20 |
# File 'lib/chef/target_io/train/http.rb', line 18 def head(path, headers = {}) request(:HEAD, path, headers) end |
#post(path, json, headers = {}) ⇒ Object
Send an HTTP POST request to the path
Parameters
- path
-
path part of the request URL
42 43 44 |
# File 'lib/chef/target_io/train/http.rb', line 42 def post(path, json, headers = {}) request(:POST, path, headers, json) end |
#put(path, json, headers = {}) ⇒ Object
Send an HTTP PUT request to the path
Parameters
- path
-
path part of the request URL
34 35 36 |
# File 'lib/chef/target_io/train/http.rb', line 34 def put(path, json, headers = {}) request(:PUT, path, headers, json) end |
#request(method, path, headers = {}, data = false) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chef/target_io/train/http.rb', line 65 def request(method, path, headers = {}, data = false) cmd = nil path = path.is_a?(URI) ? path.to_s : path headers.merge!(@options[:headers] || {}) SUPPORTED_COMMANDS.each do |command_name| executable = which(command_name).chop next if !executable || executable.empty? # There are different ways to call (constructor, argument, combination of both) full_url = if path.start_with?("http") path elsif path.empty? || @url.end_with?(path) @url else File.join(@url, path) end cmd = send(command_name.to_sym, executable, method.to_s.upcase, full_url, headers, data) break end raise "Target needs one of #{SUPPORTED_COMMANDS.join("/")} for HTTP requests to work" unless cmd connection = Chef.run_context&.transport_connection connection.run_command(cmd).stdout end |
#streaming_request(path, headers = {}, tempfile = nil) ⇒ Object
Used inside Chef::Provider::RemoteFile::HTTPS
55 56 57 58 59 60 61 62 63 |
# File 'lib/chef/target_io/train/http.rb', line 55 def streaming_request(path, headers = {}, tempfile = nil) content = get(path, headers) @last_response = content tempfile.write(content) tempfile.close tempfile end |
#wget(cmd, method, url, headers, _data) ⇒ Object
Sending data is not yet supported
103 104 105 106 107 108 |
# File 'lib/chef/target_io/train/http.rb', line 103 def wget(cmd, method, url, headers, _data) cmd += headers.map { |name, value| " --header '#{name}: #{value}'" }.join cmd += " --method #{method}" cmd += " --output-document=- " cmd += url end |
#which(cmd) ⇒ Object
extend Chef::Mixin::Which
111 112 113 114 |
# File 'lib/chef/target_io/train/http.rb', line 111 def which(cmd) connection = Chef.run_context&.transport_connection connection.run_command("which #{cmd}").stdout end |