Class: TargetIO::TrainCompat::HTTP

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/chef/target_io/train/http.rb

Constant Summary collapse

SUPPORTED_COMMANDS =
%w{curl wget}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#clean_staging, #read_file, #remote_user, #run_command, #staging_file, #sudo?, #transport_connection, #upload, #write_file

Constructor Details

#initialize(url, options = {}) ⇒ HTTP

Returns a new instance of HTTP.



10
11
12
13
14
# File 'lib/chef/target_io/train/http.rb', line 10

def initialize(url, options = {})
  @url = url.is_a?(URI) ? url.to_s : url
  @options = options
  @last_response = ""
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



8
9
10
# File 'lib/chef/target_io/train/http.rb', line 8

def last_response
  @last_response
end

Instance Method Details

#curl(cmd, method, url, headers, _data) ⇒ Object

Sending data is not yet supported



97
98
99
100
101
# File 'lib/chef/target_io/train/http.rb', line 97

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



52
53
54
# File 'lib/chef/target_io/train/http.rb', line 52

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



28
29
30
# File 'lib/chef/target_io/train/http.rb', line 28

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



20
21
22
# File 'lib/chef/target_io/train/http.rb', line 20

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



44
45
46
# File 'lib/chef/target_io/train/http.rb', line 44

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



36
37
38
# File 'lib/chef/target_io/train/http.rb', line 36

def put(path, json, headers = {})
  request(:PUT, path, headers, json)
end

#request(method, path, headers = {}, data = false) ⇒ Object



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
92
# File 'lib/chef/target_io/train/http.rb', line 67

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

  run_command(cmd).stdout
end

#streaming_request(path, headers = {}, tempfile = nil) ⇒ Object

Used inside Chef::Provider::RemoteFile::HTTPS



57
58
59
60
61
62
63
64
65
# File 'lib/chef/target_io/train/http.rb', line 57

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



104
105
106
107
108
109
# File 'lib/chef/target_io/train/http.rb', line 104

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



112
113
114
# File 'lib/chef/target_io/train/http.rb', line 112

def which(cmd)
  run_command("which #{cmd}").stdout
end