Module: Curl::Spawn

Defined in:
lib/curl/spawn.rb,
lib/curl/spawn/args.rb,
lib/curl/spawn/example.rb,
lib/curl/spawn/version.rb,
lib/curl/spawn/args_builder.rb

Defined Under Namespace

Classes: Args, ArgsBuilder

Constant Summary collapse

EXAMPLE =
"# - The block is executed in the context of a Curl::Spawn::ArgsBuilder, which\n#   provides a DSL for describing curl commands.\n#\n# - Any parameters to `Curl.spawn` are merged with the arguments derived\n#   from the DSL block, then passed through to `Kernel.spawn` when invoking curl.\n#   (Arguments derived from the block are preferred in case of conflicts.)\n#\n#   + You can use this to set positional arguments, redirect stdin/stdout/stderr,\n#     and a few other things. (Note: the env hash argument to `Kernel.spawn` is\n#     not supported through this api.)\n\npid = Curl.spawn('--progress-bar', out: $stdout, err: $stderr) {\n  https                   # Use https. Same as `scheme 'https'`. (default: http)\n  ssl_no_verify           # Don't verify ssl certs. (Primarily for development)\n\n  user $opt[:user] if $opt[:user]  # Set the username to use for authentication\n  password 'abcd1234'              # Set the password to use for authentication\n\n  verb :post              # Set the http method/verb (default: 'GET')\n  host 'localhost'        # Specify host (default: 'localhost')\n  port 8000               # On port 8000 (default: 80)\n  path '/'                # The path segment of the url (default: '/')\n\n  query :param => Curl.url_encode('value') # Set query params (alias: queries)\n  header :Accept => 'application/json'     # Set headers (alias: headers)\n\n  content $stdin          # IO object or string to use as the request content.\n                          # Note that this option overwrite the `:in` option\n                          # from the positional arguments to this method.\n\n  # For debugging, you can print out the generated Curl::Spawn::Args object\n  $stderr.puts build!.inspect\n}\n"
EXAMPLE_SCRIPT =
"#! /usr/bin/env ruby\n\nrequire 'curl/spawn'\n\n# User Manual\n# -----------\n\ndef help(status = 0)\n  puts <<-EOS\nUsage:\n  \\\#{File.basename(__FILE__)} [[--]help] [OPTION...] -- [CURL_ARG...]\n\nOPTION:\n  -u,--user=USER[:PASSWORD]\n    Username & optional password to authenticate with.\n\n  -h,--host=HOST\n    The host to send the request to (default: localhost).\n\n  -p,--port=PORT\n    The port to send the request to (default: 80).\n\n  -c,--content=FILENAME\n    The file to send as the request body. If FILENAME is a hyphen (-), then\n    stdin is used.\n\nCURL_ARG:\n  Any additional arguments after a -- are passed straight through to curl.\nEOS\n  exit status\nend\n\n# Argument Parsing\n# ----------------\n\n$opt = {\n  user: nil,\n  password: nil,\n  host: 'localhost',\n  port: 80,\n  content: nil\n}\n\nin_curl_args = false\ncurl_args = []\nARGV.dup.each do |arg|\n  if !in_curl_args\n    case arg\n    when '--help', 'help'\n      help(0)\n    when /^(?:-u|--user)=?(.*)$/\n      $opt[:user], $opt[:password] = $1.match(/([^:]*):?(.*)/).captures\n      ARGV.delete(arg)\n    when /^(?:-h|--host)=?(.*)$/\n      $opt[:host] = $1\n      ARGV.delete(arg)\n    when /^(?:-p|--port)=?(.*)$/\n      $opt[:port] = $1\n      ARGV.delete(arg)\n    when /^(?:-c|--content)=?(.*)$/\n      prev = $opt[:content]\n      prev.close if prev && prev != $stdin\n      if $1 == '-'\n        $opt[:content] = $stdin\n      else\n        $opt[:content] = File.open($1, 'r')\n      end\n      ARGV.delete(arg)\n    when '--'\n      in_curl_args = true\n      ARGV.delete(arg)\n    end\n  else\n    curl_args.push(arg)\n    ARGV.delete(arg)\n  end\nend\n\nif !ARGV.empty?\n  $stderr.puts \"Unexpected arguments: \\\#{ARGV.inspect}\"\n  help(1)\nend\n\n# Curl Invocation\n# ---------------\n\npid = Curl.spawn(*curl_args, out: $stdout, err: $stderr) {\n  https\n  ssl_no_verify\n\n  user $opt[:user] unless $opt[:user].nil? || $opt[:user].empty?\n  password $opt[:password] unless $opt[:password].nil? || $opt[:password].empty?\n\n  verb 'POST'\n  host $opt[:host]\n  port $opt[:port]\n  path '/'\n\n  header 'Content-Type' => 'application/json'\n  header 'Accept' => 'application/json'\n\n  content $opt[:content] if $opt[:content]\n\n  # # Debug:\n  # $stderr.puts build!.inspect\n}\n\nProcess.wait(pid)\nexit $?.exitstatus\n"
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.build_args(&block) ⇒ Object



7
8
9
10
11
# File 'lib/curl/spawn.rb', line 7

def self.build_args(&block)
  builder = ArgsBuilder.new
  builder.instance_eval(&block) if block
  builder.build!
end