console_utils 0.1.2 → 0.1.3

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: ad4d5f9bd6d4cf1de1823c6d9324cb63e0bdde7a
4
- data.tar.gz: 7e26b5ed138029ee1cfede1739e626635d7e963f
3
+ metadata.gz: f8c1497e6b3181baa95fe33d6112b806e0724404
4
+ data.tar.gz: a815bff00b9fbfeb15e6d2ac6c6d78af13776098
5
5
  SHA512:
6
- metadata.gz: d51785856f9560a3da0712885395c273fa9e234b3bd17ef7ff88e68fe768924b9178faa2e813c9f7398dc73cca1e4859ab98de79862168cead17a0e1ee20ccb9
7
- data.tar.gz: 0db4069026917e568d057c188d9537a7880c9728596b9a42f3b5864b6309fc72717b6f53ba9e758521af810eff741832e3cb2667e674542c28da13cc2becd61c
6
+ metadata.gz: 082d3d022dccbd045c3a0312cc2c34a684076eff3d5119c7b70579361298d317efe29f3e2f4c71e1187ebd7209584a367dbd759177c55b97554594c6302358d3
7
+ data.tar.gz: 68470ed097f30a499a6ecdc71af8f4d162d00f617e29f18caeaeb158f47e5e1ca72f3c17a7e27c96d4a5ac090d64b1a969be8bd19279a419cca3aa4ad2df22bd
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
@@ -48,10 +48,10 @@ module ConsoleUtils
48
48
  :OtherUtils
49
49
  ]
50
50
 
51
- MODULES.each { |mod| autoload mod }
52
-
53
51
  JSON_FORMATTERS = %i(default jq)
54
52
 
53
+ MODULES.each { |mod| autoload mod }
54
+
55
55
  # :section: Configuration
56
56
 
57
57
  ##
@@ -134,9 +134,15 @@ module ConsoleUtils
134
134
  end
135
135
  alias_method :user_class, :user_model
136
136
 
137
- # Finds +user_model+ by +user_primary_key+
137
+ # Finds +user_model+ by +user_primary_key+.
138
+ # If the first argument is <tt>:any</tt>, gets a random user.
138
139
  def find_user(id, scope: nil)
139
- (scope || user_model).where(user_primary_key => id).first!
140
+ case id
141
+ when :any
142
+ (scope || user_model).anyone
143
+ else
144
+ (scope || user_model).where(user_primary_key => id).first!
145
+ end
140
146
  end
141
147
 
142
148
  def enabled_modules
@@ -12,6 +12,11 @@ module ConsoleUtils #:nodoc:
12
12
  autoload :Exap
13
13
  autoload :Remo
14
14
 
15
+ autoload :JSONOutput do
16
+ autoload :Default
17
+ autoload :Jq
18
+ end
19
+
15
20
  # :call-seq:
16
21
  # autoken(id)
17
22
  # autoken(:any)
@@ -23,7 +23,6 @@ module ConsoleUtils::RequestUtils #:nodoc:
23
23
 
24
24
  def resp_wrap(meth, url, *args)
25
25
  @url, @_args = url, args
26
- p args
27
26
  app.send(meth, url, *normalize_args)
28
27
  self
29
28
  end
@@ -0,0 +1,57 @@
1
+ module ConsoleUtils::RequestUtils
2
+ module JSONOutput
3
+ FORMATTERS = {}
4
+
5
+ # The abstract singleton class for a prettified JSON formatting.
6
+ class Formatter
7
+ include Singleton
8
+
9
+ # Prints formatted JSON to stdout.
10
+ def call(body)
11
+ puts format(body)
12
+ end
13
+
14
+ # Formats a given JSON string
15
+ def format(body)
16
+ raise NotImplementedError
17
+ end
18
+
19
+ def self.inherited(sub) #:nodoc:
20
+ super
21
+ key = sub.name.demodulize.underscore.to_sym
22
+ FORMATTERS[key] = sub
23
+ end
24
+ end
25
+
26
+ # The default formatter uses standart JSON library to output prettified JSON
27
+ class Default < Formatter
28
+ def format(body) #:nodoc:
29
+ jj JSON(body)
30
+ rescue JSON::GeneratorError => e
31
+ warn "Warning: Failed to format a json.", e.message, body
32
+ body.to_s
33
+ end
34
+ end
35
+
36
+ # The jq formatter uses the external {jq}[http://stedolan.github.com/jq] utility.
37
+ class Jq < Formatter
38
+ delegate :jq_command, :to => :ConsoleUtils
39
+
40
+ def format(body) #:nodoc:
41
+ IO.popen(jq_command, 'r+') { |io| (io << body).tap(&:close_write).read }
42
+ end
43
+ end
44
+
45
+ class << self
46
+ delegate :json_formatter, :to => :ConsoleUtils
47
+
48
+ # Get current formatter object
49
+ def formatter
50
+ FORMATTERS[json_formatter].instance
51
+ end
52
+ end
53
+
54
+ private_constant :Formatter
55
+ private_class_method :json_formatter
56
+ end
57
+ end
@@ -3,78 +3,59 @@ require 'active_support/core_ext/numeric'
3
3
 
4
4
  module ConsoleUtils::RequestUtils #:nodoc:
5
5
  class Requester < SimpleDelegator
6
- REQUEST_METHODS = %i(get post put delete patch).freeze
6
+ REQUEST_METHODS = %i(get post put delete patch).freeze
7
+ INFO_HASH_FIELDS = %i(url size time human_size human_time).freeze
8
+ INFO_FORMAT = "%#-.50{url} | %#10{human_size} | %#10{human_time}\n".freeze
9
+ AUTOAUTH_FORMAT = %('ID: %s' %p\n).freeze
10
+ NO_RESPONSE = Term::ANSIColor.red(" \u27A7 Empty response's body.").freeze
11
+ PBCOPY_MESSAGE = Term::ANSIColor.green(" \u27A4 Response body copied to pasteboard\n").freeze
12
+ COMPLETE_IN = Term::ANSIColor.green("Complete in %s").freeze
13
+ TRANSFERED = Term::ANSIColor.cyan("Transfered: %s").freeze
7
14
 
8
15
  attr_reader :url
9
16
 
10
- def to_h
11
- JSON.parse(to_s)
12
- end
13
-
14
- def to_body
15
- JSON.pretty_generate(to_h)
16
- rescue JSON::GeneratorError
17
- to_s
18
- end
19
-
20
- NO_RESPONSE = Term::ANSIColor.red(" \u27A7 Empty response's body.").freeze
21
-
22
17
  def preview(mth = nil)
23
18
  if output = to_s.presence
24
- case ConsoleUtils.json_formatter
25
- when :default then puts to_body
26
- when :jq then puts jq(output)
27
- end
28
-
19
+ JSONOutput.formatter.(output)
29
20
  show_complete_in!
30
21
  show_transfered!
31
-
32
22
  yield(self) if block_given?
33
23
  else
34
24
  puts NO_RESPONSE
35
25
  end
36
26
  end
37
27
 
38
- INFO_FORMAT = "%#-.50{url} | %#10{human_size} | %#10{human_time}\n".freeze
39
-
40
28
  def print_info
41
29
  tap { printf(INFO_FORMAT, to_info_hash) }
42
30
  end
43
31
 
44
- INFO_HASH_FIELDS = %i(url size time human_size human_time)
32
+ def size
33
+ @_size.bytes
34
+ end
45
35
 
46
- def to_info_hash
47
- INFO_HASH_FIELDS.zip(INFO_HASH_FIELDS.map(&method(:public_send))).to_h
36
+ def time
37
+ @_time.in_milliseconds
48
38
  end
49
39
 
40
+ alias_method :human_size,
50
41
  def size_downloaded
51
42
  size.to_s(:human_size)
52
43
  end
53
44
 
54
- alias_method :human_size, :size_downloaded
55
-
45
+ alias_method :human_time,
56
46
  def time_ms
57
47
  time.to_s(:human, units: { unit: 'ms' })
58
48
  end
59
49
 
60
- alias_method :human_time, :time_ms
61
-
62
- def size
63
- @_size.bytes
64
- end
65
-
66
- def time
67
- @_time.in_milliseconds
50
+ def to_info_hash
51
+ INFO_HASH_FIELDS.zip(INFO_HASH_FIELDS.map(&method(:public_send))).to_h
68
52
  end
69
53
 
70
54
  protected
71
55
 
72
- AUTOAUTH_FORMAT = %('ID: %s' %p\n).freeze
73
-
74
56
  def normalize_args
75
57
  if ConsoleUtils.auto_token
76
58
  uid = (@_args[0].is_a?(Hash) || @_args.empty?) ? ConsoleUtils.default_uid : @_args.shift
77
-
78
59
  if uid.present?
79
60
  printf(AUTOAUTH_FORMAT, uid, @_args)
80
61
  opts = @_args.extract_options!
@@ -85,8 +66,6 @@ module ConsoleUtils::RequestUtils #:nodoc:
85
66
  @_args
86
67
  end
87
68
 
88
- protected
89
-
90
69
  # Copies to pasteboard
91
70
  def pbcopy(content = nil)
92
71
  content ||= to_body
@@ -96,18 +75,12 @@ module ConsoleUtils::RequestUtils #:nodoc:
96
75
 
97
76
  private
98
77
 
99
- PBCOPY_MESSAGE = Term::ANSIColor.green(" \u27A4 Response body copied to pasteboard\n").freeze
100
-
101
- COMPLETE_IN = Term::ANSIColor.green("Complete in %s").freeze
102
-
103
78
  def show_complete_in!(reset = true)
104
79
  return if @_time.nil?
105
80
  puts "=> #{COMPLETE_IN % [time_ms]}"
106
81
  @_time = nil
107
82
  end
108
83
 
109
- TRANSFERED = Term::ANSIColor.cyan("Transfered: %s").freeze
110
-
111
84
  def show_transfered!(reset = true)
112
85
  return if @_size.nil?
113
86
  puts "=> #{TRANSFERED % [size_downloaded]}"
@@ -117,11 +90,5 @@ module ConsoleUtils::RequestUtils #:nodoc:
117
90
  private_constant :REQUEST_METHODS, :AUTOAUTH_FORMAT, :PBCOPY_MESSAGE,
118
91
  :NO_RESPONSE, :COMPLETE_IN, :TRANSFERED,
119
92
  :INFO_FORMAT
120
- # -
121
-
122
- # Pretty formats json
123
- def jq(raw)
124
- IO.popen(ConsoleUtils.jq_command, 'r+') { |io| (io << raw).tap(&:close_write).read }
125
- end
126
93
  end
127
94
  end
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.1.2"
3
- end
2
+ VERSION = "0.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.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -124,6 +124,7 @@ files:
124
124
  - ".gitignore"
125
125
  - ".rspec"
126
126
  - ".travis.yml"
127
+ - CODE_OF_CONDUCT.md
127
128
  - Gemfile
128
129
  - LICENSE.txt
129
130
  - README.md
@@ -142,6 +143,7 @@ files:
142
143
  - lib/console_utils/railtie.rb
143
144
  - lib/console_utils/request_utils.rb
144
145
  - lib/console_utils/request_utils/exap.rb
146
+ - lib/console_utils/request_utils/json_output.rb
145
147
  - lib/console_utils/request_utils/remo.rb
146
148
  - lib/console_utils/request_utils/requester.rb
147
149
  - lib/console_utils/version.rb