class Curl::Spawn::ArgsBuilder

Public Class Methods

new() click to toggle source
# File lib/curl/spawn/args_builder.rb, line 8
def initialize
  @user = nil
  @password = nil
  @scheme = 'http'
  @ssl_no_verify = false
  @host = 'localhost'
  @port = 80
  @path = '/'
  @verb = 'GET'
  @queries = {}
  @headers = {}
  @data = nil

  @show_headers = false
  @verbose = false
end

Public Instance Methods

build!() click to toggle source
# File lib/curl/spawn/args_builder.rb, line 108
def build!
  args = ::Curl::Spawn::Args.new

  args.argv.push('curl')
  args.argv.push('-k') if @ssl_no_verify
  args.argv.push('--user') unless @user.nil?
  args.argv.push("#{@user}#{":#{@password}" unless @password.nil?}")

  query_str = ''
  @queries.each do |k, v|
    if query_str == ''
      query_str << '?'
    else
      query_str << '&'
    end
    query_str << "#{ERB::Util.url_encode(k)}=#{ERB::Util.url_encode(v)}"
  end

  args.argv.push('-X')
  args.argv.push(@verb.upcase)

  args.argv.push("#{@scheme}://#{@host}:#{@port}#{@path}#{query_str}")

  @headers.each do |k, v|
    args.argv.push("-H")
    args.argv.push("'#{k}: #{v}'")
  end

  if @data
    args.argv.push('--data-binary')
    args.argv.push('@-')
    args.opt[:in] = @data
  end

  if @show_headers
    args.argv.push('-i')
  end

  if @verbose
    args.argv.push('-v')
  end

  args.argv = args.argv.reject { |arg| arg.nil? || arg.empty? }.map { |arg| arg.to_s }

  args
end
content(d)
Alias for: data
content=(d)
Alias for: data
data(d) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 90
def data(d)
  @data =d
end
Also aliased as: data=, content, content=
data=(d)
Alias for: data
dump_headers(v=true) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 97
def dump_headers(v=true)
  @show_headers = v
end
dump_headers=(v=true)
Alias for: dump_headers
header(h) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 85
def header(h)
  @headers.merge!(h)
end
Also aliased as: headers
headers(h)
Alias for: header
host(h) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 59
def host(h)
  @host = h
end
Also aliased as: host=
host=(h)
Alias for: host
http() click to toggle source
# File lib/curl/spawn/args_builder.rb, line 46
def http
  @scheme = 'http'
end
https() click to toggle source
# File lib/curl/spawn/args_builder.rb, line 50
def https
  @scheme = 'https'
end
password(p) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 36
def password(p)
  @password = p
end
Also aliased as: password=
password=(p)
Alias for: password
path(p) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 69
def path(p)
  p = "/#{p}" unless p.start_with?('/')
  @path = p
end
Also aliased as: path=
path=(p)
Alias for: path
port(p) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 64
def port(p)
  @port = p
end
Also aliased as: port=
port=(p)
Alias for: port
queries(q)
Alias for: query
query(q) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 80
def query(q)
  @queries.merge!(q)
end
Also aliased as: queries
scheme(h) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 41
def scheme(h)
  @scheme = h
end
Also aliased as: scheme=
scheme=(h)
Alias for: scheme
show_headers(v=true)
Alias for: dump_headers
show_headers=(v=true)
Alias for: dump_headers
ssl_no_verify(val = true) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 54
def ssl_no_verify(val = true)
  @ssl_no_verify = val
end
Also aliased as: ssl_no_verify=
ssl_no_verify=(val = true)
Alias for: ssl_no_verify
url_encode(str) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 25
def url_encode(str)
  Curl.url_encode(str)
end
user(user) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 29
def user(user)
  @user = user
end
Also aliased as: user=, username, username=
user=(user)
Alias for: user
username(user)
Alias for: user
username=(user)
Alias for: user
verb(v) click to toggle source
# File lib/curl/spawn/args_builder.rb, line 75
def verb(v)
  @verb = v
end
Also aliased as: verb=
verb=(v)
Alias for: verb
verbose() click to toggle source
# File lib/curl/spawn/args_builder.rb, line 104
def verbose
  @verbose = true
end