scorn 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +12 -0
- data/lib/scorn.rb +74 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5ba08d0daf842995b186b35c7617fb9f76e80789939cb17e2f42bf492e09aa4f
|
|
4
|
+
data.tar.gz: 36bbefc8c53658eed3aee3685088cf57884f52395fe9e3afc22ee33db841c427
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a7e169aa56ddb9acba9855a8c98ddaf9b6d43f9f2863726c5bb0c30a77702cbdfb61763912ee3661c0bd0435a4189f98efb6b48fe64c246efbcbdd13eb472da3
|
|
7
|
+
data.tar.gz: 1965a0ff5a7c74ba854609929ad38185fb7007e58a9a60fd39de17a774564010a71dae1ffa68eaa1f65a341ce4a4b10f34504add53952cbee6b885a786892f68
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -37,6 +37,18 @@ p r.class # => Hash
|
|
|
37
37
|
p r['form'] # => { 'n' => '-1', 'source' => 'src', 'target' => 'tgt' }
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
```ruby
|
|
41
|
+
r = Scorn.post(
|
|
42
|
+
'https://httpbin.org/post',
|
|
43
|
+
json: { who: 'Doomberg', what: 'buy gold', when: -1 },
|
|
44
|
+
debug: Scorn::Debugger.new)
|
|
45
|
+
|
|
46
|
+
p r['json'] # => { 'who' => 'Doomberg', 'what' => 'buy gold', 'when' => -1 }
|
|
47
|
+
|
|
48
|
+
p r._response._c # => 200
|
|
49
|
+
p r['url'] # => 'https://httpbin.org/post'
|
|
50
|
+
p r['headers']['Accept'] # => 'application/json'
|
|
51
|
+
```
|
|
40
52
|
|
|
41
53
|
### Options:
|
|
42
54
|
|
data/lib/scorn.rb
CHANGED
|
@@ -11,7 +11,7 @@ require 'net/http'
|
|
|
11
11
|
|
|
12
12
|
module Scorn
|
|
13
13
|
|
|
14
|
-
VERSION = '0.
|
|
14
|
+
VERSION = '0.6.0'.freeze
|
|
15
15
|
|
|
16
16
|
class << self
|
|
17
17
|
|
|
@@ -97,7 +97,22 @@ module Scorn
|
|
|
97
97
|
req = make_req(:post, uri, gather_headers(:post, opts))
|
|
98
98
|
|
|
99
99
|
data = opts[:data]
|
|
100
|
-
|
|
100
|
+
json = opts[:json]
|
|
101
|
+
|
|
102
|
+
if data
|
|
103
|
+
|
|
104
|
+
req.set_form_data(data)
|
|
105
|
+
|
|
106
|
+
elsif json.is_a?(Hash) || json.is_a?(Array)
|
|
107
|
+
|
|
108
|
+
req['Content-Type'] = 'application/json'
|
|
109
|
+
req.body = JSON.dump(json)
|
|
110
|
+
|
|
111
|
+
elsif json.is_a?(String)
|
|
112
|
+
|
|
113
|
+
req['Content-Type'] = 'application/json'
|
|
114
|
+
req.body = json
|
|
115
|
+
end
|
|
101
116
|
|
|
102
117
|
req
|
|
103
118
|
end
|
|
@@ -213,8 +228,9 @@ module Scorn
|
|
|
213
228
|
else
|
|
214
229
|
s
|
|
215
230
|
end
|
|
216
|
-
class << r; attr_accessor :_response; end
|
|
231
|
+
class << r; attr_accessor :_response, :_debug; end
|
|
217
232
|
r._response = res
|
|
233
|
+
r._debug = @opts[:debug]
|
|
218
234
|
|
|
219
235
|
r
|
|
220
236
|
end
|
|
@@ -255,5 +271,60 @@ module Scorn
|
|
|
255
271
|
|
|
256
272
|
}.freeze
|
|
257
273
|
end
|
|
274
|
+
|
|
275
|
+
class Debugger
|
|
276
|
+
|
|
277
|
+
attr_reader :a
|
|
278
|
+
|
|
279
|
+
def initialize; @a = []; end
|
|
280
|
+
|
|
281
|
+
def <<(l); @a << l; end
|
|
282
|
+
|
|
283
|
+
def to_s; @a.join(); end
|
|
284
|
+
|
|
285
|
+
def request_lines
|
|
286
|
+
|
|
287
|
+
@a.find { |s| s.match?(/\A"(GET|POST|HEAD|PUT|DELETE) /) }.split('\r\n')
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def request_headera
|
|
291
|
+
|
|
292
|
+
request_lines
|
|
293
|
+
.inject([]) { |a, l|
|
|
294
|
+
m = l.match(/\A([-a-zA-Z0-9]+): (.+)\z/)
|
|
295
|
+
a << [ m[1], m[2] ] if m
|
|
296
|
+
a }
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def response_header_lines
|
|
300
|
+
|
|
301
|
+
@a
|
|
302
|
+
.take_while { |l|
|
|
303
|
+
l != '-> "\\r\\n"\n' }
|
|
304
|
+
.inject([]) { |a, l|
|
|
305
|
+
m = l.match(/\A-> "(.+)\\r\\n"\n\z/)
|
|
306
|
+
a << m[1] if m
|
|
307
|
+
a }
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def response_headera
|
|
311
|
+
|
|
312
|
+
response_header_lines
|
|
313
|
+
.inject([]) { |a, l|
|
|
314
|
+
m = l.match(/\A([A-Z][-a-zA-Z0-9]*):\s*(.+)\z/)
|
|
315
|
+
a << [ m[1], m[2] ] if m
|
|
316
|
+
a }
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def request_headerh
|
|
320
|
+
|
|
321
|
+
request_headera.inject({}) { |h, (k, v)| h[k] = v; h }
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def response_headerh
|
|
325
|
+
|
|
326
|
+
response_headera.inject({}) { |h, (k, v)| h[k] = v; h }
|
|
327
|
+
end
|
|
328
|
+
end
|
|
258
329
|
end
|
|
259
330
|
|