http_fn 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http_fn/curl.rb +3 -3
- data/lib/http_fn/http_fn.rb +1 -0
- data/lib/http_fn/httpie.rb +2 -2
- data/lib/http_fn/net_http.rb +10 -9
- data/lib/http_fn/operators.rb +1 -1
- data/lib/http_fn/rack.rb +6 -6
- data/lib/http_fn/utils.rb +17 -17
- data/lib/http_fn/version.rb +1 -1
- data/lib/http_fn.rb +11 -11
- data/test/http_fn/curl_test.rb +6 -11
- data/test/minitest_helper.rb +3 -3
- data/test/test_http_fn.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 194bd7ff500a4dfbe818e70197ab5db7728ff7c243a31d92392c7912d10371eb
|
4
|
+
data.tar.gz: 844b65792a38d7c008a6a81462e92f580b6daa60a0e446dd011088b7e1afff4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac8ff07d43e82facf96ea66ca8fb3534fdbe5fef4d15a8abd6eb0ca13d40ebfa848ef0e91398d1de0ec621d5c9bee982b5a7c16e16ddadba1de80756714ed174
|
7
|
+
data.tar.gz: dfc8bc0157f372b1339b27315924e49e4f929dd2dc19fb6274be3475e40e14c47e31cfde8f9118a949355c436a4dccca0add6e4627d4fce41b7e87307385681f
|
data/lib/http_fn/curl.rb
CHANGED
@@ -6,7 +6,7 @@ module HttpFn::Curl
|
|
6
6
|
|
7
7
|
fn_reader :print_curl, :req
|
8
8
|
|
9
|
-
@@req = ->
|
9
|
+
@@req = ->req {
|
10
10
|
first_part = %{curl -X '#{req[:method]}' '#{HttpFn::to_uri.(req).to_s}' #{req[:header].map(&@@header_to_curl).join(" ")}}
|
11
11
|
if req[:body] && !req[:body].empty?
|
12
12
|
first_part + %{\n\ -d $'#{req[:body].gsub("'", "\'")}'}
|
@@ -14,9 +14,9 @@ module HttpFn::Curl
|
|
14
14
|
first_part
|
15
15
|
end
|
16
16
|
}
|
17
|
-
@@header_to_curl = ->
|
17
|
+
@@header_to_curl = ->a {
|
18
18
|
"\\\n -H '#{a[0]}: #{a[1]}'"
|
19
19
|
}
|
20
20
|
|
21
|
-
@@print_curl = ->
|
21
|
+
@@print_curl = ->req { $stdout.puts(to_curl.(req)); req }.curry
|
22
22
|
end
|
data/lib/http_fn/http_fn.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
|
data/lib/http_fn/httpie.rb
CHANGED
@@ -6,7 +6,7 @@ module HttpFn::Httpie
|
|
6
6
|
|
7
7
|
fn_reader :print_curl, :req
|
8
8
|
|
9
|
-
@@req = ->
|
9
|
+
@@req = ->req {
|
10
10
|
first_part = %{http #{req[:method]} '#{HttpFn::to_uri.(req).to_s}' #{req[:header].map(&@@header_to_httpie).join(" ")}}
|
11
11
|
if req[:body] && !req[:body].empty?
|
12
12
|
%{echo $'#{req[:body].gsub("'", "\'")}' |\\\n#{first_part}}
|
@@ -14,7 +14,7 @@ module HttpFn::Httpie
|
|
14
14
|
first_part
|
15
15
|
end
|
16
16
|
}
|
17
|
-
@@header_to_httpie = ->
|
17
|
+
@@header_to_httpie = ->a {
|
18
18
|
"\\\n '#{a[0]}: #{a[1]}'"
|
19
19
|
}
|
20
20
|
end
|
data/lib/http_fn/net_http.rb
CHANGED
@@ -1,23 +1,24 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "net/http"
|
2
|
+
require "http_fn"
|
3
|
+
require "fn_reader"
|
3
4
|
|
4
5
|
module HttpFn::NetHttp
|
5
6
|
include HttpFn
|
6
|
-
|
7
|
+
fn_reader :method_str_to_req, :server, :net_resp
|
7
8
|
|
8
|
-
@@method_str_to_req = {"GET" => Net::HTTP::Get, "POST" => Net::HTTP::Post, "DELETE" => Net::HTTP::Delete, "PUT" => Net::HTTP::Put, "PATCH" => Net::HTTP::Patch}
|
9
|
+
@@method_str_to_req = { "GET" => Net::HTTP::Get, "POST" => Net::HTTP::Post, "DELETE" => Net::HTTP::Delete, "PUT" => Net::HTTP::Put, "PATCH" => Net::HTTP::Patch }
|
9
10
|
|
10
|
-
@@server = ->
|
11
|
-
uri = to_uri.(req)
|
11
|
+
@@server = ->req {
|
12
|
+
uri = to_uri.(req)
|
12
13
|
req_ = method_str_to_req.fetch(req.fetch(:method)).new(uri)
|
13
14
|
req_.set_body_internal(req[:body]) if req[:body]
|
14
15
|
header = req.fetch(:header)
|
15
16
|
header.each { |key, val| req_[key] = val }
|
16
17
|
http = Net::HTTP.new(uri.host, uri.port)
|
17
|
-
http.use_ssl = uri.scheme ==
|
18
|
+
http.use_ssl = uri.scheme == "https"
|
18
19
|
# http.set_debug_output($stdout)
|
19
20
|
@@net_resp.(http.request(req_))
|
20
|
-
}
|
21
|
+
}
|
21
22
|
|
22
|
-
@@net_resp = ->
|
23
|
+
@@net_resp = ->resp { { status: resp.code, header: resp.to_hash, body: resp.body } }
|
23
24
|
end
|
data/lib/http_fn/operators.rb
CHANGED
data/lib/http_fn/rack.rb
CHANGED
@@ -6,8 +6,8 @@ require "pp"
|
|
6
6
|
module HttpFn::Rack
|
7
7
|
fn_reader :to_env, :server, :rack_resp_to_resp
|
8
8
|
|
9
|
-
@@server = ->
|
10
|
-
@@to_env = ->
|
9
|
+
@@server = ->rack { to_env >> rack.method(:call) >> rack_resp_to_resp }
|
10
|
+
@@to_env = ->request {
|
11
11
|
session ||= {}
|
12
12
|
session_options ||= {}
|
13
13
|
|
@@ -43,13 +43,13 @@ module HttpFn::Rack
|
|
43
43
|
env
|
44
44
|
}
|
45
45
|
|
46
|
-
@@rack_resp_to_resp = ->
|
46
|
+
@@rack_resp_to_resp = ->resp {
|
47
47
|
{ status: resp[0],
|
48
|
-
|
49
|
-
|
48
|
+
header: resp[1],
|
49
|
+
body: @@body_from_rack_response.(resp[2]) }
|
50
50
|
}
|
51
51
|
|
52
|
-
@@body_from_rack_response = ->
|
52
|
+
@@body_from_rack_response = ->response {
|
53
53
|
body = ""
|
54
54
|
response.each { |line| body << line }
|
55
55
|
response.close if response.respond_to?(:close)
|
data/lib/http_fn/utils.rb
CHANGED
@@ -8,12 +8,12 @@ module Utils
|
|
8
8
|
:expired, :apply, :and_then, :default, :map, :get, :try
|
9
9
|
|
10
10
|
@@parse_json = JSON.method(:parse)
|
11
|
-
@@debug = ->
|
12
|
-
@@at = ->
|
13
|
-
@@red = ->
|
11
|
+
@@debug = ->print, a, b { print.(a); print.(b.to_s); b }.curry
|
12
|
+
@@at = ->key, hash { hash[key] }.curry
|
13
|
+
@@red = ->a { "\033[31m#{a}\033[0m" }
|
14
14
|
# ( a -> b ) -> a -> b
|
15
|
-
@@try = ->
|
16
|
-
@@retry_fn = ->
|
15
|
+
@@try = ->f, a { a.nil? ? nil : f.(a) }.curry
|
16
|
+
@@retry_fn = ->fn, a {
|
17
17
|
begin
|
18
18
|
fn.(a)
|
19
19
|
rescue
|
@@ -22,13 +22,13 @@ module Utils
|
|
22
22
|
@@retry_fn.(fn).(a)
|
23
23
|
end
|
24
24
|
}.curry
|
25
|
-
@@record = ->
|
25
|
+
@@record = ->filename, to_save {
|
26
26
|
File.open(filename, "w+") { |a| a << to_save.to_yaml }
|
27
27
|
to_save
|
28
28
|
}.curry
|
29
|
-
@@play = ->
|
29
|
+
@@play = ->filename, _params { YAML.load(File.read(filename)) }.curry
|
30
30
|
# (Float -> String -> Bool) -> String -> (a -> b) -> b
|
31
|
-
@@cache = ->
|
31
|
+
@@cache = ->expired, filename, fn, param {
|
32
32
|
if expired.(filename)
|
33
33
|
@@record.(filename).(fn.(param))
|
34
34
|
else
|
@@ -36,8 +36,8 @@ module Utils
|
|
36
36
|
@@play.(filename).(nil)
|
37
37
|
end
|
38
38
|
}.curry
|
39
|
-
@@expired = ->
|
40
|
-
@@count_by = ->
|
39
|
+
@@expired = ->sec, a { !File.exist?(a) || (Time.now - File.mtime(a)) > sec }.curry
|
40
|
+
@@count_by = ->fn, a {
|
41
41
|
a.inject({}) do |res, a|
|
42
42
|
by = fn.(a)
|
43
43
|
res[by] ||= 0
|
@@ -47,16 +47,16 @@ module Utils
|
|
47
47
|
}.curry
|
48
48
|
|
49
49
|
# (String -> String) -> String -> ( a -> b ) -> a -> b
|
50
|
-
@@time = ->
|
50
|
+
@@time = ->print, msg, fn, a {
|
51
51
|
start_time = Time.now
|
52
52
|
res = fn.(a)
|
53
53
|
print.("Time duration for #{msg} = #{Time.now - start_time}")
|
54
54
|
res
|
55
55
|
}.curry
|
56
|
-
@@apply = ->
|
57
|
-
@@default = ->
|
58
|
-
@@and_then = ->
|
59
|
-
@@map = ->
|
60
|
-
@@at = ->
|
61
|
-
@@get = ->
|
56
|
+
@@apply = ->method, a { a.send(method) }.curry
|
57
|
+
@@default = ->default, a { a.nil? ? default : a }.curry
|
58
|
+
@@and_then = ->f, a { a.nil? ? nil : f.(a) }.curry
|
59
|
+
@@map = ->f, enum { enum.map(&f) }.curry
|
60
|
+
@@at = ->key, hash { hash; hash[key] }.curry
|
61
|
+
@@get = ->method, obj { obj.send(method) }.curry
|
62
62
|
end
|
data/lib/http_fn/version.rb
CHANGED
data/lib/http_fn.rb
CHANGED
@@ -15,22 +15,22 @@ module HttpFn
|
|
15
15
|
@@empty_req = { proto: "HTTP/1.1", host: "http://example.com", path: "/", query: {}, header: {}, method: "GET", body: "" }
|
16
16
|
@@empty_resp = { status: nil, header: {}, body: {} }
|
17
17
|
|
18
|
-
@@run_ = ->
|
19
|
-
@@verb = ->
|
20
|
-
@@with_host = ->
|
21
|
-
@@with_path = ->
|
22
|
-
@@with_query = ->
|
23
|
-
@@with_json = ->
|
24
|
-
@@with_headers = ->
|
25
|
-
@@add_headers = ->
|
26
|
-
@@with_basic_auth = ->
|
18
|
+
@@run_ = ->fn { fn.(@@empty_req) } # underscore because run conflicts with run fn in minitest
|
19
|
+
@@verb = ->verb, req { req.merge({ method: verb.to_s.upcase }) }.curry
|
20
|
+
@@with_host = ->host, req { req[:host] = host; req }.curry
|
21
|
+
@@with_path = ->path, req { req[:path] = path; req }.curry
|
22
|
+
@@with_query = ->params, req { req[:query] = params; req }.curry
|
23
|
+
@@with_json = ->hash, req { req[:body] = hash.to_json; req }.curry
|
24
|
+
@@with_headers = ->header, req { req[:header] = header; req }.curry
|
25
|
+
@@add_headers = ->header, req { req[:header].merge!(header); req }.curry
|
26
|
+
@@with_basic_auth = ->user_name, pwd, req do
|
27
27
|
encoded = Base64.strict_encode64("#{user_name}:#{pwd}")
|
28
28
|
req.then(&add_headers.({ "Authorization" => "Basic #{encoded}" }))
|
29
29
|
end.curry
|
30
30
|
|
31
31
|
@@json_resp = Utils.at.(:body) >> Utils.parse_json
|
32
|
-
@@print = ->
|
33
|
-
@@to_uri = ->
|
32
|
+
@@print = ->a { $stdout.puts a.pretty_inspect; a }
|
33
|
+
@@to_uri = ->req {
|
34
34
|
uri = URI(req.fetch(:host))
|
35
35
|
req[:query] && uri.query = URI.encode_www_form(req[:query])
|
36
36
|
uri.path = req[:path]
|
data/test/http_fn/curl_test.rb
CHANGED
@@ -1,18 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "minitest_helper"
|
2
|
+
require "http_fn"
|
3
|
+
require "http_fn/rack"
|
4
|
+
require "http_fn/curl"
|
5
5
|
|
6
6
|
class HttpFn::CurlTest < Minitest::Test
|
7
7
|
include HttpFn
|
8
8
|
|
9
|
-
def setup
|
10
|
-
@curl = verb.("GET")
|
11
|
-
with_path.("/coucou") >>~
|
12
|
-
with_headers.(json_headers) >>~
|
13
|
-
with_host.("https://api.github.com") >>~
|
14
|
-
with_json.({user: "martin"}) >>~
|
15
|
-
HttpFn::Curl.req >>+ run_
|
9
|
+
def setup
|
10
|
+
@curl = verb.("GET") >> ~with_path.("/coucou") >> ~with_headers.(json_headers) >> ~with_host.("https://api.github.com") >> ~with_json.({ user: "martin" }) >> ~HttpFn::Curl.req >> +run_
|
16
11
|
end
|
17
12
|
|
18
13
|
def test_should_return_a_curl_command
|
data/test/minitest_helper.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path(
|
2
|
-
require
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "http_fn"
|
3
3
|
|
4
|
-
require
|
4
|
+
require "minitest/autorun"
|
data/test/test_http_fn.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_fn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Chabot
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|