SReq 1.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 +7 -0
- data/lib/sreq.rb +74 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 62e472dff13ff272f9ef5e186fad911155764d8c9a38c4aa313a160f4498bef0
|
|
4
|
+
data.tar.gz: 40dc0c0ab5b48728060cbbd4dea11b296e225946a9190e7b85fdd0e09d5e6b6f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1f5202c1274532e50efdd903f538f2b7c69ce877d3b01e6e57ba196a76f20d2bfbb22a00740e36ac2083964311bfdbae4709385be6c7f0f6644753b0e7d16f01
|
|
7
|
+
data.tar.gz: fa9de7f8de0d5f06f05533f17a737a31359bb205b30f8238eb6acc50a826ec6225edb6ea563cb8c0e9c51e4ba7872f37f925c2499b87327a15ddec5dc15b9d39
|
data/lib/sreq.rb
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require("uri")
|
|
2
|
+
require("net/http")
|
|
3
|
+
require("json")
|
|
4
|
+
|
|
5
|
+
module SReq
|
|
6
|
+
extend self
|
|
7
|
+
|
|
8
|
+
# Example:
|
|
9
|
+
# >> SReq::create_url("https://cardanoscan.io/", "/transaction/{{id}}/", ["name", "age"])
|
|
10
|
+
# => https://cardanoscan.io/transaction/{{id}}?name={{name}}&age={{age}}
|
|
11
|
+
def create_url(base, added, path_params = [])
|
|
12
|
+
url = trim(base, "/") + "/" + trim(added, "/")
|
|
13
|
+
return url if path_params.empty?
|
|
14
|
+
url = url + "?"
|
|
15
|
+
path_params.each do |name|
|
|
16
|
+
url += "#{name}={{#{name}}}&"
|
|
17
|
+
end
|
|
18
|
+
trim(url, "&")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def GET(url, data = {})
|
|
22
|
+
url = fill_url_data(url, data)
|
|
23
|
+
puts "making request: #{url}"
|
|
24
|
+
uri = URI(url)
|
|
25
|
+
Net::HTTP.get_response(uri)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def POST(url, data = {})
|
|
29
|
+
url = fill_url_data(url, data)
|
|
30
|
+
puts "making request: #{url} with data: #{data}"
|
|
31
|
+
uri = URI(url)
|
|
32
|
+
Net::HTTP.post_form(uri, data)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test
|
|
36
|
+
base_url = "https://cardanoscan.io/".freeze
|
|
37
|
+
generated_url = SReq::create_url(base_url, "transaction/{{id}}", ["name", "age"]).freeze
|
|
38
|
+
expected_url = "https://cardanoscan.io/transaction/{{id}}?name={{name}}&age={{age}}"
|
|
39
|
+
raise "Output: #{generated_url}, Expected: #{expected_url}" unless generated_url == expected_url
|
|
40
|
+
data = {"id" => "0x123", "age" => 23, "name" => "Oswald"}
|
|
41
|
+
filled_url = fill_url_data(generated_url, data)
|
|
42
|
+
expected_url = "https://cardanoscan.io/transaction/#{data["id"]}?name=#{data["name"]}&age=#{data["age"]}"
|
|
43
|
+
raise "Output: #{filled_url}, Expected: #{expected_url}" unless filled_url == expected_url
|
|
44
|
+
puts "Test succeeded generating: #{expected_url}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
def trim(inp_str, chars)
|
|
49
|
+
str = inp_str.dup
|
|
50
|
+
chars.each_char do |c|
|
|
51
|
+
deleted = "1"
|
|
52
|
+
while !deleted.nil?
|
|
53
|
+
deleted = str.delete_prefix!(c)
|
|
54
|
+
deleted ||= str.delete_suffix!(c)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
str
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Example:
|
|
62
|
+
# >> SReq::fill_url_data("https://cardanoscan.io/transaction/{{id}}?name={{name}}&age={{age}}", {"id" => "0x123", "age" => 23, "name" => "Oswald"})
|
|
63
|
+
# => https://cardanoscan.io/transaction/0x123?name=Oswald&age=23
|
|
64
|
+
def fill_url_data(url, data)
|
|
65
|
+
param_format = /\{\{([a-zA-Z]*)\}\}/
|
|
66
|
+
url.gsub(param_format).each do |m|
|
|
67
|
+
s = trim(m, "{}")
|
|
68
|
+
data[s] || m
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
puts SReq::test
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: SReq
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: '1.0'
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Zeo Zagart
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-02-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'Small simple composable api-requests good: https://github.com/ZeoZagart/SReq/'
|
|
14
|
+
email: abhash@mail.space
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/sreq.rb
|
|
20
|
+
homepage: https://github.com/ZeoZagart/SReq/
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubygems_version: 3.1.2
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: 'SReq : simple composable api-requests in ruby'
|
|
43
|
+
test_files: []
|