kis_http 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/kis_http.rb +48 -0
- data/lib/kis_http/headers.rb +83 -0
- data/lib/kis_http/options.rb +148 -0
- data/lib/kis_http/version.rb +3 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 90cf8804c9b8fab2d42e58567bc05f304344fac286b6874240a20e3e832b8852
|
4
|
+
data.tar.gz: a0eb1a0095d20ba88ffbffc95accc6a08e95e6081811dccb87002b8ae1cb1f80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f72fcfc774f7167b69d1affa960f9cee2bfa75f63efe2e6da743f92348f6a96b8474f9f05b537e7107f924bd95328468c9fe26c4f7b37d1e543a0e287bdc10c
|
7
|
+
data.tar.gz: 29f28c1523dc105a1360dd80daee941f61fd9de9b9fb890460c488b22b2a188e31a680a76b57e55c02b33137718f5e9742942db5c20eab00edcdf50ac3b54f05
|
data/lib/kis_http.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'kis_http/version'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
# All the HTTP things
|
7
|
+
module KisHttp
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def get(url, **kwargs)
|
12
|
+
request(url, **kwargs) do |uri|
|
13
|
+
Net::HTTP::Get.new(uri)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(url, **kwargs)
|
18
|
+
request(url, **kwargs) do |uri|
|
19
|
+
Net::HTTP::Post.new(uri)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def put(url, **kwargs)
|
24
|
+
request(url, **kwargs) do |uri|
|
25
|
+
Net::HTTP::Put.new(uri)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def request(url, body: nil, headers: nil, options: nil)
|
32
|
+
options = Rest.Options(options) if options
|
33
|
+
|
34
|
+
uri = URI("#{url}#{options}")
|
35
|
+
|
36
|
+
request = yield uri.request_uri
|
37
|
+
|
38
|
+
Rest.Headers(headers).assign_each_to(request) if headers
|
39
|
+
|
40
|
+
request.body = body ? JSON.generate(body) : ''
|
41
|
+
|
42
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
43
|
+
http.use_ssl = (uri.scheme == 'https')
|
44
|
+
|
45
|
+
http.request(request)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
# TLD
|
6
|
+
module KisHttp
|
7
|
+
# A header builder class for outgoing requests
|
8
|
+
class Headers
|
9
|
+
extend Forwardable
|
10
|
+
# @!macro [attach] def_delegators
|
11
|
+
# @!method each_pair
|
12
|
+
# Forwards to $1.
|
13
|
+
# @see Hash#each_pair
|
14
|
+
# @!method fetch
|
15
|
+
# Forwards to $1.
|
16
|
+
# @see Hash#fetch
|
17
|
+
def_delegators :@heads, :each_pair, :fetch
|
18
|
+
|
19
|
+
def initialize(args = {})
|
20
|
+
build(args)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Add or update the request headers
|
24
|
+
#
|
25
|
+
# @return [Headers] self
|
26
|
+
def build(args = {})
|
27
|
+
@heads ||= {}
|
28
|
+
|
29
|
+
args.to_h.each do |(key, value)|
|
30
|
+
@heads[key] = value
|
31
|
+
end
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
# Assign each header to object via :[]
|
37
|
+
def assign_each_to(obj)
|
38
|
+
each_pair do |header, value|
|
39
|
+
obj[header] = value
|
40
|
+
end
|
41
|
+
|
42
|
+
obj
|
43
|
+
end
|
44
|
+
|
45
|
+
# Remove key/value from headers via key
|
46
|
+
#
|
47
|
+
# @param key [Symbol, String] key to look up
|
48
|
+
# @return [String, Symbol, nil] returns value if key found, `nil` otherwise.
|
49
|
+
def remove(key)
|
50
|
+
@heads.delete(key)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Add the values of one `Headers` into another
|
54
|
+
#
|
55
|
+
# @param other [Headers] instance of `Headers`
|
56
|
+
# @return [Headers]
|
57
|
+
def +(other)
|
58
|
+
raise TypeError, "Headers type expected, #{other.class} given" \
|
59
|
+
unless other.is_a? Headers
|
60
|
+
|
61
|
+
@heads.merge(other.instance_variable_get(:@heads))
|
62
|
+
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
# @return [Hash] hash of the `Headers`
|
67
|
+
def to_h
|
68
|
+
@heads
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.Headers(obj)
|
73
|
+
if obj.is_a? Rest::Headers
|
74
|
+
obj
|
75
|
+
elsif obj.is_a? Hash
|
76
|
+
Rest::Headers.new(**obj)
|
77
|
+
elsif obj.is_a? Array
|
78
|
+
Rest::Headers.new(**obj.to_h)
|
79
|
+
else
|
80
|
+
raise 'Invalid object type for Headers!'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# TLD
|
4
|
+
module KisHttp
|
5
|
+
# A url options builder class for outgoing requests.
|
6
|
+
class Options
|
7
|
+
def initialize(args = {})
|
8
|
+
build(args)
|
9
|
+
end
|
10
|
+
|
11
|
+
# url safe rendering of options for the url
|
12
|
+
#
|
13
|
+
# @return [String] url options
|
14
|
+
def opts
|
15
|
+
if @opts.empty?
|
16
|
+
''
|
17
|
+
else
|
18
|
+
"?#{@opts.join('&')}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Add or update url options
|
23
|
+
#
|
24
|
+
# @return [Options] self
|
25
|
+
def build(args = {})
|
26
|
+
@opts ||= []
|
27
|
+
|
28
|
+
args.to_h.each do |(key, value)|
|
29
|
+
remove(key)
|
30
|
+
@opts.push("#{key}=#{value}")
|
31
|
+
end
|
32
|
+
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
# Fetch the `key=value`
|
37
|
+
#
|
38
|
+
# @param [Symbol, String] key of the key/value pair to fetch
|
39
|
+
# @return [String]
|
40
|
+
def fetch(key)
|
41
|
+
@opts.each do |item|
|
42
|
+
return item if key.to_s == split.call(item).first
|
43
|
+
end
|
44
|
+
|
45
|
+
raise KeyError, "key not found #{key}" unless block_given?
|
46
|
+
|
47
|
+
yield
|
48
|
+
end
|
49
|
+
|
50
|
+
# Fetch and remove `key=value`. Modifies `Options`.
|
51
|
+
#
|
52
|
+
# @param [Symbol, String] key of the key/value pair to fetch
|
53
|
+
# @return [String]
|
54
|
+
def fetch!(key, &block)
|
55
|
+
result = fetch(key, &block)
|
56
|
+
remove(key)
|
57
|
+
result
|
58
|
+
end
|
59
|
+
|
60
|
+
# Execute a block of code and restore original `Options` state afterwards
|
61
|
+
# @yield [Options]
|
62
|
+
def immutable
|
63
|
+
old = @opts
|
64
|
+
result = yield self
|
65
|
+
@opts = old
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
# Remove key/value from options via key
|
70
|
+
#
|
71
|
+
# @param key [Symbol, String] key to look up
|
72
|
+
# @return [String, nil] returns a `String` if key found, `nil` otherwise.
|
73
|
+
def remove(key)
|
74
|
+
return_value = nil
|
75
|
+
|
76
|
+
@opts = @opts.delete_if do |item|
|
77
|
+
head, tail = split.call item
|
78
|
+
|
79
|
+
return_value = tail if head == key.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
return_value
|
83
|
+
end
|
84
|
+
|
85
|
+
# this purges all options
|
86
|
+
#
|
87
|
+
# @return [Options] self
|
88
|
+
def reset!
|
89
|
+
@opts = []
|
90
|
+
|
91
|
+
self
|
92
|
+
end
|
93
|
+
|
94
|
+
# Add the values of one `Options` into another
|
95
|
+
#
|
96
|
+
# @param other [Options] instance of `Options`
|
97
|
+
# @return [Options]
|
98
|
+
def +(other)
|
99
|
+
unless other.is_a? Options
|
100
|
+
raise TypeError, "Options type expected, #{other.class} given"
|
101
|
+
end
|
102
|
+
|
103
|
+
update other.instance_variable_get(:@opts)
|
104
|
+
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
# (see #opts)
|
109
|
+
def to_s
|
110
|
+
opts
|
111
|
+
end
|
112
|
+
|
113
|
+
# @return [Hash] hash of the `Options`
|
114
|
+
def to_h
|
115
|
+
@opts.map(&split).to_h
|
116
|
+
end
|
117
|
+
|
118
|
+
private # @private
|
119
|
+
|
120
|
+
def split
|
121
|
+
->(entry) { entry.split('=') }
|
122
|
+
end
|
123
|
+
|
124
|
+
def parse(other)
|
125
|
+
return other.split('&').map(&split).to_h if other.is_a? String
|
126
|
+
|
127
|
+
other.map(&split).to_h
|
128
|
+
end
|
129
|
+
|
130
|
+
def update(other)
|
131
|
+
return self unless other
|
132
|
+
|
133
|
+
build(parse(other))
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.Options(obj)
|
138
|
+
if obj.is_a? Rest::Options
|
139
|
+
obj
|
140
|
+
elsif obj.is_a? Hash
|
141
|
+
Rest::Options.new(**obj)
|
142
|
+
elsif obj.is_a? Array
|
143
|
+
Rest::Options.new(**obj.to_h)
|
144
|
+
else
|
145
|
+
raise 'Invalid object type for Options!'
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kis_http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel P. Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: DRY for Net::HTTP
|
14
|
+
email:
|
15
|
+
- 6ftdan@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/kis_http.rb
|
21
|
+
- lib/kis_http/headers.rb
|
22
|
+
- lib/kis_http/options.rb
|
23
|
+
- lib/kis_http/version.rb
|
24
|
+
homepage: https://github.com/danielpclark/kist_http
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.3.0
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.1.4
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Keep it Simple for Net::HTTP
|
47
|
+
test_files: []
|