console_utils 0.7.0 → 0.8.1
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 +5 -5
- data/lib/console_utils/active_record_utils.rb +1 -1
- data/lib/console_utils/active_record_utils/random_record.rb +10 -2
- data/lib/console_utils/request_utils/exap.rb +1 -1
- data/lib/console_utils/request_utils/remo.rb +38 -15
- data/lib/console_utils/request_utils/request_params.rb +1 -1
- data/lib/console_utils/request_utils/requester.rb +3 -3
- data/lib/console_utils/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1b476b929197a0b2c0c282737682ba40f6fe8c02d14858f253f569a47ea20c52
|
4
|
+
data.tar.gz: 77abfe7679f71f9f7aae8089ec2a660675d86c3ebe139ed9005b6ce4880473b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4c557b3bae82eaf659e3384351e51afa271965b8b0496414cb6624401c01063ba3cc49635571de0ac3e7c24eff3119e13fbc5d20058582a049844cec33508d5
|
7
|
+
data.tar.gz: 0ea55105bfdfea055fe4e21cd1e76f31f1966d1a7b835bc410683047f1bf126f627432c171ad3016fce5d47b95b29b21493c64689fa2332ec3e7cda52f75514c
|
@@ -6,7 +6,7 @@ module ConsoleUtils #:nodoc:
|
|
6
6
|
|
7
7
|
def self.extended(mod)
|
8
8
|
ActiveSupport.on_load(:active_record) do
|
9
|
-
ActiveRecord::Relation.send(:
|
9
|
+
ActiveRecord::Relation.send(:prepend, RandomRecord::FinderMethods)
|
10
10
|
ActiveRecord::Base.send(:extend, RandomRecord::Querying)
|
11
11
|
end
|
12
12
|
end
|
@@ -9,8 +9,16 @@ module ConsoleUtils::ActiveRecordUtils #:nodoc:
|
|
9
9
|
random.first
|
10
10
|
end
|
11
11
|
|
12
|
-
def anyid
|
13
|
-
|
12
|
+
def anyid(n = nil)
|
13
|
+
if n
|
14
|
+
@_anyid_history[-n.abs].presence || anyid()
|
15
|
+
else
|
16
|
+
idval = connection.select_value(select(:id).random.limit(1))
|
17
|
+
model.type_for_attribute('id').send(:cast_value, idval).tap do |result|
|
18
|
+
(@_anyid_history ||= []) << result
|
19
|
+
@_anyid_history.shift if @_anyid_history.size > 10
|
20
|
+
end
|
21
|
+
end
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
@@ -14,8 +14,9 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
14
14
|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
15
15
|
def #{request_method}(url, *args)
|
16
16
|
@_args = args
|
17
|
-
@url = urlify(url, *normalize_args)
|
18
17
|
@request_method = "#{request_method.to_s.upcase}"
|
18
|
+
@request_params = normalize_args
|
19
|
+
@url = urlify(url, @request_params.params)
|
19
20
|
perform
|
20
21
|
end
|
21
22
|
RUBY
|
@@ -38,7 +39,8 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
38
39
|
protected
|
39
40
|
|
40
41
|
def perform
|
41
|
-
|
42
|
+
data = @request_params.params.to_json unless params_to_query?
|
43
|
+
Curl.(request_method, url, data: data, headers: @request_params.headers) do |result, payload|
|
42
44
|
@_result = result
|
43
45
|
set_payload!(payload)
|
44
46
|
end
|
@@ -54,14 +56,18 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
54
56
|
self
|
55
57
|
end
|
56
58
|
|
57
|
-
def urlify(
|
58
|
-
|
59
|
-
|
60
|
-
|
59
|
+
def urlify(path, options = nil)
|
60
|
+
URI.join(ConsoleUtils.remote_endpoint, path).
|
61
|
+
tap { |uri| uri.query = options.to_query if options && params_to_query? }.to_s
|
62
|
+
end
|
63
|
+
|
64
|
+
def params_to_query?
|
65
|
+
["GET", "HEAD"].include?(@request_method) || @request_method.headers["Content-Type"] != "application/json"
|
61
66
|
end
|
62
67
|
|
63
68
|
class Curl
|
64
|
-
OUT_FORMAT =
|
69
|
+
OUT_FORMAT = '\n%{http_code}\n%{time_total}\n%{size_download}'.freeze
|
70
|
+
HEADER_JOIN_PROC = proc { |*kv| ["-H", kv.flatten.join(": ")] }
|
65
71
|
|
66
72
|
def self.call(*args)
|
67
73
|
result = new(*args)
|
@@ -70,20 +76,33 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
70
76
|
|
71
77
|
attr_reader :request, :response, :payload
|
72
78
|
|
73
|
-
def initialize(request_method, url)
|
74
|
-
cmd = %W(#{ConsoleUtils.curl_bin} --silent -v
|
75
|
-
|
79
|
+
def initialize(request_method, url, data: nil, headers: nil)
|
80
|
+
cmd = %W(#{ConsoleUtils.curl_bin} --silent -v -g)
|
81
|
+
cmd.push("-X#{request_method}")
|
82
|
+
cmd.push(url)
|
83
|
+
|
84
|
+
cmd.concat(headers.flat_map(&HEADER_JOIN_PROC)) if headers.present?
|
85
|
+
cmd.push("-d", data) if data.present?
|
86
|
+
|
87
|
+
cmd_line = Shellwords.join(cmd)
|
88
|
+
cmd_line << %( --write-out "#{OUT_FORMAT}")
|
89
|
+
|
90
|
+
puts "$ #{cmd_line}" if verbose?
|
76
91
|
|
77
92
|
@response = {}
|
78
93
|
@request = {}
|
79
94
|
@payload = []
|
80
95
|
|
81
|
-
Open3.popen3(
|
96
|
+
Open3.popen3(cmd_line) do |stdin, stdout, stderr, thr|
|
82
97
|
# stdin.close
|
83
|
-
{ stderr: stderr,
|
98
|
+
{ stderr: stderr, stdout: stdout }.each do |key, io|
|
84
99
|
Thread.new do
|
85
|
-
|
86
|
-
|
100
|
+
begin
|
101
|
+
until (line = io.gets).nil? do
|
102
|
+
key == :stderr ? process_stderr(line) : @payload << line
|
103
|
+
end
|
104
|
+
rescue => e
|
105
|
+
warn e
|
87
106
|
end
|
88
107
|
end
|
89
108
|
end
|
@@ -120,7 +139,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
120
139
|
def set_response(line)
|
121
140
|
# warn("Response: #{line}")
|
122
141
|
if !@response.key?(:http_version) && line =~ /^HTTP\/(.+) (\d+?) (.+)$/
|
123
|
-
@response.merge!(http_version: $1, http_code: $2, http_status: $3)
|
142
|
+
@response.merge!(http_version: $1, http_code: $2.to_i, http_status: $3)
|
124
143
|
else
|
125
144
|
header, value = line.split(": ", 2)
|
126
145
|
@response[header] = value
|
@@ -130,6 +149,10 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
130
149
|
def to_h
|
131
150
|
{ response: @response, request: @request }
|
132
151
|
end
|
152
|
+
|
153
|
+
def verbose?
|
154
|
+
!ConsoleUtils.curl_silence
|
155
|
+
end
|
133
156
|
end
|
134
157
|
end
|
135
158
|
end
|
@@ -60,9 +60,9 @@ module ConsoleUtils::RequestUtils #:nodoc:
|
|
60
60
|
protected
|
61
61
|
|
62
62
|
def normalize_args
|
63
|
-
RequestParams.new(*@_args).with_default(default_params)
|
64
|
-
|
65
|
-
|
63
|
+
params = RequestParams.new(*@_args).with_default(default_params)
|
64
|
+
ConsoleUtils.logger.debug { params.to_a }
|
65
|
+
params
|
66
66
|
end
|
67
67
|
|
68
68
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: console_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
196
|
rubyforge_project:
|
197
|
-
rubygems_version: 2.
|
197
|
+
rubygems_version: 2.7.6
|
198
198
|
signing_key:
|
199
199
|
specification_version: 4
|
200
200
|
summary: Groovy tools for Rails Console.
|