mudbug 0.5.1.3 → 0.5.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +8 -8
  2. data/MANIFEST.txt +1 -0
  3. data/VERSION +1 -1
  4. data/bin/mb +79 -56
  5. data/lib/mb_config.rb +55 -0
  6. metadata +2 -1
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDg5MjE2ZjY2M2ZjNDg0YTYxNGI3MzA3YWVhY2M5ZjMyYzk3OTVhZA==
4
+ ODU1YmNjZGVkYjY0MDQ3MmYyMmRkOTBiMDBmNmM2ZDllOGY4ODg3Yg==
5
5
  data.tar.gz: !binary |-
6
- ZDhiNWRkYzlmZTBlNTc0NzEyMWY2ZmM3ODFiZjRiZThjZjUwYjgwMw==
6
+ N2RhYWNmNWE0ZjU5NTRmZWIyZTA5MTQ4OGQzYjQwMTdlMDdiMDIyNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- N2M1ZTFhNjUxOGEwZDNhODBkMDA5ZmY4ZGVmMGIzMTkyMDBlNDZiMDlhYzhj
10
- NTYxMDk0ZGFjY2VjODc0ZmYwNWMxMTZmYWVkNDc0ODYyNjg2MzgzMWU3MzZk
11
- MDA2NGQxMDk3MThkNzNhMWYyYjZlMzY5ZjAyZDdjOGQ5MWFmMjk=
9
+ YjdlYmQ5YmYyNzNhZmVlNTQ1NmU5YWQwZmMzMTBjNWM5OGNhZDg5MGNiNDg2
10
+ NmJlNGUwYmNkNThhMWZhNjNmMTIwNWIwODViOThhNGI1OGQ1ODNlZmI5MDFl
11
+ NjM1NjM4ZDJlNzE0NmM2YTM1ZTc0MDFjOTIxN2IxM2Y3OGVkNGE=
12
12
  data.tar.gz: !binary |-
13
- YTA1YjE4Y2I0ODQ0YWY1YmM3YTJlYmQ1ZGQ0ZGMzNWJmZTRkMmE1ZTg2ZjEz
14
- NWRmZjQyOTFhNjRkNjY3MjlhMzkyNDk0ZDU1MTRiOGQ5MWI1YWFhMTVjMjcz
15
- NmU2ODgyN2Q2N2QzM2QwNWFjNDc2MDcxMTY5ZmY0NDUyYTY1MzE=
13
+ MWZlYmYyZWRhNDdlMzQ1ZTQ2ODhlNWJmMzBkMmUzNjNjY2UzNGNiYTgyNGY1
14
+ NTZlNWNjZDVjZThlMTBiYjRlOGM1ZWNkMmQwMzI3YTBjMjdiZTUzZWExMjA3
15
+ YjUwNjA4YTk5NzFiYWIxYjFlMzFkMTZkMDU0NjRkOWY4NzIxNzE=
@@ -5,5 +5,6 @@ README.md
5
5
  LICENSE.txt
6
6
  rakefile.rb
7
7
  lib/mudbug.rb
8
+ lib/mb_config.rb
8
9
  test/mudbug.rb
9
10
  examples/accepts_and_methods.rb
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.1.3
1
+ 0.5.2.1
data/bin/mb CHANGED
@@ -4,18 +4,20 @@ def usage(msg = nil)
4
4
  puts "ERROR: #{msg}" if msg
5
5
  puts <<USAGE
6
6
  USAGE:
7
- mb host|method|path|payload [VAL] - Set or display the current config item
8
- mb accept [ACC1] [ACC2] [etc] - Set or display the accept config item
9
- mb config - Display the current config
10
- mb [get|delete] [PATH] - GET or DELETE PATH
11
- mb [post|put] [PATH] [PAYLOAD] - POST or PUT PAYLOAD to PATH
7
+ mb [get|delete] [PATH] - GET or DELETE PATH
8
+ mb [post|put] [PATH] - - POST or PUT payload (STDIN) to PATH
9
+
10
+ mb host|method|path [VAL] - Set or display current config item
11
+ mb accept [ACC1] [ACC2] [etc] - Set or display accept types
12
+ mb payload [-] - Set (from STDIN) or display payload config
13
+ mb config - Display current config
12
14
 
13
15
  * A host must be configured for mb to operate
14
16
  * method, path, and payload, if configured, can be omitted as CLI arguments
15
17
  * accept is optional; valid values include json, html, text
16
18
  USAGE
17
19
  puts
18
- puts "CONFIG: #{Mudbug.load.inspect}" if Mudbug.fresh?
20
+ puts "CONFIG: #{MbConfig.dump}"
19
21
 
20
22
  exit 1
21
23
  end
@@ -25,33 +27,11 @@ def conclude(msg)
25
27
  exit 0
26
28
  end
27
29
 
28
- require 'mudbug'
29
-
30
- CFG_FILE = File.expand_path '~/.mudbug'
31
- FILE_EXPIRY = 3600 # seconds
32
-
33
- class Mudbug
34
- def self.save config
35
- raise "unable to write to #{CFG_FILE}" unless File.writable? CFG_FILE
36
- File.open(CFG_FILE, 'w') { |f| f.write config.to_json }
37
- end
38
-
39
- def self.load
40
- if self.fresh?
41
- File.open(CFG_FILE, 'r') { |f| JSON.parse(f.read) rescue nil }
42
- end
43
- end
44
-
45
- def self.fresh?
46
- File.exists?(CFG_FILE) and File.readable?(CFG_FILE) and
47
- Time.now - File.mtime(CFG_FILE) < FILE_EXPIRY
48
- end
49
- end
50
-
51
- CFG = Mudbug.load || {}
30
+ require 'mb_config'
52
31
 
53
32
  #
54
- # Stage 1: handle commands that manipulate CFG / CFG_FILE
33
+ # Config section: handle commands that manipulate MbConfig
34
+ # Should exit in all cases with conclude()
55
35
  #
56
36
 
57
37
  # don't consume ARGV here
@@ -61,23 +41,45 @@ if cfg_cmd
61
41
  cfg_cmd = cfg_cmd.downcase
62
42
 
63
43
  case cfg_cmd
64
- when 'host', 'method', 'path', 'payload'
44
+ when 'host', 'method', 'path'
65
45
  ARGV.shift # confirmed cfg_cmd, go ahead and shift
66
46
  val = ARGV.shift
67
47
  if val and !val.empty?
68
48
  if val.downcase == 'nil'
69
- CFG.delete cfg_cmd
70
- Mudbug.save CFG
49
+ MbConfig.delete cfg_cmd
50
+ MbConfig.save
71
51
  conclude "removed config for #{cfg_cmd}"
72
52
  else
73
- CFG[cfg_cmd] = val
74
- Mudbug.save CFG
53
+ MbConfig[cfg_cmd] = val
54
+ MbConfig.save
75
55
  end
76
56
  else
77
- val = CFG[cfg_cmd] or conclude "no #{cfg_cmd} is set"
57
+ val = MbConfig[cfg_cmd] or conclude "no #{cfg_cmd} is set"
78
58
  end
79
59
  conclude "Using #{cfg_cmd}: #{val}"
80
60
 
61
+ when 'reset'
62
+ MbConfig.reset
63
+ conclude "config was reset"
64
+
65
+ when 'payload'
66
+ ARGV.shift # confirmed
67
+ val = ARGV.shift
68
+ if val and !val.empty?
69
+ case val.downcase
70
+ when '-'
71
+ MbConfig['payload'] = JSON.parse $stdin.read
72
+ MbConfig.save
73
+ when 'nil'
74
+ MbConfig.delete 'payload'
75
+ MbConfig.save
76
+ conclude "removed config for payload"
77
+ end
78
+ else
79
+ MbConfig['payload'] or conclude "no payload is set"
80
+ end
81
+ conclude "Using payload:\n#{JSON.pretty_generate MbConfig['payload']}"
82
+
81
83
  when 'accept'
82
84
  ARGV.shift # confirmed cfg_cmd, go ahead and shift
83
85
  accepts = []
@@ -87,8 +89,8 @@ if cfg_cmd
87
89
 
88
90
  case accepts.length
89
91
  when 0
90
- if CFG['accept']
91
- conclude "Using accept: #{CFG['accept'].join(' ')}"
92
+ if MbConfig['accept']
93
+ conclude "Using accept: #{MbConfig['accept'].join(' ')}"
92
94
  else
93
95
  conclude "no accept is set"
94
96
  end
@@ -100,28 +102,28 @@ if cfg_cmd
100
102
  else
101
103
  accepts.map! { |s| s.gsub %r{[ ,:]+}, '' }
102
104
  end
103
- CFG['accept'] = accepts
104
- Mudbug.save CFG
105
- conclude "using accept: #{CFG['accept']}"
105
+ MbConfig['accept'] = accepts
106
+ MbConfig.save
107
+ conclude "using accept: #{MbConfig['accept']}"
106
108
 
107
109
  when 'config'
108
- conclude Mudbug.load.inspect
110
+ conclude MbConfig.dump
109
111
  end
110
112
  end
111
113
 
112
114
  #
113
- # Done with manipulating CFG and CFG_FILE
115
+ # Done with manipulating MbConfig
114
116
  #
115
117
 
116
118
  #
117
- # Stage 2: create Mudbug based on CFG and command line options
119
+ # Stage 2: create Mudbug based on MbConfig and command line options
118
120
  #
119
121
 
120
- # set the host, always from CFG
121
- conclude "no host is set" unless CFG['host']
122
- mb = Mudbug.new CFG['host']
122
+ # set the host, always from MbConfig
123
+ usage "no host is set" unless MbConfig['host']
124
+ mb = Mudbug.new MbConfig['host']
123
125
 
124
- mb.accept CFG['accept'].map(&:to_sym) if CFG['accept']
126
+ mb.accept MbConfig['accept'].map(&:to_sym) if MbConfig['accept']
125
127
 
126
128
  # set the method, possibly from CLI args
127
129
  meth_cmd = ARGV.first
@@ -130,7 +132,7 @@ when 'get', 'post', 'put', 'delete', 'del'
130
132
  ARGV.shift # confirmed meth_cmd, go ahead and shift
131
133
  method = meth_cmd
132
134
  else
133
- method = CFG['method'] or usage "no method provided"
135
+ method = MbConfig['method'] or usage "no method provided"
134
136
  end
135
137
 
136
138
  # set the path, possibly from CLI args
@@ -140,15 +142,29 @@ when %r{^\/} # starts with a slash
140
142
  ARGV.shift # confirmed path_cmd, go ahead and shift
141
143
  path = path_cmd
142
144
  else
143
- path = CFG['path'] or usage "no path provided"
145
+ path = MbConfig['path'] or usage "no path provided"
144
146
  end
145
147
 
148
+ #
149
+ # no payload yet; run GET and DELETE
150
+ #
151
+
146
152
  case method
147
153
  when 'get'
148
- data = mb.get path
154
+ begin
155
+ data = mb.get path
156
+ rescue RuntimeError => e
157
+ puts "#{e} - #{e.class}"
158
+ exit 1
159
+ end
149
160
  conclude JSON.pretty_generate data
150
161
  when 'del', 'delete'
151
- data = mb.delete path
162
+ begin
163
+ data = mb.delete path
164
+ rescue RuntimeError => e
165
+ puts "#{e} - #{e.class}"
166
+ exit 1
167
+ end
152
168
  conclude JSON.pretty_generate data
153
169
  when 'post', 'put', 'patch'
154
170
  # do nothing for now
@@ -162,8 +178,9 @@ end
162
178
  payload = ARGV.first
163
179
  case payload
164
180
  when nil
165
- payload = CFG['payload'] or usage "no payload provided"
166
- else
181
+ payload = MbConfig['payload'] or usage "no payload provided"
182
+ when '-'
183
+ payload = $stdin.read
167
184
  begin
168
185
  JSON.parse payload
169
186
  rescue
@@ -171,5 +188,11 @@ else
171
188
  end
172
189
  end
173
190
 
174
- data = mb.send(method, path, payload)
191
+ begin
192
+ data = mb.send(method, path, payload)
193
+ rescue RuntimeError => e
194
+ puts "#{e} - #{e.class}"
195
+ exit 1
196
+ end
197
+
175
198
  conclude JSON.pretty_generate data
@@ -0,0 +1,55 @@
1
+ require 'mudbug'
2
+
3
+ module MbConfig
4
+ FILE = File.expand_path '~/.mudbug'
5
+ CFG = {}
6
+
7
+ def self.[] key
8
+ CFG[key]
9
+ end
10
+
11
+ def self.[]= key, value
12
+ CFG[key] = value
13
+ end
14
+
15
+ def self.delete key
16
+ CFG.delete key
17
+ end
18
+
19
+ # write CFG to FILE
20
+ #
21
+ def self.save
22
+ File.open(FILE, 'w') { |f| f.write CFG.to_json }
23
+ end
24
+
25
+ # call reset as necessary
26
+ #
27
+ def self.load
28
+ reset unless File.exists?(FILE)
29
+ begin
30
+ File.open(FILE, 'r') { |f|
31
+ CFG.merge! JSON.parse f.read
32
+ }
33
+ rescue JSON::ParserError => e
34
+ puts "#{e} (#{e.class})"
35
+ puts "Resetting #{FILE}"
36
+ reset
37
+ # recursion; depends on .reset writing parseable JSON to stop
38
+ load
39
+ end
40
+ end
41
+
42
+ # write an empty hash/object
43
+ #
44
+ def self.reset
45
+ File.open(FILE, 'w') { |f| f.write '{}' }
46
+ end
47
+
48
+ # dump the current CFG (may differ from FILE contents!)
49
+ #
50
+ def self.dump
51
+ JSON.pretty_generate CFG
52
+ end
53
+ end
54
+
55
+ MbConfig.load
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mudbug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1.3
4
+ version: 0.5.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Hull
@@ -95,6 +95,7 @@ files:
95
95
  - LICENSE.txt
96
96
  - rakefile.rb
97
97
  - lib/mudbug.rb
98
+ - lib/mb_config.rb
98
99
  - test/mudbug.rb
99
100
  - examples/accepts_and_methods.rb
100
101
  - bin/mb