console_utils 0.7.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cf9d61e130700afa5abe8e93c11b6fada2eb6cb4
4
- data.tar.gz: e406176372e604f376c6806cc5d48c0141b46241
2
+ SHA256:
3
+ metadata.gz: 1b476b929197a0b2c0c282737682ba40f6fe8c02d14858f253f569a47ea20c52
4
+ data.tar.gz: 77abfe7679f71f9f7aae8089ec2a660675d86c3ebe139ed9005b6ce4880473b0
5
5
  SHA512:
6
- metadata.gz: ab3c2fd6ebbed1a16715e688e28ca54179fd091e3ca49a93031b3cf6b612f7e59771c8ff9dcd01fe8be898677328d4af1b839fc871f554773b4be2390149ffa9
7
- data.tar.gz: 21b6aa7cc8c3bf5fe6aa5a5d1952256ee7153915ad595b80170639d909617fea473f05fa27252bcda6903d5d9cbfdcf3d3926c5fefc0edaefd625527d0cda211
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(:include, RandomRecord::FinderMethods)
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
- model.type_for_attribute('id').send(:cast_value, connection.select_value(select(:id).random.limit(1)))
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
 
@@ -9,7 +9,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
9
9
  def #{request_method}(url, *args)
10
10
  @url = url
11
11
  @_args = args
12
- app.#{request_method}(@url, *normalize_args)
12
+ app.request_via_redirect(:#{request_method}, @url, *normalize_args.to_a)
13
13
  self
14
14
  end
15
15
  RUBY
@@ -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
- Curl.(request_method, url) do |result, payload|
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(*args)
58
- options = args.extract_options!
59
- URI.join(ConsoleUtils.remote_endpoint, *args).
60
- tap { |uri| uri.query = options.to_query }.to_s
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 = "\n%{http_code}\n%{time_total}\n%{size_download}".freeze
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 --write-out #{OUT_FORMAT} -X #{request_method} #{url})
75
- puts "# #{cmd.shelljoin.inspect}" unless ConsoleUtils.curl_silence
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(Shellwords.join(cmd)) do |stdin, stdout, stderr, thr|
96
+ Open3.popen3(cmd_line) do |stdin, stdout, stderr, thr|
82
97
  # stdin.close
83
- { stderr: stderr, stdin: stdout }.each do |key, io|
98
+ { stderr: stderr, stdout: stdout }.each do |key, io|
84
99
  Thread.new do
85
- while line = io.gets
86
- key == :stderr ? process_stderr(line) : @payload << line
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
@@ -39,7 +39,7 @@ module ConsoleUtils::RequestUtils
39
39
  headers.merge!(default_headers.to_h)
40
40
  end
41
41
 
42
- to_a
42
+ self
43
43
  end
44
44
 
45
45
  def can_auto_auth?
@@ -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).tap do |args|
64
- ConsoleUtils.logger.debug { args }
65
- end
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
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.1"
3
3
  end
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.7.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: 2016-08-14 00:00:00.000000000 Z
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.6.6
197
+ rubygems_version: 2.7.6
198
198
  signing_key:
199
199
  specification_version: 4
200
200
  summary: Groovy tools for Rails Console.