console_utils 0.2.2 → 0.3.0

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
2
  SHA1:
3
- metadata.gz: bfc695ee09f05cefe834a9aacb4bc1725f923e0a
4
- data.tar.gz: 6cf231b7ba03c3d14d4b893836938969d6b84f32
3
+ metadata.gz: edf4a90c287e0a2b35baf4b4b30a5a5205279aba
4
+ data.tar.gz: 7ee2964fc56778c17508aaa7f1f754149cc65fd1
5
5
  SHA512:
6
- metadata.gz: 7eea9de0af66cb899fc6f7bd64f196383054852750c14908140b520fd4b433aa95fe0a46d9bd6483759cdc447c28ac1c76b45cadf5defcd61a43174575446f2f
7
- data.tar.gz: c24230444dcdd36451ef6cfd86d085a897bb389f4654e0a7232d4fecea44348a526badf748d93d6d872fb8c16aa526333157bfdcad2bc238909642a9653cd908
6
+ metadata.gz: 2915ca3739fdad30c77ef674c71ace430178c5de0b5be74e49aeadaac7068b847181ae90aed5fc51a6f6f72a6207c7e6222eda7b37ff4bb72e3b45a419df0b24
7
+ data.tar.gz: bd9daf266a95ac31b80aed492ba63d40b949067b884d186f690c1a48ec6610b345ace9199266276f60b06dbc6522e0e7e4e5ebf95fb9879954eaa79cd48441b3
data/README.md CHANGED
@@ -57,7 +57,6 @@ end
57
57
 
58
58
  ### Options:
59
59
 
60
- * `auto_token` - Enable auto-fetch of user's auth token in requests (default: `true`)
61
60
  * `curl_bin` - Binary path to curl (using in remote requests). (default: `"curl"`)
62
61
  * `curl_silence` - Disable print out generated curl command with remote requests. (default: `false`)
63
62
  * `default_token` - A plain string of the default token used to authorize user (default: `nil`)
@@ -71,6 +70,9 @@ end
71
70
  * `user_model_name` - A name of user's model (default: `:User`)
72
71
  * `user_primary_key` - A primary key of user's model (default: `:id`)
73
72
  * `user_token_column` - A column name with a user's token. Using by request tools. (default: `:auth_token`)
73
+ * `request_auto_auth` - Enable the auth automator with the `exap` (default: `true`)
74
+ * `auth_automator` - Specifies a callable object, which will hook requests with credentials. There are two built-in implementations: the default one `ConsoleUtils::RequestUtils::DefaultAuthAutomator` and the `ConsoleUtils::RequestUtils::SimpleTokenAutomator`, which is useful when using the `simple_token_automator` gem.
75
+
74
76
 
75
77
  ## RequestUtils
76
78
 
@@ -43,6 +43,11 @@ end
43
43
  module ConsoleUtils
44
44
  extend ActiveSupport::Autoload
45
45
 
46
+ JSON_FORMATTERS = [
47
+ :default,
48
+ :jq
49
+ ]
50
+
46
51
  MODULES = [
47
52
  :ActiveRecordUtils,
48
53
  :RequestUtils,
@@ -50,76 +55,82 @@ module ConsoleUtils
50
55
  :OtherUtils
51
56
  ]
52
57
 
53
- JSON_FORMATTERS = %i(default jq)
54
-
55
58
  MODULES.each { |mod| autoload mod }
56
59
 
60
+
57
61
  # :section: Configuration
58
62
 
59
- ##
60
63
  # :attr:
61
64
  # An array with disabled modules (default: <tt>[]</tt>)
62
65
  mattr_accessor(:disabled_modules) { [] }
63
- ##
64
- # :attr:
65
- # Enable the auto-fetching of user's auth token in requests
66
- # (default: <tt>true</tt>)
67
- mattr_accessor(:auto_token) { true }
68
- ##
66
+
69
67
  # :attr:
70
68
  # ID of the user which will be used by default in requests
71
69
  # (default: <tt>1</tt>)
72
70
  mattr_accessor(:default_uid) { 1 }
73
- ##
71
+
72
+ # :attr:
73
+ # A plain string of the default token used to authorize user
74
+ # (default: <tt>nil</tt>)
75
+ mattr_accessor(:default_token)
76
+
74
77
  # :attr:
75
78
  # A name of user's model (default: <tt>:User</tt>)
76
79
  mattr_accessor(:user_model_name) { :User }
77
- ##
80
+
78
81
  # :attr:
79
82
  # A primary key of user's model (default: <tt>:id</tt>)
80
83
  mattr_accessor(:user_primary_key) { :id }
81
- ##
84
+
82
85
  # :attr:
83
86
  # A column name with a user's token. Using by request tools.
84
87
  # (default: <tt>:auth_token</tt>)
85
88
  mattr_accessor(:user_token_column) { :auth_token }
86
- ##
89
+
87
90
  # :attr:
88
91
  # A name of the request parameter used to authorize user by a token
89
92
  # (default: <tt>:token</tt>)
90
93
  mattr_accessor(:token_param) { :token }
91
- ##
92
- # :attr:
93
- # A plain string of the default token used to authorize user
94
- # (default: <tt>nil</tt>)
95
- mattr_accessor(:default_token)
96
- ##
94
+
97
95
  # :attr:
98
96
  # JSON formatter used in API request helpers
99
97
  # (<tt>:default</tt> or <tt>:jq</tt>)
100
98
  mattr_accessor(:json_formatter) { :default }
101
- ##
99
+
102
100
  # :attr:
103
101
  # Command for +jq+ json formatter (default: <tt>"jq . -C"</tt>)
104
102
  mattr_accessor(:jq_command) { "jq . -C" }
105
- ##
103
+
106
104
  # :attr:
107
105
  # Binary path to +curl+ (using in remote requests). (default: <tt>"curl"</tt>)
108
106
  mattr_accessor(:curl_bin) { "curl" }
109
- ##
107
+
110
108
  # :attr:
111
109
  # Don't print generated curl command with remote requests.
112
110
  # (default: <tt>false</tt>)
113
111
  mattr_accessor(:curl_silence) { false }
114
- ##
112
+
115
113
  # :attr:
116
114
  # Remote endpoint used in remote API request helpers
117
115
  # (default: <tt>"http://example.com"</tt>)
118
116
  mattr_accessor(:remote_endpoint) { "http://example.com" }
119
- ##
117
+
118
+ # :attr:
119
+ # Output logger (<tt>Logger.new(STDOUT)</tt> by default)
120
+ mattr_accessor(:logger) { Logger.new(STDOUT) }
121
+
120
122
  # :attr:
121
- # Output logger (<tt>Rails.logger</tt> by default)
122
- mattr_accessor :logger
123
+ # Enable the auth automator with the "exap"
124
+ # (default: <tt>true</tt>)
125
+ mattr_accessor(:request_auto_auth) { true }
126
+
127
+ # :attr:
128
+ # Specifies a callable object, which will hook requests with
129
+ # credentials. There are two built-in implementations: the default
130
+ # one <tt>ConsoleUtils::RequestUtils::DefaultAuthAutomator</tt> and
131
+ # the <tt>ConsoleUtils::RequestUtils::SimpleTokenAutomator</tt>,
132
+ # which is useful when using the +simple_token_automator+ gem.
133
+ mattr_accessor(:auth_automator) { ConsoleUtils::RequestUtils::DefaultAuthAutomator }
123
134
 
124
135
 
125
136
  # :section: Class Methods
@@ -183,11 +194,22 @@ module ConsoleUtils
183
194
  end
184
195
 
185
196
  # Setup enabled modules by extending given context
186
- def setup_modules_to(context = nil, &block)
197
+ def setup_modules_to(context = nil)
187
198
  context, block = block, context if !block_given? && context.respond_to?(:call)
188
- context ||= block.call
199
+ context ||= yield if block_given?
200
+
201
+ if context.nil?
202
+ warn "[ConsoleUtils] Trying to setup with empty context"
203
+ return
204
+ end
205
+
206
+ if ENV["CONSOLE_UTILS_DEBUG"] == "1"
207
+ logger.level = Logger::DEBUG
208
+ logger.debug { "Console instance: #{context.inspect} (#{ReplContext.instance.initialized_to})" }
209
+ else
210
+ logger.level = Logger::WARN
211
+ end
189
212
 
190
- puts "Console instance: #{context.inspect}" if ENV["CONSOLE_UTILS_DEBUG"]
191
213
  each_enabled_module { |mod| context.send(:extend, mod) }
192
214
  end
193
215
  end
@@ -7,9 +7,9 @@ module ConsoleUtils
7
7
  #:nodoc: all
8
8
  config.console_utils = ActiveSupport::OrderedOptions.new
9
9
 
10
- initializer 'console_utils.logger' do
11
- ActiveSupport.on_load(:console_utils) { self.logger = ::Rails.logger }
12
- end
10
+ # initializer 'console_utils.logger' do
11
+ # ActiveSupport.on_load(:console_utils) { self.logger = ::Rails.logger }
12
+ # end
13
13
 
14
14
  initializer "console_utils.set_configs" do |app|
15
15
  options = app.config.console_utils
@@ -11,25 +11,40 @@ module ConsoleUtils
11
11
  end
12
12
 
13
13
  def irb!
14
- irb_rails! || ::IRB::ExtendCommandBundle
14
+ init_to(:irb) { irb_rails! || ::IRB::ExtendCommandBundle }
15
15
  end
16
16
 
17
17
  def irb_rails!
18
- ::Rails.application.config.console::ExtendCommandBundle if rails?
18
+ init_to(:rails) { ::Rails.application.config.console::ExtendCommandBundle } if rails?
19
19
  end
20
20
 
21
21
  def pry!
22
- ::TOPLEVEL_BINDING.eval('self') if pry?
22
+ init_to(:pry) { ::TOPLEVEL_BINDING.eval('self') } if pry?
23
23
  end
24
24
 
25
- private
25
+ def initialized_to
26
+ @initialized_to ||= []
27
+ end
28
+
29
+ def initialized?
30
+ initialized_to.size > 0
31
+ end
26
32
 
27
33
  def rails?
28
- defined?(::Rails::Application)
34
+ defined? ::Rails::Application
29
35
  end
30
36
 
31
37
  def pry?
32
- defined?(::Pry)
38
+ defined? ::Pry
39
+ end
40
+
41
+ private
42
+
43
+ def init_to(context)
44
+ unless initialized_to.include?(context)
45
+ initialized_to << context
46
+ yield if block_given?
47
+ end
33
48
  end
34
49
  end
35
50
  end
@@ -18,6 +18,9 @@ module ConsoleUtils #:nodoc:
18
18
  autoload :Jq
19
19
  end
20
20
 
21
+ autoload :DefaultAuthAutomator, "console_utils/request_utils/auth_automators"
22
+ autoload :SimpleTokenAutomator, "console_utils/request_utils/auth_automators"
23
+
21
24
  # :call-seq:
22
25
  # autoken(id)
23
26
  # autoken(:any)
@@ -0,0 +1,18 @@
1
+ module ConsoleUtils::RequestUtils
2
+ class DefaultAuthAutomator
3
+ def self.call(rq)
4
+ rq.params[ConsoleUtils.token_param] ||=
5
+ ConsoleUtils.default_token.presence or ConsoleUtils.auto_token_for(rq.uid)
6
+ end
7
+ end
8
+
9
+ class SimpleTokenAutomator
10
+ def self.call(rq)
11
+ model_key = ConsoleUtils.user_model.model_name.param_key
12
+ header_names = ::SimpleTokenAuthentication.header_names[model_key.to_sym]
13
+ fields = header_names.keys
14
+ user = ConsoleUtils.find_user(rq.uid, scope: ConsoleUtils.user_model.select(:id, *fields))
15
+ header_names.each { |field, name| rq.headers[name] ||= user.public_send(field) }
16
+ end
17
+ end
18
+ end
@@ -31,7 +31,7 @@ module ConsoleUtils::RequestUtils
31
31
  # The default formatter uses standart JSON library to output prettified JSON
32
32
  class Default < Formatter
33
33
  def format(body) #:nodoc:
34
- jj JSON(body)
34
+ JSON.pretty_generate JSON(body), :allow_nan => true, :max_nesting => false
35
35
  rescue JSON::GeneratorError => e
36
36
  warn "Warning: Failed to format a json.", e.message, body
37
37
  body.to_s
@@ -1,55 +1,50 @@
1
1
  module ConsoleUtils::RequestUtils
2
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 then uid_or_params
7
- when headers_or_env.nil? then need_shift!
8
- end
9
- else
10
- need_shift!
3
+ attr_accessor :uid
4
+
5
+ def initialize(uid_or_params = true, params = nil, headers = nil)
6
+ if uid_or_params.is_a? Hash
7
+ headers, params, uid_or_params = [params, uid_or_params, nil]
11
8
  end
12
9
 
13
- params, headers_or_env = [uid_or_params, params] if need_shift?
10
+ @params = params
11
+ @headers = headers
12
+ @uid = auto_auth? && ((uid_or_params.nil? || uid_or_params == true) ? ConsoleUtils.default_uid : uid_or_params)
14
13
 
15
- @params = params.is_a?(Hash) ? params : {}
16
- @headers = headers_or_env.to_h
14
+ ConsoleUtils.logger.debug { "#{uid}, #{params()}, #{headers()}" }
17
15
 
18
- if need_default_token?
19
- use_token ConsoleUtils.default_token
20
- else
21
- @uid = ConsoleUtils.default_uid if need_default_uid?
22
- use_token ConsoleUtils.auto_token_for(@uid) if @uid.present?
23
- end
16
+ auth_automator.(self) if can_auto_auth?
24
17
  end
25
18
 
26
- def to_a
27
- [@params.presence, @headers.presence].tap(&:compact!)
19
+ def params
20
+ @params ||= {}
28
21
  end
29
22
 
30
- def use_token value
31
- @params[ConsoleUtils.token_param] ||= value
23
+ def headers
24
+ @headers ||= {}
25
+ end
26
+
27
+ def to_a
28
+ [params, headers.presence].tap(&:compact!)
32
29
  end
33
30
 
34
31
  def with_default(default_params = nil)
35
- @params.merge!(default_params.to_h)
32
+ params.merge!(default_params.to_h)
36
33
  to_a
37
34
  end
38
35
 
39
- def need_default_uid?
40
- @uid == true && ConsoleUtils.default_token.nil?
36
+ def can_auto_auth?
37
+ auto_auth? && uid && auth_automator.respond_to?(:call)
41
38
  end
42
39
 
43
- def need_default_token?
44
- @uid == true && ConsoleUtils.default_token.present?
45
- end
40
+ private
46
41
 
47
- def need_shift!
48
- @need_shift = true
42
+ def auto_auth?
43
+ ConsoleUtils.request_auto_auth
49
44
  end
50
45
 
51
- def need_shift?
52
- !!@need_shift
46
+ def auth_automator
47
+ ConsoleUtils.auth_automator
53
48
  end
54
49
  end
55
50
  end
@@ -16,13 +16,10 @@ module ConsoleUtils::RequestUtils #:nodoc:
16
16
 
17
17
  def preview(mth = nil)
18
18
  if output = to_s.presence
19
- JSONOutput.formatter.(output) do |formatted|
20
- @formatted = formatted
21
- puts @formatted
22
- show_complete_in!
23
- show_transfered!
24
- yield(self) if block_given?
25
- end
19
+ JSONOutput.formatter.(output)
20
+ show_complete_in!
21
+ show_transfered!
22
+ yield(self) if block_given?
26
23
  else
27
24
  puts NO_RESPONSE
28
25
  end
@@ -30,7 +27,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
30
27
 
31
28
  # Copies to pasteboard
32
29
  def pbcopy(content = nil)
33
- content ||= @formatted
30
+ content ||= JSONOutput::Default.instance.format(to_s)
34
31
  IO.popen('pbcopy', 'w') { |io| io << content.to_s }
35
32
  puts PBCOPY_MESSAGE
36
33
  end
@@ -64,9 +61,9 @@ module ConsoleUtils::RequestUtils #:nodoc:
64
61
  protected
65
62
 
66
63
  def normalize_args
67
- RequestParams.new(*@_args) { |uid| autoken(uid) }.
68
- with_default(default_params).
69
- to_a
64
+ RequestParams.new(*@_args).with_default(default_params).tap do |args|
65
+ ConsoleUtils.logger.debug { args }
66
+ end
70
67
  end
71
68
 
72
69
  private
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
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.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-05 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -145,6 +145,7 @@ files:
145
145
  - lib/console_utils/railtie.rb
146
146
  - lib/console_utils/repl_context.rb
147
147
  - lib/console_utils/request_utils.rb
148
+ - lib/console_utils/request_utils/auth_automators.rb
148
149
  - lib/console_utils/request_utils/exap.rb
149
150
  - lib/console_utils/request_utils/json_output.rb
150
151
  - lib/console_utils/request_utils/remo.rb