Class: HTTPDisk::Cli::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/httpdisk/cli/main.rb

Overview

Command line httpdisk command.

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/httpdisk/cli/main.rb', line 9

def options
  @options
end

Instance Method Details

#client_optionsObject

Options to HTTPDisk::Client



162
163
164
165
166
# 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_faradayObject



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 = 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) ⇒ 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 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_bodyObject

Request body



135
136
137
# File 'lib/httpdisk/cli/main.rb', line 135

def request_body
  options[:data]
end

#request_headersObject

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 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_methodObject

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 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_urlObject

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 = options[: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

#runObject

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 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

#statusObject

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_options)
  client.status(env).each do
    puts "#{_1}: #{_2.inspect}"
  end
end