httpdisk 0.5.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +4 -8
- data/.rubocop.yml +10 -24
- data/Gemfile +7 -7
- data/Gemfile.lock +51 -54
- data/README.md +8 -2
- data/Rakefile +3 -56
- data/bin/httpdisk +8 -8
- data/bin/httpdisk-grep +9 -9
- data/examples.rb +17 -17
- data/httpdisk.gemspec +17 -16
- data/justfile +59 -0
- data/lib/httpdisk/cache.rb +7 -6
- data/lib/httpdisk/cache_key.rb +19 -19
- data/lib/httpdisk/cli/args.rb +27 -27
- data/lib/httpdisk/cli/main.rb +12 -12
- data/lib/httpdisk/client.rb +12 -12
- data/lib/httpdisk/grep/args.rb +10 -10
- data/lib/httpdisk/grep/main.rb +27 -24
- data/lib/httpdisk/grep/printer.rb +5 -4
- data/lib/httpdisk/payload.rb +3 -3
- data/lib/httpdisk/slop_duration.rb +3 -3
- data/lib/httpdisk/sloptions.rb +4 -4
- data/lib/httpdisk/version.rb +1 -1
- data/lib/httpdisk.rb +13 -13
- metadata +14 -12
data/lib/httpdisk/cache_key.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "cgi"
|
2
|
+
require "digest/md5"
|
3
|
+
require "uri"
|
4
4
|
|
5
5
|
module HTTPDisk
|
6
6
|
class CacheKey
|
@@ -10,7 +10,7 @@ module HTTPDisk
|
|
10
10
|
@env, @ignore_params = env, ignore_params
|
11
11
|
|
12
12
|
# sanity checks
|
13
|
-
raise InvalidUrl, "http/https required #{env.url.inspect}" if env.url.scheme
|
13
|
+
raise InvalidUrl, "http/https required #{env.url.inspect}" if !/^https?$/.match?(env.url.scheme)
|
14
14
|
raise InvalidUrl, "hostname required #{env.url.inspect}" if !env.url.host
|
15
15
|
end
|
16
16
|
|
@@ -43,23 +43,23 @@ module HTTPDisk
|
|
43
43
|
def calculate_key
|
44
44
|
key = []
|
45
45
|
key << env.method.upcase
|
46
|
-
key <<
|
46
|
+
key << " "
|
47
47
|
key << url.scheme
|
48
|
-
key <<
|
48
|
+
key << "://"
|
49
49
|
key << url.host.downcase
|
50
50
|
if !default_port?
|
51
|
-
key <<
|
51
|
+
key << ":"
|
52
52
|
key << url.port
|
53
53
|
end
|
54
|
-
if url.path !=
|
54
|
+
if url.path != "/"
|
55
55
|
key << url.path
|
56
56
|
end
|
57
|
-
if (q = url.query) && q !=
|
58
|
-
key <<
|
57
|
+
if (q = url.query) && q != ""
|
58
|
+
key << "?"
|
59
59
|
key << querykey(q)
|
60
60
|
end
|
61
61
|
if env.request_body
|
62
|
-
key <<
|
62
|
+
key << " "
|
63
63
|
key << bodykey
|
64
64
|
end
|
65
65
|
key.join
|
@@ -68,7 +68,7 @@ module HTTPDisk
|
|
68
68
|
# Calculate cache key segment for body
|
69
69
|
def bodykey
|
70
70
|
body = env.request_body.to_s
|
71
|
-
if env.request_headers[
|
71
|
+
if env.request_headers["Content-Type"] == "application/x-www-form-urlencoded"
|
72
72
|
querykey(body)
|
73
73
|
elsif body.length < 50
|
74
74
|
body
|
@@ -79,16 +79,16 @@ module HTTPDisk
|
|
79
79
|
|
80
80
|
# Calculate canonical key for a query
|
81
81
|
def querykey(q)
|
82
|
-
parts = q.split(
|
82
|
+
parts = q.split("&").sort
|
83
83
|
if !ignore_params.empty?
|
84
84
|
parts = parts.map do |part|
|
85
|
-
key, value = part.split(
|
85
|
+
key, value = part.split("=", 2)
|
86
86
|
next if ignore_params.include?(key)
|
87
87
|
|
88
88
|
"#{key}=#{value}"
|
89
89
|
end.compact
|
90
90
|
end
|
91
|
-
parts.join(
|
91
|
+
parts.join("&")
|
92
92
|
end
|
93
93
|
|
94
94
|
def default_port?
|
@@ -98,10 +98,10 @@ module HTTPDisk
|
|
98
98
|
# Calculate nice directory name from url.host
|
99
99
|
def hostdir
|
100
100
|
hostdir = url.host.downcase
|
101
|
-
hostdir = hostdir.gsub(/^www\./,
|
102
|
-
hostdir = hostdir.gsub(/[^a-z0-9._-]/,
|
103
|
-
hostdir = hostdir.squeeze(
|
104
|
-
hostdir =
|
101
|
+
hostdir = hostdir.gsub(/^www\./, "")
|
102
|
+
hostdir = hostdir.gsub(/[^a-z0-9._-]/, "")
|
103
|
+
hostdir = hostdir.squeeze(".")
|
104
|
+
hostdir = "any" if hostdir.empty?
|
105
105
|
hostdir
|
106
106
|
end
|
107
107
|
end
|
data/lib/httpdisk/cli/args.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# manually load dependencies here since this is loaded standalone by bin
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
2
|
+
require "httpdisk/error"
|
3
|
+
require "httpdisk/slop_duration"
|
4
|
+
require "httpdisk/version"
|
5
|
+
require "slop"
|
6
6
|
|
7
7
|
module HTTPDisk
|
8
8
|
module Cli
|
@@ -10,43 +10,43 @@ module HTTPDisk
|
|
10
10
|
module Args
|
11
11
|
def self.slop(args)
|
12
12
|
slop = Slop.parse(args) do |o|
|
13
|
-
o.banner =
|
13
|
+
o.banner = "httpdisk [options] [url]"
|
14
14
|
|
15
15
|
# similar to curl
|
16
|
-
o.separator
|
17
|
-
o.string
|
18
|
-
o.array
|
19
|
-
o.boolean
|
20
|
-
o.integer
|
21
|
-
o.string
|
22
|
-
o.string
|
23
|
-
o.string
|
24
|
-
o.integer
|
25
|
-
o.boolean
|
26
|
-
o.string
|
16
|
+
o.separator "Similar to curl:"
|
17
|
+
o.string "-d", "--data", "HTTP POST data"
|
18
|
+
o.array "-H", "--header", "pass custom header(s) to server", delimiter: nil
|
19
|
+
o.boolean "-i", "--include", "include response headers in the output"
|
20
|
+
o.integer "-m", "--max-time", "maximum time allowed for the transfer"
|
21
|
+
o.string "-o", "--output", "write to file instead of stdout"
|
22
|
+
o.string "-x", "--proxy", "use host[:port] as proxy"
|
23
|
+
o.string "-X", "--request", "HTTP method to use"
|
24
|
+
o.integer "--retry", "retry request if problems occur"
|
25
|
+
o.boolean "-s", "--silent", "silent mode (don't print errors)"
|
26
|
+
o.string "-A", "--user-agent", "send User-Agent to server"
|
27
27
|
|
28
28
|
# from httpdisk
|
29
|
-
o.separator
|
30
|
-
o.string
|
31
|
-
o.duration
|
32
|
-
o.boolean
|
33
|
-
o.boolean
|
34
|
-
o.boolean
|
29
|
+
o.separator "Specific to httpdisk:"
|
30
|
+
o.string "--dir", "httpdisk cache directory (defaults to ~/httpdisk)"
|
31
|
+
o.duration "--expires", "when to expire cached requests (ex: 1h, 2d, 3w)"
|
32
|
+
o.boolean "--force", "don't read anything from cache (but still write)"
|
33
|
+
o.boolean "--force-errors", "don't read errors from cache (but still write)"
|
34
|
+
o.boolean "--status", "show status for a url in the cache"
|
35
35
|
|
36
36
|
# generic
|
37
|
-
o.boolean
|
37
|
+
o.boolean "--version", "show version" do
|
38
38
|
puts "httpdisk #{HTTPDisk::VERSION}"
|
39
39
|
exit
|
40
40
|
end
|
41
|
-
o.on
|
41
|
+
o.on "--help", "show this help" do
|
42
42
|
puts o
|
43
43
|
exit
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
raise Slop::Error,
|
48
|
-
raise Slop::Error,
|
49
|
-
raise Slop::Error,
|
47
|
+
raise Slop::Error, "" if args.empty?
|
48
|
+
raise Slop::Error, "no URL specified" if slop.args.empty?
|
49
|
+
raise Slop::Error, "more than one URL specified" if slop.args.length > 1
|
50
50
|
|
51
51
|
slop.to_h.tap do
|
52
52
|
_1[:url] = slop.args.first
|
data/lib/httpdisk/cli/main.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "faraday-cookie_jar"
|
2
|
+
require "faraday/follow_redirects"
|
3
|
+
require "ostruct"
|
4
4
|
|
5
5
|
module HTTPDisk
|
6
6
|
module Cli
|
@@ -31,7 +31,7 @@ module HTTPDisk
|
|
31
31
|
|
32
32
|
# output
|
33
33
|
if options[:output]
|
34
|
-
File.open(options[:output],
|
34
|
+
File.open(options[:output], "w") { output(response, _1) }
|
35
35
|
else
|
36
36
|
output(response, $stdout)
|
37
37
|
end
|
@@ -59,7 +59,7 @@ module HTTPDisk
|
|
59
59
|
max: options[:retry],
|
60
60
|
methods: %w[delete get head options patch post put trace],
|
61
61
|
retry_statuses: (500..600).to_a,
|
62
|
-
retry_if: ->(_env, _err) { true }
|
62
|
+
retry_if: ->(_env, _err) { true }
|
63
63
|
}
|
64
64
|
_1.request :retry, retry_options
|
65
65
|
end
|
@@ -103,9 +103,9 @@ module HTTPDisk
|
|
103
103
|
method = if options[:request]
|
104
104
|
options[:request]
|
105
105
|
elsif options[:data]
|
106
|
-
|
106
|
+
"post"
|
107
107
|
end
|
108
|
-
method ||=
|
108
|
+
method ||= "get"
|
109
109
|
method = method.downcase.to_sym
|
110
110
|
|
111
111
|
if !Faraday::Connection::METHODS.include?(method)
|
@@ -119,9 +119,9 @@ module HTTPDisk
|
|
119
119
|
def request_url
|
120
120
|
url = options[:url]
|
121
121
|
# recover from missing http:
|
122
|
-
if
|
123
|
-
if
|
124
|
-
raise CliError,
|
122
|
+
if !%r{^https?://}i.match?(url)
|
123
|
+
if %r{^\w+://}.match?(url)
|
124
|
+
raise CliError, "only http/https supported"
|
125
125
|
end
|
126
126
|
|
127
127
|
url = "http://#{url}"
|
@@ -140,11 +140,11 @@ module HTTPDisk
|
|
140
140
|
def request_headers
|
141
141
|
{}.tap do |headers|
|
142
142
|
if options[:user_agent]
|
143
|
-
headers[
|
143
|
+
headers["User-Agent"] = options[:user_agent]
|
144
144
|
end
|
145
145
|
|
146
146
|
options[:header].each do |header|
|
147
|
-
key, value = header.split(
|
147
|
+
key, value = header.split(": ", 2)
|
148
148
|
if !key || !value || key.empty? || value.empty?
|
149
149
|
raise CliError, "invalid --header #{header.inspect}"
|
150
150
|
end
|
data/lib/httpdisk/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "content-type"
|
2
|
+
require "faraday"
|
3
|
+
require "logger"
|
4
4
|
|
5
5
|
module HTTPDisk
|
6
6
|
# Middleware and main entry point.
|
@@ -9,7 +9,7 @@ module HTTPDisk
|
|
9
9
|
|
10
10
|
def initialize(app, options = {})
|
11
11
|
options = Sloptions.parse(options) do
|
12
|
-
_1.string :dir, default: File.join(ENV[
|
12
|
+
_1.string :dir, default: File.join(ENV["HOME"], "httpdisk")
|
13
13
|
_1.integer :expires
|
14
14
|
_1.boolean :force
|
15
15
|
_1.boolean :force_errors
|
@@ -23,12 +23,12 @@ module HTTPDisk
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def call(env)
|
26
|
-
cache_key = CacheKey.new(env, ignore_params:
|
26
|
+
cache_key = CacheKey.new(env, ignore_params:)
|
27
27
|
logger&.info("#{env.method.upcase} #{env.url} (#{cache.status(cache_key)})")
|
28
28
|
env[:httpdisk_diskpath] = cache.diskpath(cache_key)
|
29
29
|
|
30
30
|
# check cache, fallback to network
|
31
|
-
if response = read(cache_key, env)
|
31
|
+
if (response = read(cache_key, env))
|
32
32
|
response.env[:httpdisk] = true
|
33
33
|
else
|
34
34
|
response = perform(env)
|
@@ -48,7 +48,7 @@ module HTTPDisk
|
|
48
48
|
status: cache.status(cache_key).to_s,
|
49
49
|
key: cache_key.key,
|
50
50
|
digest: cache_key.digest,
|
51
|
-
path: cache.diskpath(cache_key)
|
51
|
+
path: cache.diskpath(cache_key)
|
52
52
|
}
|
53
53
|
end
|
54
54
|
|
@@ -90,7 +90,7 @@ module HTTPDisk
|
|
90
90
|
def stuff_999_response(env, err)
|
91
91
|
env.tap do
|
92
92
|
_1.reason_phrase = "#{err.class} #{err.message}"
|
93
|
-
_1.response_body =
|
93
|
+
_1.response_body = ""
|
94
94
|
_1.response_headers = Faraday::Utils::Headers.new
|
95
95
|
_1.status = HTTPDisk::ERROR_STATUS
|
96
96
|
end
|
@@ -110,11 +110,11 @@ module HTTPDisk
|
|
110
110
|
# network. Not all adapters honor Content-Type (including the default
|
111
111
|
# adapter).
|
112
112
|
def encode_body(response)
|
113
|
-
body = response.body ||
|
113
|
+
body = response.body || ""
|
114
114
|
|
115
115
|
# parse Content-Type
|
116
116
|
begin
|
117
|
-
content_type = response[
|
117
|
+
content_type = response["Content-Type"] && ContentType.parse(response["Content-Type"])
|
118
118
|
rescue Parslet::ParseFailed
|
119
119
|
# unparsable
|
120
120
|
end
|
@@ -130,7 +130,7 @@ module HTTPDisk
|
|
130
130
|
if options[:utf8] && content_type && response_text?(content_type)
|
131
131
|
body = body.dup if body.frozen?
|
132
132
|
begin
|
133
|
-
body.encode!(
|
133
|
+
body.encode!("UTF-8", invalid: :replace, undef: :replace, replace: "?")
|
134
134
|
rescue Encoding::ConverterNotFoundError
|
135
135
|
# rare, can't do anything here
|
136
136
|
body = "httpdisk could not convert from #{body.encoding.name} to UTF-8"
|
@@ -152,7 +152,7 @@ module HTTPDisk
|
|
152
152
|
end
|
153
153
|
|
154
154
|
def response_text?(content_type)
|
155
|
-
content_type.type ==
|
155
|
+
content_type.type == "text" || content_type.mime_type == "application/json"
|
156
156
|
end
|
157
157
|
|
158
158
|
#
|
data/lib/httpdisk/grep/args.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# manually load dependencies here since this is loaded standalone by bin
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require "httpdisk/version"
|
3
|
+
require "slop"
|
4
4
|
|
5
5
|
module HTTPDisk
|
6
6
|
module Grep
|
@@ -8,22 +8,22 @@ module HTTPDisk
|
|
8
8
|
# Slop parsing. This is broken out so we can run without require 'httpdisk'.
|
9
9
|
def self.slop(args)
|
10
10
|
slop = Slop.parse(args) do |o|
|
11
|
-
o.banner =
|
12
|
-
o.boolean
|
13
|
-
o.boolean
|
14
|
-
o.boolean
|
15
|
-
o.boolean
|
11
|
+
o.banner = "httpdisk-grep [options] pattern [path ...]"
|
12
|
+
o.boolean "-c", "--count", "suppress normal output and show count"
|
13
|
+
o.boolean "-h", "--head", "show req headers before each match"
|
14
|
+
o.boolean "-s", "--silent", "do not print anything to stdout"
|
15
|
+
o.boolean "--version", "show version" do
|
16
16
|
puts "httpdisk-grep #{HTTPDisk::VERSION}"
|
17
17
|
exit
|
18
18
|
end
|
19
|
-
o.on
|
19
|
+
o.on "--help", "show this help" do
|
20
20
|
puts o
|
21
21
|
exit
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
raise Slop::Error,
|
26
|
-
raise Slop::Error,
|
25
|
+
raise Slop::Error, "" if args.empty?
|
26
|
+
raise Slop::Error, "no PATTERN specified" if slop.args.empty?
|
27
27
|
|
28
28
|
slop.to_h.tap do
|
29
29
|
_1[:pattern] = slop.args.shift
|
data/lib/httpdisk/grep/main.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "find"
|
2
|
+
require "json"
|
3
3
|
|
4
4
|
module HTTPDisk
|
5
5
|
module Grep
|
@@ -13,25 +13,29 @@ module HTTPDisk
|
|
13
13
|
# Enumerate file paths one at a time. Returns true if matches were found.
|
14
14
|
def run
|
15
15
|
paths.each do
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
raise CliError, "#{e.message[0, 70]} (#{_1})"
|
25
|
-
end
|
16
|
+
run_one(_1)
|
17
|
+
rescue => e
|
18
|
+
if ENV["HTTPDISK_DEBUG"]
|
19
|
+
$stderr.puts
|
20
|
+
warn e.class
|
21
|
+
warn e.backtrace.join("\n")
|
22
|
+
end
|
23
|
+
raise CliError, "#{e.message[0, 70]} (#{_1})"
|
26
24
|
end
|
27
25
|
success
|
28
26
|
end
|
29
27
|
|
30
28
|
def run_one(path)
|
31
29
|
# read payload & body
|
32
|
-
|
33
|
-
|
30
|
+
begin
|
31
|
+
payload = Zlib::GzipReader.open(path, encoding: "ASCII-8BIT") do
|
32
|
+
Payload.read(_1)
|
33
|
+
end
|
34
|
+
rescue Zlib::GzipFile::Error
|
35
|
+
puts "httpdisk: #{path} not in gzip format, skipping" if !options[:silent]
|
36
|
+
return
|
34
37
|
end
|
38
|
+
|
35
39
|
body = prepare_body(payload)
|
36
40
|
|
37
41
|
# collect all_matches
|
@@ -51,14 +55,14 @@ module HTTPDisk
|
|
51
55
|
def paths
|
52
56
|
# roots
|
53
57
|
roots = options[:roots]
|
54
|
-
roots = [
|
58
|
+
roots = ["."] if roots.empty?
|
55
59
|
|
56
60
|
# find files in roots
|
57
61
|
paths = roots.flat_map { Find.find(_1).to_a }.sort
|
58
62
|
paths = paths.select { File.file?(_1) }
|
59
63
|
|
60
64
|
# strip default './'
|
61
|
-
paths = paths.map { _1.gsub(%r{^\./},
|
65
|
+
paths = paths.map { _1.gsub(%r{^\./}, "") } if options[:roots].empty?
|
62
66
|
paths
|
63
67
|
end
|
64
68
|
|
@@ -66,13 +70,13 @@ module HTTPDisk
|
|
66
70
|
def prepare_body(payload)
|
67
71
|
body = payload.body
|
68
72
|
|
69
|
-
if content_type = payload.headers[
|
73
|
+
if (content_type = payload.headers["Content-Type"])
|
70
74
|
# Mismatches between Content-Type and body.encoding are fatal, so make
|
71
75
|
# an effort to align them.
|
72
|
-
if charset = content_type[/charset=([^;]+)/, 1]
|
76
|
+
if (charset = content_type[/charset=([^;]+)/, 1])
|
73
77
|
encoding = begin
|
74
78
|
Encoding.find(charset)
|
75
|
-
rescue
|
79
|
+
rescue
|
76
80
|
nil
|
77
81
|
end
|
78
82
|
if encoding && body.encoding != encoding
|
@@ -81,7 +85,7 @@ module HTTPDisk
|
|
81
85
|
end
|
82
86
|
|
83
87
|
# pretty print json for easier searching
|
84
|
-
if
|
88
|
+
if /\bjson\b/.match?(content_type)
|
85
89
|
body = JSON.pretty_generate(JSON.parse(body))
|
86
90
|
end
|
87
91
|
end
|
@@ -96,12 +100,11 @@ module HTTPDisk
|
|
96
100
|
|
97
101
|
# printer for output
|
98
102
|
def printer
|
99
|
-
@printer ||=
|
100
|
-
when options[:silent]
|
103
|
+
@printer ||= if options[:silent]
|
101
104
|
Grep::SilentPrinter.new
|
102
|
-
|
105
|
+
elsif options[:count]
|
103
106
|
Grep::CountPrinter.new($stdout)
|
104
|
-
|
107
|
+
elsif options[:head] || $stdout.tty?
|
105
108
|
Grep::HeaderPrinter.new($stdout, options[:head])
|
106
109
|
else
|
107
110
|
Grep::TersePrinter.new($stdout)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module HTTPDisk
|
2
2
|
module Grep
|
3
3
|
class Printer
|
4
|
-
GREP_COLOR =
|
4
|
+
GREP_COLOR = "37;45".freeze
|
5
5
|
|
6
6
|
attr_reader :output
|
7
7
|
|
@@ -9,7 +9,8 @@ module HTTPDisk
|
|
9
9
|
@output = output
|
10
10
|
end
|
11
11
|
|
12
|
-
def print(path, payload, all_matches)
|
12
|
+
def print(path, payload, all_matches)
|
13
|
+
end
|
13
14
|
|
14
15
|
protected
|
15
16
|
|
@@ -18,7 +19,7 @@ module HTTPDisk
|
|
18
19
|
#
|
19
20
|
|
20
21
|
def grep_color
|
21
|
-
@grep_color ||= (ENV[
|
22
|
+
@grep_color ||= (ENV["GREP_COLOR"] || GREP_COLOR)
|
22
23
|
end
|
23
24
|
|
24
25
|
def print_matches(matches)
|
@@ -30,7 +31,7 @@ module HTTPDisk
|
|
30
31
|
result << s[ii..._1.begin(0)]
|
31
32
|
result << "\e["
|
32
33
|
result << grep_color
|
33
|
-
result <<
|
34
|
+
result << "m"
|
34
35
|
result << _1[0]
|
35
36
|
result << "\e[0m"
|
36
37
|
ii = _1.end(0)
|
data/lib/httpdisk/payload.rb
CHANGED
@@ -12,7 +12,7 @@ module HTTPDisk
|
|
12
12
|
|
13
13
|
# headers
|
14
14
|
while (line = f.gets.chomp) && !line.empty?
|
15
|
-
key, value = line.split(
|
15
|
+
key, value = line.split(": ", 2)
|
16
16
|
p.headers[key] = value
|
17
17
|
end
|
18
18
|
|
@@ -34,8 +34,8 @@ module HTTPDisk
|
|
34
34
|
attr_accessor :body, :comment, :headers, :reason_phrase, :status
|
35
35
|
|
36
36
|
def initialize
|
37
|
-
@body =
|
38
|
-
@comment =
|
37
|
+
@body = ""
|
38
|
+
@comment = ""
|
39
39
|
@headers = Faraday::Utils::Headers.new
|
40
40
|
end
|
41
41
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "slop"
|
2
2
|
|
3
3
|
module Slop
|
4
4
|
# Custom duration type for Slop, used for --expires. Raises aggressively
|
@@ -10,14 +10,14 @@ module Slop
|
|
10
10
|
h: 60 * 60,
|
11
11
|
d: 24 * 60 * 60,
|
12
12
|
w: 7 * 24 * 60 * 60,
|
13
|
-
y: 365 * 7 * 24 * 60 * 60
|
13
|
+
y: 365 * 7 * 24 * 60 * 60
|
14
14
|
}.freeze
|
15
15
|
|
16
16
|
def call(value)
|
17
17
|
m = value.match(/^(\d+)([smhdwy])?$/)
|
18
18
|
raise Slop::Error, "invalid --expires #{value.inspect}" if !m
|
19
19
|
|
20
|
-
num, unit = m[1].to_i, (m[2] ||
|
20
|
+
num, unit = m[1].to_i, (m[2] || "s").to_sym
|
21
21
|
num * UNITS[unit]
|
22
22
|
end
|
23
23
|
end
|
data/lib/httpdisk/sloptions.rb
CHANGED
@@ -32,10 +32,10 @@ module HTTPDisk
|
|
32
32
|
|
33
33
|
%i[array boolean float hash integer string symbol].each do |method|
|
34
34
|
define_method(method) do |flag, foptions = {}|
|
35
|
-
on(flag, {
|
35
|
+
on(flag, {type: method}.merge(foptions))
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
38
|
+
alias_method :bool, :boolean
|
39
39
|
|
40
40
|
#
|
41
41
|
# return parsed options
|
@@ -96,9 +96,9 @@ module HTTPDisk
|
|
96
96
|
def error_message(flag, value, valid)
|
97
97
|
classes = valid.compact.map do
|
98
98
|
s = _1.to_s
|
99
|
-
s = s.downcase if
|
99
|
+
s = s.downcase if /\b(Array|Float|Hash|Integer|String|Symbol)\b/.match?(s)
|
100
100
|
s
|
101
|
-
end.join(
|
101
|
+
end.join("/")
|
102
102
|
"expected :#{flag} to be #{classes}, not #{value.inspect}"
|
103
103
|
end
|
104
104
|
end
|
data/lib/httpdisk/version.rb
CHANGED
data/lib/httpdisk.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
1
|
+
require "httpdisk/cache_key"
|
2
|
+
require "httpdisk/cache"
|
3
|
+
require "httpdisk/client"
|
4
|
+
require "httpdisk/error"
|
5
|
+
require "httpdisk/payload"
|
6
|
+
require "httpdisk/slop_duration"
|
7
|
+
require "httpdisk/sloptions"
|
8
|
+
require "httpdisk/version"
|
9
9
|
|
10
10
|
# cli
|
11
|
-
require
|
12
|
-
require
|
11
|
+
require "httpdisk/cli/args"
|
12
|
+
require "httpdisk/cli/main"
|
13
13
|
|
14
14
|
# grep
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
15
|
+
require "httpdisk/grep/args"
|
16
|
+
require "httpdisk/grep/main"
|
17
|
+
require "httpdisk/grep/printer"
|