scorn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b641e96b08b81079197dad19ae5b9cc0a8cee82e4b2682055d705d3a1bdaff52
4
+ data.tar.gz: f35ce97fea114e1e9d754cb44a6e03b9d7834689280de793d9539dd988850552
5
+ SHA512:
6
+ metadata.gz: f9bdec0978022c368b95bb9f56f090fcbda07051ee1411f877b54d8aebd60717dd45fd67f659c53e510560db71a4aafbf841d09430e45a5f27267dc78ce2d3db
7
+ data.tar.gz: c0f2e3dc90c6b396dbba5f0a7cdba58edbd137bcc05073e83cf173a3a7a07fd70763e1b4ceab8c0c1918a81b5d36b99e2987c5534d51f3d270cb04d4dc914e6f
@@ -0,0 +1,8 @@
1
+
2
+ # CHANGELOG.md
3
+
4
+
5
+ ## scorn 0.1.0 released 2021-01-05
6
+
7
+ * Initial release
8
+
@@ -0,0 +1,5 @@
1
+
2
+ # scorn credits
3
+
4
+ * John Mettraux https://github.com/jmettraux author and maintainer
5
+
@@ -0,0 +1,24 @@
1
+
2
+ Copyright (c) 2021-2021, John Mettraux, jmettraux@gmail.com
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ THE SOFTWARE.
21
+
22
+
23
+ Made in Japan
24
+
@@ -0,0 +1,45 @@
1
+
2
+ ## gem tasks ##
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")
8
+
9
+ count_lines:
10
+ find lib -name "*.rb" | xargs cat | ruby -e "p STDIN.readlines.count { |l| l = l.strip; l[0, 1] != '#' && l != '' }"
11
+ find spec -name "*_spec.rb" | xargs cat | ruby -e "p STDIN.readlines.count { |l| l = l.strip; l[0, 1] != '#' && l != '' }"
12
+ cl: count_lines
13
+
14
+ scan:
15
+ scan lib/**/*.rb
16
+
17
+ gemspec_validate:
18
+ @echo "---"
19
+ ruby -e "s = eval(File.read(Dir['*.gemspec'].first)); p s.validate"
20
+ @echo "---"
21
+
22
+ name: gemspec_validate
23
+ @echo "$(NAME) $(VERSION)"
24
+
25
+ cw:
26
+ find lib -name "*.rb" -exec ruby -cw {} \;
27
+
28
+ build: gemspec_validate
29
+ gem build $(NAME).gemspec
30
+ mkdir -p pkg
31
+ mv $(NAME)-$(VERSION).gem pkg/
32
+
33
+ push: build
34
+ gem push --otp "$(OTP)" pkg/$(NAME)-$(VERSION).gem
35
+
36
+ spec:
37
+ bundle exec rspec
38
+ test: spec
39
+
40
+
41
+ ## specific to project ##
42
+
43
+
44
+ .PHONY: count_lines scan gemspec_validate name cw build push spec
45
+
@@ -0,0 +1,14 @@
1
+
2
+ # scorn
3
+
4
+ A stupid HTTP client library.
5
+
6
+ ```ruby
7
+ data = Scorn.get('https://example.com/data.json')
8
+ ```
9
+
10
+
11
+ ## LICENSE
12
+
13
+ MIT, see [LICENSE.txt](LICENSE.txt)
14
+
@@ -0,0 +1,108 @@
1
+
2
+ require 'json'
3
+ require 'time'
4
+ require 'base64'
5
+ require 'webrick' # for the http code to message mapping
6
+ require 'ostruct'
7
+ require 'openssl'
8
+ require 'net/http'
9
+
10
+
11
+ module Scorn
12
+
13
+ VERSION = '0.1.0'
14
+
15
+ class << self
16
+
17
+ def get(uri, opts={})
18
+
19
+ res = Scorn::Client.new(opts).get(uri, opts)
20
+
21
+ s = res.body
22
+
23
+ st = s.strip
24
+ return (JSON.parse(st) rescue s) if st[0, 1] == '{' && st[-1, 1] == '}'
25
+
26
+ s
27
+ end
28
+ end
29
+
30
+ class Client
31
+
32
+ attr_accessor :user_agent, :ssl_verify_mode
33
+
34
+ def initialize(opts)
35
+
36
+ @opts = opts
37
+
38
+ @user_agent =
39
+ opts[:user_agent] ||
40
+ "Scorn #{VERSION} - " +
41
+ [ 'Ruby', RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM ].join(' ')
42
+
43
+ @ssl_verify_mode =
44
+ (opts[:ssl_verify_none] == :none || opts[:verify_none] == :none) ?
45
+ OpenSSL::SSL::VERIFY_NONE : # :-(
46
+ OpenSSL::SSL::VERIFY_PEER
47
+ end
48
+
49
+ def get(uri, opts)
50
+
51
+ request(uri, make_get_req(uri, opts))
52
+ end
53
+
54
+ protected
55
+
56
+ def make_get_req(uri, opts)
57
+
58
+ req = Net::HTTP::Get.new(uri.to_s)
59
+ req.instance_eval { @header.clear }
60
+ def req.set_header(k, v); @header[k] = [ v ]; end
61
+
62
+ req.set_header('User-Agent', user_agent)
63
+ #req.set_header('Accept', 'application/json')
64
+
65
+ req
66
+ end
67
+
68
+ def monow; Process.clock_gettime(Process::CLOCK_MONOTONIC); end
69
+
70
+ def request(uri, req)
71
+
72
+ u = uri.is_a?(String) ? URI(uri) : uri
73
+
74
+ t0 = monow
75
+
76
+ http = make_net_http(u)
77
+ #t.set_debug_output($stdout) if u.to_s.match?(/search/)
78
+
79
+ res = http.request(req)
80
+
81
+ class << res
82
+ attr_accessor :_uri, :_elapsed, :_proxy
83
+ def _headers; each_header.inject({}) { |h, (k, v)| h[k] = v; h }; end
84
+ end
85
+ res._uri = uri.to_s
86
+ res._elapsed = monow - t0
87
+ #res._proxy = detail_proxy(http)
88
+
89
+ fail "request returned a #{res.class} and not a Net::HTTPResponse" \
90
+ unless res.is_a?(Net::HTTPResponse)
91
+
92
+ res
93
+ end
94
+
95
+ def make_net_http(uri)
96
+
97
+ http = Net::HTTP.new(uri.host, uri.port)
98
+
99
+ if uri.scheme == 'https'
100
+ http.use_ssl = true
101
+ http.verify_mode = ssl_verify_mode
102
+ end
103
+
104
+ http
105
+ end
106
+ end
107
+ end
108
+
@@ -0,0 +1,46 @@
1
+
2
+ Gem::Specification.new do |s|
3
+
4
+ s.name = 'scorn'
5
+
6
+ s.version = File.read(
7
+ File.expand_path('../lib/scorn.rb', __FILE__)
8
+ ).match(/ VERSION *= *['"]([^'"]+)/)[1]
9
+
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = [ 'John Mettraux' ]
12
+ s.email = [ 'jmettraux@gmail.com' ]
13
+ s.homepage = 'https://github.com/jmettraux/scorn'
14
+ s.license = 'MIT'
15
+ s.summary = 'stupid HTTP client'
16
+
17
+ s.description = %{
18
+ A stupid HTTP client library. Consider with scorn.
19
+ }.strip
20
+
21
+ s.metadata = {
22
+ 'changelog_uri' => s.homepage + '/blob/master/CHANGELOG.md',
23
+ 'documentation_uri' => s.homepage,
24
+ 'bug_tracker_uri' => s.homepage + '/issues',
25
+ #'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/floraison',
26
+ 'homepage_uri' => s.homepage,
27
+ 'source_code_uri' => s.homepage,
28
+ #'wiki_uri' => s.homepage + '/wiki',
29
+ }
30
+
31
+ #s.files = `git ls-files`.split("\n")
32
+ s.files = Dir[
33
+ 'README.{md,txt}',
34
+ 'CHANGELOG.{md,txt}', 'CREDITS.{md,txt}', 'LICENSE.{md,txt}',
35
+ 'Makefile',
36
+ 'lib/**/*.rb', #'spec/**/*.rb', 'test/**/*.rb',
37
+ "#{s.name}.gemspec",
38
+ ]
39
+
40
+ #s.add_runtime_dependency 'raabro', '~> 1.4'
41
+
42
+ s.add_development_dependency 'rspec', '~> 3.8'
43
+
44
+ s.require_path = 'lib'
45
+ end
46
+
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scorn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Mettraux
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.8'
27
+ description: A stupid HTTP client library. Consider with scorn.
28
+ email:
29
+ - jmettraux@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - CREDITS.md
36
+ - LICENSE.txt
37
+ - Makefile
38
+ - README.md
39
+ - lib/scorn.rb
40
+ - scorn.gemspec
41
+ homepage: https://github.com/jmettraux/scorn
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ changelog_uri: https://github.com/jmettraux/scorn/blob/master/CHANGELOG.md
46
+ documentation_uri: https://github.com/jmettraux/scorn
47
+ bug_tracker_uri: https://github.com/jmettraux/scorn/issues
48
+ homepage_uri: https://github.com/jmettraux/scorn
49
+ source_code_uri: https://github.com/jmettraux/scorn
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.0.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: stupid HTTP client
69
+ test_files: []