Class: HTTPDisk::Cli::Main
- Inherits:
-
Object
- Object
- HTTPDisk::Cli::Main
- Defined in:
- lib/httpdisk/cli/main.rb
Overview
Command line httpdisk command.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#client_options ⇒ Object
Options to HTTPDisk::Client.
- #create_faraday ⇒ Object
-
#initialize(options) ⇒ Main
constructor
A new instance of Main.
-
#output(response, f) ⇒ Object
Output response to f.
-
#request_body ⇒ Object
Request body.
-
#request_headers ⇒ Object
Request headers.
-
#request_method ⇒ Object
HTTP method (get, post, etc.).
-
#request_url ⇒ Object
Request url.
-
#run ⇒ Object
Make the request (or print status).
-
#status ⇒ Object
Support for –status.
Constructor Details
#initialize(options) ⇒ Main
Returns a new instance of Main.
11 12 13 |
# File 'lib/httpdisk/cli/main.rb', line 11 def initialize() @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/httpdisk/cli/main.rb', line 9 def @options end |
Instance Method Details
#client_options ⇒ Object
Options to HTTPDisk::Client
162 163 164 165 166 |
# File 'lib/httpdisk/cli/main.rb', line 162 def = .slice(:dir, :expires, :force, :force_errors) [:utf8] = true end |
#create_faraday ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/httpdisk/cli/main.rb', line 40 def create_faraday Faraday.new do # connection settings _1.proxy = [:proxy] if [:proxy] _1..timeout = [:max_time] if [:max_time] # cookie middleware _1.use :cookie_jar # BEFORE httpdisk so each redirect segment is cached _1.response :follow_redirects # httpdisk _1.use :httpdisk, # AFTER httpdisk so transient failures are not cached if [:retry] # we have a very liberal retry policy = { max: [: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, end end end |
#output(response, f) ⇒ Object
Output response to f
88 89 90 91 92 93 94 95 |
# File 'lib/httpdisk/cli/main.rb', line 88 def output(response, f) if [: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 ⇒ Object
Request body
135 136 137 |
# File 'lib/httpdisk/cli/main.rb', line 135 def request_body [:data] end |
#request_headers ⇒ Object
Request headers
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/httpdisk/cli/main.rb', line 140 def request_headers {}.tap do |headers| if [:user_agent] headers["User-Agent"] = [:user_agent] end [: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 ⇒ Object
HTTP method (get, post, etc.)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/httpdisk/cli/main.rb', line 102 def request_method method = if [:request] [:request] elsif [: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 ⇒ Object
Request url
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/httpdisk/cli/main.rb', line 119 def request_url url = [:url] # recover from missing http: if !%r{^https?://}i.match?(url) if %r{^\w+://}.match?(url) 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 ⇒ Object
Make the request (or print status)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/httpdisk/cli/main.rb', line 16 def run # short circuit --status if [: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 [:output] File.open([:output], "w") { output(response, _1) } else output(response, $stdout) end end |
#status ⇒ Object
Support for –status
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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.status(env).each do puts "#{_1}: #{_2.inspect}" end end |