console_utils 0.1.6 → 0.2.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: 1cbb55a155eb323c890fe24163466ab6b1c081a1
4
- data.tar.gz: 51077eec5dbdc7eb7949501216f5a2f934802011
3
+ metadata.gz: 885596f011ab0c949a352aa22abe4e9c85d93ac2
4
+ data.tar.gz: bd7daaf39ea1b539f5bae93ac4a0751580ca08a8
5
5
  SHA512:
6
- metadata.gz: 6180d6fa56625afc702a252e3841a314557fdcb38e4cd3245e1c416097bea76b3e1c8d53f1a85b526102d70fc74da7a5da4908735f382f0b36131a2b8176b52e
7
- data.tar.gz: 7b1448456a5999cab112ecc799e42c9c4d6368412a17fd8516d5726860068c440e42bee8d3deed122f82ebd7c5017ea3f157ef3feaf97cfe2f0212cfdd2d275b
6
+ metadata.gz: bee127748adcea9da28b3372869aa3770dc14048b54b31454ea6e78d2945c9893375c8391bda662c9b8c8919b1b741e6613c1517b49c9e2fb2dd5b7735757b00
7
+ data.tar.gz: 6fab7992b4534b45d5058a54db989faee505a47b9839cbd7eb243196f4b94fa53a00edbf6a01e26cf2a437dd41205792d168a1009d5f58f00514c606d71bb94d
data/README.md CHANGED
@@ -60,10 +60,11 @@ end
60
60
  * `auto_token` - Enable auto-fetch of user's auth token in requests (default: `true`)
61
61
  * `curl_bin` - Binary path to curl (using in remote requests). (default: `"curl"`)
62
62
  * `curl_silence` - Disable print out generated curl command with remote requests. (default: `false`)
63
+ * `default_token` - A plain string of the default token used to authorize user (default: `nil`)
63
64
  * `default_uid` - ID of the user which will be used by default in requests (default: `1`)
64
65
  * `disabled_modules` - An array of disabled modules' names (default: `[]`)
65
66
  * `jq_command` - Command for jq json formatter (default: `"jq . -C"`)
66
- * `json_formatter` - JSON formatter used in API request helpers (:default or :jq)
67
+ * `json_formatter` - JSON formatter used in API request helpers (`:default` or `:jq`)
67
68
  * `logger` - Output logger (Rails.logger by default)
68
69
  * `remote_endpoint` - Remote endpoint used in remote API request helpers (default: `"http://example.com"`)
69
70
  * `token_param` - A name of the request parameter used to authorize user by a token (default: `:token`)
@@ -90,6 +90,11 @@ module ConsoleUtils
90
90
  mattr_accessor(:token_param) { :token }
91
91
  ##
92
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
+ ##
97
+ # :attr:
93
98
  # JSON formatter used in API request helpers
94
99
  # (<tt>:default</tt> or <tt>:jq</tt>)
95
100
  mattr_accessor(:json_formatter) { :default }
@@ -120,39 +125,61 @@ module ConsoleUtils
120
125
  # :section: Class Methods
121
126
 
122
127
  class << self
123
- def config
124
- self
125
- end
128
+ alias_method :config, :itself
126
129
 
127
130
  # :method: self.configure
128
131
  def configure
129
132
  yield(config)
130
133
  end
131
134
 
135
+ def enabled_modules
136
+ ConsoleUtils::MODULES - disabled_modules
137
+ end
138
+
139
+ # Yields each enabled module with a given block
140
+ def each_enabled_module
141
+ enabled_modules.each { |mod| yield const_get(mod) }
142
+ end
143
+
132
144
  # Returns User's class set in the <tt>:user_class_name</tt>
133
145
  def user_model
134
146
  Object.const_get(user_model_name)
135
147
  end
136
148
  alias_method :user_class, :user_model
137
149
 
150
+ def user_scope(scope = nil)
151
+ case scope
152
+ when nil then user_model
153
+ when Symbol then user_model.public_send(scope)
154
+ else user_model.all.merge(scope)
155
+ end
156
+ end
157
+
138
158
  # Finds +user_model+ by +user_primary_key+.
139
159
  # If the first argument is <tt>:any</tt>, gets a random user.
140
160
  def find_user(id, scope: nil)
141
- case id
142
- when :any
143
- (scope || user_model).anyone
161
+ if id == :any
162
+ user_scope(scope).anyone.tap do |u|
163
+ puts "random user #{user_primary_key}: #{u.public_send(user_primary_key)}"
164
+ end
144
165
  else
145
- (scope || user_model).where(user_primary_key => id).first!
166
+ user_scope(scope).where(user_primary_key => id).first!
146
167
  end
147
168
  end
148
169
 
149
- def enabled_modules
150
- ConsoleUtils::MODULES - disabled_modules
170
+ def auto_token_for(id)
171
+ user = find_user(id, scope: user_model.select([:id, user_token_column]))
172
+ user.public_send(user_token_column)
151
173
  end
152
174
 
153
- # Yields each enabled module with a given block
154
- def each_enabled_module
155
- enabled_modules.each { |mod| yield const_get(mod) }
175
+ # Setup enabled modules for IRB context
176
+ def irb!
177
+ setup_modules_to(ReplContext.instance.irb!)
178
+ end
179
+
180
+ # Setup enabled modules for Pry context
181
+ def pry!
182
+ setup_modules_to(ReplContext.instance.pry!)
156
183
  end
157
184
 
158
185
  # Setup enabled modules by extending given context
@@ -163,14 +190,6 @@ module ConsoleUtils
163
190
  puts "Console instance: #{context.inspect}" if ENV["CONSOLE_UTILS_DEBUG"]
164
191
  each_enabled_module { |mod| context.send(:extend, mod) }
165
192
  end
166
-
167
- def irb!
168
- setup_modules_to(ReplContext.instance.irb!)
169
- end
170
-
171
- def pry!
172
- setup_modules_to(ReplContext.instance.pry!)
173
- end
174
193
  end
175
194
 
176
195
  ActiveSupport.run_load_hooks(:console_utils, self)
@@ -24,9 +24,7 @@ module ConsoleUtils #:nodoc:
24
24
  #
25
25
  # Returns user's token by primary key. Use <tt>:any</tt> to get random user.
26
26
  def autoken(id)
27
- user = id == :any ? user_scope.anyone : ConsoleUtils.find_user(id, scope: user_scope)
28
- puts "User #{user.public_send(ConsoleUtils.user_primary_key)}" if id == :any
29
- user.public_send(ConsoleUtils.user_token_column)
27
+ ConsoleUtils.auto_token_for(id)
30
28
  end
31
29
 
32
30
  # :call-seq:
@@ -73,11 +71,5 @@ module ConsoleUtils #:nodoc:
73
71
  def remo
74
72
  Remo.new(self)
75
73
  end
76
-
77
- private
78
-
79
- def user_scope
80
- ConsoleUtils.user_model.select([:id, :auth_token])
81
- end
82
74
  end
83
75
  end
@@ -3,10 +3,8 @@ module ConsoleUtils::RequestUtils
3
3
  def initialize(uid_or_params = true, params = nil, headers_or_env = nil)
4
4
  if ConsoleUtils.auto_token
5
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!
6
+ when Numeric, true, false, nil then uid_or_params
7
+ when headers_or_env.nil? then need_shift!
10
8
  end
11
9
  else
12
10
  need_shift!
@@ -14,28 +12,36 @@ module ConsoleUtils::RequestUtils
14
12
 
15
13
  params, headers_or_env = [uid_or_params, params] if need_shift?
16
14
 
17
- @params = params || {}
18
- @headers_or_env = headers_or_env || {}
15
+ @params = params.is_a?(Hash) ? params : {}
16
+ @headers = headers_or_env.to_h
19
17
 
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)
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?
25
23
  end
26
24
  end
27
25
 
28
26
  def to_a
29
- [@params.presence, @headers_or_env.presence].tap(&:compact!)
27
+ [@params.presence, @headers.presence].tap(&:compact!)
28
+ end
29
+
30
+ def use_token value
31
+ @params[ConsoleUtils.token_param] ||= value
30
32
  end
31
33
 
32
34
  def with_default(default_params = nil)
33
- @params.merge!(default_params.is_a?(Hash) ? default_params : {})
35
+ @params.merge!(default_params.to_h)
34
36
  to_a
35
37
  end
36
38
 
37
39
  def need_default_uid?
38
- @uid == true
40
+ @uid == true && ConsoleUtils.default_token.nil?
41
+ end
42
+
43
+ def need_default_token?
44
+ @uid == true && ConsoleUtils.default_token.present?
39
45
  end
40
46
 
41
47
  def need_shift!
@@ -6,7 +6,6 @@ module ConsoleUtils::RequestUtils #:nodoc:
6
6
  REQUEST_METHODS = %i(get post put delete patch).freeze
7
7
  INFO_HASH_FIELDS = %i(url size time human_size human_time).freeze
8
8
  INFO_FORMAT = "%#-.50{url} | %#10{human_size} | %#10{human_time}\n".freeze
9
- AUTOAUTH_FORMAT = %('ID: %s' %p\n).freeze
10
9
  NO_RESPONSE = Term::ANSIColor.red(" \u27A7 Empty response's body.").freeze
11
10
  PBCOPY_MESSAGE = Term::ANSIColor.green(" \u27A4 Response body copied to pasteboard\n").freeze
12
11
  COMPLETE_IN = Term::ANSIColor.green("Complete in %s").freeze
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.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.1.6
4
+ version: 0.2.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-05-23 00:00:00.000000000 Z
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails