console_utils 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ec5fe50875a44222ed008d871f20af1252723f7
4
- data.tar.gz: 49c093ec238e4ab69757bf5068af54ac4cf78ac1
3
+ metadata.gz: 1cbb55a155eb323c890fe24163466ab6b1c081a1
4
+ data.tar.gz: 51077eec5dbdc7eb7949501216f5a2f934802011
5
5
  SHA512:
6
- metadata.gz: 2d5a262aa068990424a189760e7d2a750575ecc6ab6f88f7c730f0f18c0fd6dbfc7c8cffda2f27e6092264c307dd16aa38fa1ef59815d3853122da5120259609
7
- data.tar.gz: b2c35b09c9a3caf875c586b3d47bc5d8acd9764b5d090bb710c4d3545e9e128ae5f91e6013c149966d38e120cec5b463550939842f3510824878491f3c79abb4
6
+ metadata.gz: 6180d6fa56625afc702a252e3841a314557fdcb38e4cd3245e1c416097bea76b3e1c8d53f1a85b526102d70fc74da7a5da4908735f382f0b36131a2b8176b52e
7
+ data.tar.gz: 7b1448456a5999cab112ecc799e42c9c4d6368412a17fd8516d5726860068c440e42bee8d3deed122f82ebd7c5017ea3f157ef3feaf97cfe2f0212cfdd2d275b
data/lib/console_utils.rb CHANGED
@@ -2,6 +2,7 @@ require 'active_support'
2
2
  require 'active_support/rails'
3
3
  require 'term/ansicolor'
4
4
  require 'console_utils/core_ext/array_to_proc'
5
+ require 'console_utils/core_ext/local_values'
5
6
  require 'console_utils/repl_context'
6
7
  require 'console_utils/version'
7
8
 
@@ -0,0 +1,8 @@
1
+ unless Binding.method_defined?(:local_values)
2
+ class Binding
3
+ # Returns a hash with symbol keys that maps local variable names to their corresponding values.
4
+ def local_values
5
+ Hash[local_variables.map { |name| [name.to_s, local_variable_get(name)] }]
6
+ end
7
+ end
8
+ end
@@ -8,6 +8,7 @@ module ConsoleUtils #:nodoc:
8
8
  module RequestUtils
9
9
  extend ActiveSupport::Autoload
10
10
 
11
+ autoload :RequestParams
11
12
  autoload :Requester
12
13
  autoload :Exap
13
14
  autoload :Remo
@@ -0,0 +1,49 @@
1
+ module ConsoleUtils::RequestUtils
2
+ class RequestParams
3
+ def initialize(uid_or_params = true, params = nil, headers_or_env = nil)
4
+ if ConsoleUtils.auto_token
5
+ @uid = case uid_or_params
6
+ when Numeric, true, false, nil
7
+ uid_or_params
8
+ when headers_or_env.nil?
9
+ need_shift!
10
+ end
11
+ else
12
+ need_shift!
13
+ end
14
+
15
+ params, headers_or_env = [uid_or_params, params] if need_shift?
16
+
17
+ @params = params || {}
18
+ @headers_or_env = headers_or_env || {}
19
+
20
+ @uid = ConsoleUtils.default_uid if need_default_uid?
21
+
22
+ if @uid.present?
23
+ @params[ConsoleUtils.token_param] ||= yield(@uid) if block_given?
24
+ printf(Requester::AUTOAUTH_FORMAT, @uid, to_a)
25
+ end
26
+ end
27
+
28
+ def to_a
29
+ [@params.presence, @headers_or_env.presence].tap(&:compact!)
30
+ end
31
+
32
+ def with_default(default_params = nil)
33
+ @params.merge!(default_params.is_a?(Hash) ? default_params : {})
34
+ to_a
35
+ end
36
+
37
+ def need_default_uid?
38
+ @uid == true
39
+ end
40
+
41
+ def need_shift!
42
+ @need_shift = true
43
+ end
44
+
45
+ def need_shift?
46
+ !!@need_shift
47
+ end
48
+ end
49
+ end
@@ -12,6 +12,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
12
12
  COMPLETE_IN = Term::ANSIColor.green("Complete in %s").freeze
13
13
  TRANSFERED = Term::ANSIColor.cyan("Transfered: %s").freeze
14
14
 
15
+ class_attribute :default_params
15
16
  attr_reader :url
16
17
 
17
18
  def preview(mth = nil)
@@ -54,16 +55,9 @@ module ConsoleUtils::RequestUtils #:nodoc:
54
55
  protected
55
56
 
56
57
  def normalize_args
57
- if ConsoleUtils.auto_token
58
- uid = (@_args[0].is_a?(Hash) || @_args.empty?) ? ConsoleUtils.default_uid : @_args.shift
59
- if uid.present?
60
- printf(AUTOAUTH_FORMAT, uid, @_args)
61
- opts = @_args.extract_options!
62
- @_args.unshift(opts.tap { |x| x[ConsoleUtils.token_param] ||= __getobj__.autoken(uid) })
63
- end
64
- end
65
-
66
- @_args
58
+ RequestParams.new(*@_args) { |uid| autoken(uid) }.
59
+ with_default(default_params).
60
+ to_a
67
61
  end
68
62
 
69
63
  # Copies to pasteboard
@@ -87,7 +81,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
87
81
  @_size = nil
88
82
  end
89
83
 
90
- private_constant :REQUEST_METHODS, :AUTOAUTH_FORMAT, :PBCOPY_MESSAGE,
84
+ private_constant :REQUEST_METHODS, :PBCOPY_MESSAGE,
91
85
  :NO_RESPONSE, :COMPLETE_IN, :TRANSFERED,
92
86
  :INFO_FORMAT
93
87
  end
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
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.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -139,6 +139,7 @@ files:
139
139
  - lib/console_utils/bench_utils.rb
140
140
  - lib/console_utils/bench_utils/chips.rb
141
141
  - lib/console_utils/core_ext/array_to_proc.rb
142
+ - lib/console_utils/core_ext/local_values.rb
142
143
  - lib/console_utils/other_utils.rb
143
144
  - lib/console_utils/pry.rb
144
145
  - lib/console_utils/railtie.rb
@@ -147,6 +148,7 @@ files:
147
148
  - lib/console_utils/request_utils/exap.rb
148
149
  - lib/console_utils/request_utils/json_output.rb
149
150
  - lib/console_utils/request_utils/remo.rb
151
+ - lib/console_utils/request_utils/request_params.rb
150
152
  - lib/console_utils/request_utils/requester.rb
151
153
  - lib/console_utils/version.rb
152
154
  homepage: https://github.com/estum/console_utils