scorn 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +27 -1
  4. data/lib/scorn.rb +83 -14
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21ada8f85a3f176d74bca5d1db932477f5d1e76fada2ffe0606d46a2b7424a4f
4
- data.tar.gz: 6fbe21478d688875fd39cb9d3845781c0f1aae89259ad5c98c14ffbf754fae52
3
+ metadata.gz: 85cc3564b52b437e8e8f4310cfcede5d5696f627d5b0525acb78b8160edbe694
4
+ data.tar.gz: 29aae9d5ec45908f599c3e2cba875a0568d80cfa6c831393d1f45d0d0daa15de
5
5
  SHA512:
6
- metadata.gz: 2edff84c7c87b61652f8c74d4bf89ef755f39470dc7ac3c883eeefba7c155481028c4d0995f795f729d88dbde2cae44b794291a5371e5490824ca165ca4021fa
7
- data.tar.gz: abd609912fe210f3ae8f91a750c67448f7c08ea6116fdc21c72929037b6580a0b9db4595d1163aacb42d49acf537a05b9ecfc259984bb07db5871617eb223dcf
6
+ metadata.gz: c4fc2fc03aa541256700c41971be22617aba2025db4d7770bd5f8983a0b97f2c4f030ce67f89ef5a0823a2f126c1da73fe10fdd8d6e45e3e2bd11be6f838d24a
7
+ data.tar.gz: 4f142484e9ea9296f5d1da76cc5213ee85da9cb1862c4c37d7e376805c4961ec3490ea286fc3364e2ff80821ee09e54ea80fdbf10f28f39660fe0f221fc18c38
@@ -2,6 +2,12 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## scorn 0.3.0 released 2021-01-11
6
+
7
+ * Implement basic POST form data
8
+ * Make HTTP response available via `#_response`
9
+
10
+
5
11
  ## scorn 0.2.0 released 2021-01-05
6
12
 
7
13
  * Don't pass the hostname is GET, it passes in Host:
data/README.md CHANGED
@@ -4,7 +4,33 @@
4
4
  A stupid HTTP client library.
5
5
 
6
6
  ```ruby
7
- data = Scorn.get('https://example.com/data.json')
7
+ r = Scorn.get('https://example.com/data.json')
8
+
9
+ p r._response._c # => 200
10
+ ```
11
+
12
+ ```ruby
13
+ r = Scorn.get('https://httpbin.org/get', json: true)
14
+
15
+ p r._response._c # => 200
16
+ p r['args'] # => {}
17
+ p r['headers']['Host'] # => 'httpbin.org'
18
+ p r['headers']['Accept'] # => 'application/json'
19
+ p r['url'] # => 'https://httpbin.org/get'
20
+ ```
21
+
22
+ ```ruby
23
+ r = Scorn.post(
24
+ 'https://httpbin.org/post',
25
+ data: { source: 'src', target: 'tgt', n: -1 },
26
+ debug: $stderr)
27
+
28
+ p r._response._c # => 200
29
+ p r._response._sta # => 'OK'
30
+ p r._response.code # => '200'
31
+
32
+ p r.class # => Hash
33
+ p r['form'] # => { 'n' => '-1', 'source' => 'src', 'target' => 'tgt' }
8
34
  ```
9
35
 
10
36
 
@@ -10,20 +10,18 @@ require 'net/http'
10
10
 
11
11
  module Scorn
12
12
 
13
- VERSION = '0.2.0'
13
+ VERSION = '0.3.0'
14
14
 
15
15
  class << self
16
16
 
17
17
  def get(uri, opts={})
18
18
 
19
- res = Scorn::Client.new(opts).get(uri, opts)
20
-
21
- s = res.body
19
+ Scorn::Client.new(opts).get(uri, opts)
20
+ end
22
21
 
23
- st = s.strip
24
- return (JSON.parse(st) rescue s) if st[0, 1] == '{' && st[-1, 1] == '}'
22
+ def post(uri, opts)
25
23
 
26
- s
24
+ Scorn::Client.new(opts).post(uri, opts)
27
25
  end
28
26
  end
29
27
 
@@ -46,26 +44,76 @@ module Scorn
46
44
  OpenSSL::SSL::VERIFY_PEER
47
45
  end
48
46
 
49
- def get(uri, opts)
47
+ def get(uri, opts={})
50
48
 
51
49
  u = uri.is_a?(String) ? URI(uri) : uri
52
50
 
53
- request(u, make_get_req(u, opts))
51
+ res = request(u, make_get_req(u, opts))
52
+
53
+ opts[:res] ? res : read_response(res)
54
+ end
55
+
56
+ def post(uri, opts)
57
+
58
+ u = uri.is_a?(String) ? URI(uri) : uri
59
+
60
+ res = request(u, make_post_req(u, opts))
61
+
62
+ opts[:res] ? res : read_response(res)
54
63
  end
55
64
 
56
65
  protected
57
66
 
58
67
  def make_get_req(uri, opts)
59
68
 
69
+ accept =
70
+ opts[:accept] ||
71
+ (opts[:json] && 'application/json') ||
72
+ '*/*'
73
+
74
+ make_req(
75
+ :get, uri,
76
+ agent: opts[:user_agent] || user_agent,
77
+ accept: accept)
78
+ end
79
+
80
+ def make_post_req(uri, opts)
81
+
82
+ req = make_req(
83
+ :post, uri,
84
+ agent: opts[:user_agent] || user_agent,
85
+ type: opts[:content_type] || 'application/x-www-form-urlencoded')
86
+
87
+ data = opts[:data]
88
+ req.set_form_data(data) if data
89
+
90
+ req
91
+ end
92
+
93
+ def make_req(type, uri, headers)
94
+
60
95
  u = [ uri.path, uri.query ].compact.join('?')
61
96
  u = '/' if u.length < 1
62
97
 
63
- req = Net::HTTP::Get.new(u)
98
+ req =
99
+ case type
100
+ when :get then Net::HTTP::Get.new(u)
101
+ when :post then Net::HTTP::Post.new(u)
102
+ else fail ArgumentError.new("HTTP #{type} not implemented")
103
+ end
64
104
  req.instance_eval { @header.clear }
65
105
  def req.set_header(k, v); @header[k] = [ v ]; end
66
106
 
67
- req.set_header('User-Agent', user_agent)
68
- req.set_header('Accept', '*/*')
107
+ headers.each do |k, v|
108
+ hk =
109
+ case k
110
+ when :accept then 'Accept'
111
+ when :agent then 'User-Agent'
112
+ when :type then 'Content-Type'
113
+ else k.to_s
114
+ end
115
+ req.set_header(hk, v)
116
+ end
69
117
 
70
118
  req
71
119
  end
@@ -81,10 +129,14 @@ module Scorn
81
129
  res = http.request(req)
82
130
 
83
131
  class << res
84
- attr_accessor :_uri, :_elapsed, :_proxy
132
+ attr_accessor :_u, :_uri, :_request, :_c, :_sta, :_elapsed, :_proxy
85
133
  def _headers; each_header.inject({}) { |h, (k, v)| h[k] = v; h }; end
86
134
  end
87
- res._uri = uri.to_s
135
+ res._u = uri.to_s
136
+ res._uri = uri
137
+ res._request = req
138
+ res._c = res.code.to_i
139
+ res._sta = WEBrick::HTTPStatus.reason_phrase(res.code) rescue ''
88
140
  res._elapsed = monow - t0
89
141
  #res._proxy = detail_proxy(http)
90
142
 
@@ -108,6 +160,23 @@ module Scorn
108
160
 
109
161
  http
110
162
  end
163
+
164
+ def read_response(res)
165
+
166
+ s = res.body
167
+ st = s.strip
168
+
169
+ r =
170
+ if st[0, 1] == '{' && st[-1, 1] == '}'
171
+ JSON.parse(st) rescue s
172
+ else
173
+ s
174
+ end
175
+ class << r; attr_accessor :_response; end
176
+ r._response = res
177
+
178
+ r
179
+ end
111
180
  end
112
181
  end
113
182
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-05 00:00:00.000000000 Z
11
+ date: 2021-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec