couch-shell 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/couch-shell/response.rb +7 -7
- data/lib/couch-shell/shell.rb +67 -36
- data/lib/couch-shell/version.rb +1 -1
- metadata +34 -4
data/lib/couch-shell/response.rb
CHANGED
@@ -11,7 +11,7 @@ module CouchShell
|
|
11
11
|
"application/json", "text/plain"
|
12
12
|
].freeze
|
13
13
|
|
14
|
-
# +response+ is a
|
14
|
+
# +response+ is a HTTP::Message from httpclient library
|
15
15
|
def initialize(response)
|
16
16
|
@res = response
|
17
17
|
@json = nil
|
@@ -23,9 +23,9 @@ module CouchShell
|
|
23
23
|
def json
|
24
24
|
unless @computed_json
|
25
25
|
if JSON_CONTENT_TYPES.include?(content_type) &&
|
26
|
-
|
26
|
+
!body.nil? && !body.empty?
|
27
27
|
begin
|
28
|
-
@json = JsonValue.wrap(JSON.parse(
|
28
|
+
@json = JsonValue.wrap(JSON.parse(body))
|
29
29
|
rescue JSON::ParserError
|
30
30
|
@json = false
|
31
31
|
end
|
@@ -36,11 +36,11 @@ module CouchShell
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def code
|
39
|
-
@res.
|
39
|
+
@res.status.to_s
|
40
40
|
end
|
41
41
|
|
42
42
|
def message
|
43
|
-
@res.
|
43
|
+
@res.reason
|
44
44
|
end
|
45
45
|
|
46
46
|
def ok?
|
@@ -48,11 +48,11 @@ module CouchShell
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def body
|
51
|
-
@res.
|
51
|
+
@res.content
|
52
52
|
end
|
53
53
|
|
54
54
|
def content_type
|
55
|
-
@res.
|
55
|
+
@res.contenttype.sub(/;[^;]*\z/, '')
|
56
56
|
end
|
57
57
|
|
58
58
|
def attr(name, altname = nil)
|
data/lib/couch-shell/shell.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
|
3
3
|
require "uri"
|
4
|
-
require "
|
4
|
+
require "httpclient"
|
5
5
|
require "highline"
|
6
6
|
require "couch-shell/response"
|
7
7
|
require "couch-shell/ring_buffer"
|
@@ -27,6 +27,22 @@ module CouchShell
|
|
27
27
|
|
28
28
|
end
|
29
29
|
|
30
|
+
class FileToUpload
|
31
|
+
|
32
|
+
attr_reader :filename, :content_type
|
33
|
+
|
34
|
+
def initialize(filename, content_type = nil)
|
35
|
+
@filename = filename
|
36
|
+
@content_type = content_type
|
37
|
+
end
|
38
|
+
|
39
|
+
def content_type!
|
40
|
+
# TODO: use mime-types and/or file to guess mime type
|
41
|
+
content_type || "application/octet-stream"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
30
46
|
PREDEFINED_VARS = [
|
31
47
|
"uuid", "id", "rev", "idr",
|
32
48
|
"content-type", "server"
|
@@ -43,6 +59,7 @@ module CouchShell
|
|
43
59
|
@highline = HighLine.new(@stdin, @stdout)
|
44
60
|
@responses = RingBuffer.new(10)
|
45
61
|
@eval_context = EvalContext.new(self)
|
62
|
+
@httpclient = HTTPClient.new
|
46
63
|
end
|
47
64
|
|
48
65
|
def normalize_server_url(url)
|
@@ -126,34 +143,35 @@ module CouchShell
|
|
126
143
|
errmsg "Protocol #{@server_url.scheme} not supported, use http."
|
127
144
|
return
|
128
145
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
end
|
149
|
-
res = Response.new(http.request(req))
|
150
|
-
@responses << res
|
151
|
-
rescode = res.code
|
152
|
-
vars = ["r#{@responses.index}"]
|
153
|
-
vars << ["j#{@responses.index}"] if res.json
|
154
|
-
print_response res, " vars: #{vars.join(', ')}"
|
146
|
+
res = http_client_request(method, expand(path), body)
|
147
|
+
@responses << res
|
148
|
+
rescode = res.code
|
149
|
+
vars = ["r#{@responses.index}"]
|
150
|
+
vars << ["j#{@responses.index}"] if res.json
|
151
|
+
print_response res, " vars: #{vars.join(', ')}"
|
152
|
+
res.code
|
153
|
+
end
|
154
|
+
|
155
|
+
def http_client_request(method, absolute_url, body)
|
156
|
+
file = nil
|
157
|
+
headers = {}
|
158
|
+
if body.kind_of?(FileToUpload)
|
159
|
+
file_to_upload = body
|
160
|
+
file = File.open(file_to_upload.filename, "rb")
|
161
|
+
body = [{'Content-Type' => file_to_upload.content_type!,
|
162
|
+
:content => file}]
|
163
|
+
elsif body && body =~ JSON_DOC_START_RX
|
164
|
+
headers['Content-Type'] = "application/json"
|
155
165
|
end
|
156
|
-
|
166
|
+
res = @httpclient.request(method, absolute_url, body, headers)
|
167
|
+
Response.new(res)
|
168
|
+
ensure
|
169
|
+
file.close if file
|
170
|
+
end
|
171
|
+
|
172
|
+
def expand(url)
|
173
|
+
u = @server_url
|
174
|
+
"#{u.scheme}://#{u.host}:#{u.port}#{full_path url}"
|
157
175
|
end
|
158
176
|
|
159
177
|
def full_path(path)
|
@@ -322,22 +340,31 @@ module CouchShell
|
|
322
340
|
end
|
323
341
|
end
|
324
342
|
|
343
|
+
def request_command_with_body(method, argstr)
|
344
|
+
if argstr =~ JSON_DOC_START_RX
|
345
|
+
url, bodyarg = nil, argstr
|
346
|
+
else
|
347
|
+
url, bodyarg= argstr.split(/\s+/, 2)
|
348
|
+
end
|
349
|
+
if bodyarg.start_with?("@")
|
350
|
+
filename, content_type = bodyarg[1..-1].split(/\s+/, 2)
|
351
|
+
body = FileToUpload.new(filename, content_type)
|
352
|
+
else
|
353
|
+
body = bodyarg
|
354
|
+
end
|
355
|
+
request method, interpolate(url), body
|
356
|
+
end
|
357
|
+
|
325
358
|
def command_get(argstr)
|
326
359
|
request "GET", interpolate(argstr)
|
327
360
|
end
|
328
361
|
|
329
362
|
def command_put(argstr)
|
330
|
-
|
331
|
-
request "PUT", interpolate(url), body
|
363
|
+
request_command_with_body("PUT", argstr)
|
332
364
|
end
|
333
365
|
|
334
366
|
def command_post(argstr)
|
335
|
-
|
336
|
-
url, body = nil, argstr
|
337
|
-
else
|
338
|
-
url, body = argstr.split(/\s+/, 2)
|
339
|
-
end
|
340
|
-
request "POST", interpolate(url), body
|
367
|
+
request_command_with_body("POST", argstr)
|
341
368
|
end
|
342
369
|
|
343
370
|
def command_delete(argstr)
|
@@ -396,6 +423,10 @@ module CouchShell
|
|
396
423
|
self.server = argstr
|
397
424
|
end
|
398
425
|
|
426
|
+
def command_expand(argstr)
|
427
|
+
@stdout.puts expand(interpolate(argstr))
|
428
|
+
end
|
429
|
+
|
399
430
|
end
|
400
431
|
|
401
432
|
end
|
data/lib/couch-shell/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Stefan Lang
|
@@ -16,8 +16,38 @@ cert_chain: []
|
|
16
16
|
|
17
17
|
date: 2011-01-19 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: httpclient
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 1
|
31
|
+
- 6
|
32
|
+
- 1
|
33
|
+
version: 2.1.6.1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: highline
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 6
|
47
|
+
- 1
|
48
|
+
version: 1.6.1
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
21
51
|
description:
|
22
52
|
email: langstefan@gmx.at
|
23
53
|
executables:
|