scorn 0.3.0 → 0.4.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.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE.txt +1 -1
  4. data/Makefile +4 -4
  5. data/README.md +3 -0
  6. data/lib/scorn.rb +62 -31
  7. metadata +3 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85cc3564b52b437e8e8f4310cfcede5d5696f627d5b0525acb78b8160edbe694
4
- data.tar.gz: 29aae9d5ec45908f599c3e2cba875a0568d80cfa6c831393d1f45d0d0daa15de
3
+ metadata.gz: 0bedbc6554c2cd5389e7d02371797bf45ab92660e5e25e4baa3bacd9a32035ca
4
+ data.tar.gz: b24b770e64ae7365585cd99df1a56834f9d4d7755d66bc1ade51b38b076ed56f
5
5
  SHA512:
6
- metadata.gz: c4fc2fc03aa541256700c41971be22617aba2025db4d7770bd5f8983a0b97f2c4f030ce67f89ef5a0823a2f126c1da73fe10fdd8d6e45e3e2bd11be6f838d24a
7
- data.tar.gz: 4f142484e9ea9296f5d1da76cc5213ee85da9cb1862c4c37d7e376805c4961ec3490ea286fc3364e2ff80821ee09e54ea80fdbf10f28f39660fe0f221fc18c38
6
+ metadata.gz: 4fd97916ebb14977ade4e8c692aa3e4a9037a67a29d263ac40339a6196baeef1a9b311fe51aea4af56ce4c0c1e675b6e2c5e855a3887e19c51981a8a0733c920
7
+ data.tar.gz: fcf050769e5b0f5ee946fbc3dff12ac6180ec7293969cd435159140a3359592c0d1faeb5e57e8efb4729311ae5be3577346fa4824b18ef3452176a93eb107f98
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## scorn 0.4.0 released 2025-08-01
6
+
7
+ * Parse "[]" JSON arrays too
8
+ * Accept :auth or :authentication
9
+ * Fix :verify or :ssl_verify client option
10
+ * Add HEAD requests
11
+
12
+
13
+ ## scorn 0.3.1 released 2021-01-12
14
+
15
+ * Fix duplicate Content-Type headers when POSTing
16
+
17
+
5
18
  ## scorn 0.3.0 released 2021-01-11
6
19
 
7
20
  * Implement basic POST form data
data/LICENSE.txt CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- Copyright (c) 2021-2021, John Mettraux, jmettraux@gmail.com
2
+ Copyright (c) 2021-2025, John Mettraux, jmettraux@gmail.com
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  of this software and associated documentation files (the "Software"), to deal
data/Makefile CHANGED
@@ -1,10 +1,10 @@
1
1
 
2
2
  ## gem tasks ##
3
3
 
4
- NAME = \
5
- $(shell ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.name")
6
- VERSION = \
7
- $(shell ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.version")
4
+ NAME != \
5
+ ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.name"
6
+ VERSION != \
7
+ ruby -e "s = eval(File.read(Dir['*.gemspec'][0])); puts s.version"
8
8
 
9
9
  count_lines:
10
10
  find lib -name "*.rb" | xargs cat | ruby -e "p STDIN.readlines.count { |l| l = l.strip; l[0, 1] != '#' && l != '' }"
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
 
2
2
  # scorn
3
3
 
4
+ [![tests](https://github.com/jmettraux/scorn/workflows/test/badge.svg)](https://github.com/jmettraux/scorn/actions)
5
+ [![gem version](https://badge.fury.io/rb/scorn.svg)](http://badge.fury.io/rb/scorn)
6
+
4
7
  A stupid HTTP client library.
5
8
 
6
9
  ```ruby
data/lib/scorn.rb CHANGED
@@ -10,10 +10,15 @@ require 'net/http'
10
10
 
11
11
  module Scorn
12
12
 
13
- VERSION = '0.3.0'
13
+ VERSION = '0.4.0'
14
14
 
15
15
  class << self
16
16
 
17
+ def head(uri, opts={})
18
+
19
+ Scorn::Client.new(opts).head(uri, opts)
20
+ end
21
+
17
22
  def get(uri, opts={})
18
23
 
19
24
  Scorn::Client.new(opts).get(uri, opts)
@@ -35,15 +40,27 @@ module Scorn
35
40
 
36
41
  @user_agent =
37
42
  opts[:user_agent] ||
38
- "Scorn #{VERSION} - " +
43
+ "scorn #{VERSION} - " +
39
44
  [ 'Ruby', RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM ].join(' ')
40
45
 
41
46
  @ssl_verify_mode =
42
- (opts[:ssl_verify_none] == :none || opts[:verify_none] == :none) ?
47
+ (
48
+ opts[:ssl_verify] == :none || opts[:verify] == :none ||
49
+ opts[:ssl_verify] == false || opts[:verify] == false
50
+ ) ?
43
51
  OpenSSL::SSL::VERIFY_NONE : # :-(
44
52
  OpenSSL::SSL::VERIFY_PEER
45
53
  end
46
54
 
55
+ def head(uri, opts={})
56
+
57
+ u = uri.is_a?(String) ? URI(uri) : uri
58
+
59
+ res = request(u, make_head_req(u, opts))
60
+
61
+ opts[:res] ? res : read_response(res)
62
+ end
63
+
47
64
  def get(uri, opts={})
48
65
 
49
66
  u = uri.is_a?(String) ? URI(uri) : uri
@@ -64,25 +81,19 @@ module Scorn
64
81
 
65
82
  protected
66
83
 
67
- def make_get_req(uri, opts)
84
+ def make_head_req(uri, opts)
68
85
 
69
- accept =
70
- opts[:accept] ||
71
- (opts[:json] && 'application/json') ||
72
- '*/*'
86
+ make_req(:head, uri, gather_headers(:head, opts))
87
+ end
88
+
89
+ def make_get_req(uri, opts)
73
90
 
74
- make_req(
75
- :get, uri,
76
- agent: opts[:user_agent] || user_agent,
77
- accept: accept)
91
+ make_req(:get, uri, gather_headers(:get, opts))
78
92
  end
79
93
 
80
94
  def make_post_req(uri, opts)
81
95
 
82
- req = make_req(
83
- :post, uri,
84
- agent: opts[:user_agent] || user_agent,
85
- type: opts[:content_type] || 'application/x-www-form-urlencoded')
96
+ req = make_req(:post, uri, gather_headers(:post, opts))
86
97
 
87
98
  data = opts[:data]
88
99
  req.set_form_data(data) if data
@@ -90,6 +101,32 @@ module Scorn
90
101
  req
91
102
  end
92
103
 
104
+ def gather_headers(verb, opts)
105
+
106
+ h = {}
107
+
108
+ h['Accept'] =
109
+ opts[:accept] ||
110
+ (opts[:json] && 'application/json') ||
111
+ '*/*'
112
+ h['Agent'] =
113
+ opts[:user_agent] || user_agent
114
+ h['Authorization'] =
115
+ opts[:authorization] || opts[:auth]
116
+
117
+ h['Content-Type'] =
118
+ opts[:content_type] || 'application/x-www-form-urlencoded' \
119
+ if verb == :post
120
+
121
+ opts.each do |k, v|
122
+ h[k] = v if k.start_with?(/X-/)
123
+ end
124
+
125
+ h.compact!
126
+
127
+ h
128
+ end
129
+
93
130
  def make_req(type, uri, headers)
94
131
 
95
132
  u = [ uri.path, uri.query ].compact.join('?')
@@ -97,23 +134,13 @@ module Scorn
97
134
 
98
135
  req =
99
136
  case type
137
+ when :head then Net::HTTP::Head.new(u)
100
138
  when :get then Net::HTTP::Get.new(u)
101
139
  when :post then Net::HTTP::Post.new(u)
102
140
  else fail ArgumentError.new("HTTP #{type} not implemented")
103
141
  end
104
- req.instance_eval { @header.clear }
105
- def req.set_header(k, v); @header[k] = [ v ]; end
106
-
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
142
+
143
+ headers.each { |k, v| req[k] = v }
117
144
 
118
145
  req
119
146
  end
@@ -164,11 +191,15 @@ module Scorn
164
191
  def read_response(res)
165
192
 
166
193
  s = res.body
167
- st = s.strip
194
+ st = s ? s.strip : ''
195
+
196
+ a_z = "#{st[0, 1]}#{st[-1, 1]}"
168
197
 
169
198
  r =
170
- if st[0, 1] == '{' && st[-1, 1] == '}'
199
+ if a_z == '{}' || a_z == '[]'
171
200
  JSON.parse(st) rescue s
201
+ elsif s == nil
202
+ ''
172
203
  else
173
204
  s
174
205
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-01-10 00:00:00.000000000 Z
10
+ date: 2025-08-01 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rspec
@@ -47,7 +46,6 @@ metadata:
47
46
  bug_tracker_uri: https://github.com/jmettraux/scorn/issues
48
47
  homepage_uri: https://github.com/jmettraux/scorn
49
48
  source_code_uri: https://github.com/jmettraux/scorn
50
- post_install_message:
51
49
  rdoc_options: []
52
50
  require_paths:
53
51
  - lib
@@ -62,8 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
60
  - !ruby/object:Gem::Version
63
61
  version: '0'
64
62
  requirements: []
65
- rubygems_version: 3.0.3
66
- signing_key:
63
+ rubygems_version: 3.6.2
67
64
  specification_version: 4
68
65
  summary: stupid HTTP client
69
66
  test_files: []