class HTTPDisk::Cli::Main
Command line httpdisk command.
Attributes
options[R]
Public Class Methods
new(options)
click to toggle source
# File lib/httpdisk/cli/main.rb, line 11 def initialize(options) @options = options end
Public Instance Methods
client_options()
click to toggle source
Options to HTTPDisk::Client
# File lib/httpdisk/cli/main.rb, line 162 def client_options client_options = options.slice(:dir, :expires, :force, :force_errors) client_options[:utf8] = true client_options end
create_faraday()
click to toggle source
# File lib/httpdisk/cli/main.rb, line 40 def create_faraday Faraday.new do # connection settings _1.proxy = options[:proxy] if options[:proxy] _1.options.timeout = options[:max_time] if options[:max_time] # cookie middleware _1.use :cookie_jar # BEFORE httpdisk so each redirect segment is cached _1.response :follow_redirects # httpdisk _1.use :httpdisk, client_options # AFTER httpdisk so transient failures are not cached if options[:retry] # we have a very liberal retry policy retry_options = { max: options[:retry], methods: %w[delete get head options patch post put trace], retry_statuses: (500..600).to_a, retry_if: ->(_env, _err) { true }, } _1.request :retry, retry_options end end end
output(response, f)
click to toggle source
Output response to f
# File lib/httpdisk/cli/main.rb, line 88 def output(response, f) if options[:include] f.puts "HTTPDISK #{response.status} #{response.reason_phrase}" response.headers.each { f.puts("#{_1}: #{_2}") } f.puts end f.write(response.body) end
request_body()
click to toggle source
Request body
# File lib/httpdisk/cli/main.rb, line 135 def request_body options[:data] end
request_headers()
click to toggle source
Request headers
# File lib/httpdisk/cli/main.rb, line 140 def request_headers {}.tap do |headers| if options[:user_agent] headers['User-Agent'] = options[:user_agent] end options[:header].each do |header| key, value = header.split(': ', 2) if !key || !value || key.empty? || value.empty? raise CliError, "invalid --header #{header.inspect}" end headers[key] = value end end end
request_method()
click to toggle source
HTTP method (get, post, etc.)
# File lib/httpdisk/cli/main.rb, line 102 def request_method method = if options[:request] options[:request] elsif options[:data] 'post' end method ||= 'get' method = method.downcase.to_sym if !Faraday::Connection::METHODS.include?(method) raise CliError, "invalid --request #{method.inspect}" end method end
request_url()
click to toggle source
Request url
# File lib/httpdisk/cli/main.rb, line 119 def request_url url = options[:url] # recover from missing http: if url !~ %r{^https?://}i if url =~ %r{^\w+://} raise CliError, 'only http/https supported' end url = "http://#{url}" end URI.parse(url) rescue URI::InvalidURIError raise CliError, "invalid url #{url.inspect}" end
run()
click to toggle source
Make the request (or print status)
# File lib/httpdisk/cli/main.rb, line 16 def run # short circuit --status if options[:status] status return end # create Faraday client faraday = create_faraday # run request response = faraday.run_request(request_method, request_url, request_body, request_headers) if response.status >= 400 raise CliError, "the requested URL returned error: #{response.status} #{response.reason_phrase}" end # output if options[:output] File.open(options[:output], 'w') { output(response, _1) } else output(response, $stdout) end end
status()
click to toggle source
Support for –status
# File lib/httpdisk/cli/main.rb, line 70 def status # build env env = Faraday::Env.new.tap do _1.method = request_method _1.request_body = request_body _1.request_headers = request_headers # Run the url through Faraday to make sure we see the same stuff as middleware. _1.url = Faraday.new.build_url(request_url) end # now print status client = HTTPDisk::Client.new(nil, client_options) client.status(env).each do puts "#{_1}: #{_2.inspect}" end end