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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f163f75584cb8c3efd97c4086a5c6def7b7455ddd3a51325e4b205eff1c7932
4
- data.tar.gz: 0d8c44e768a2cba2234c23294d1ac4bec17f78d3b792799387967438cd70e8a4
3
+ metadata.gz: 194bd7ff500a4dfbe818e70197ab5db7728ff7c243a31d92392c7912d10371eb
4
+ data.tar.gz: 844b65792a38d7c008a6a81462e92f580b6daa60a0e446dd011088b7e1afff4e
5
5
  SHA512:
6
- metadata.gz: 2d9f0607650dea8a96dd44491e4de2a91b7a87e947a30fdfde6c039002690e693ad0aed4695bb6ec734a9baf6c41247a005515370cda8623170a5c94f97e0cd6
7
- data.tar.gz: 794d6bf88255e7329e62a2b817f98c7c85b694aca48ccc884b89bc253bd00888267de1d5b9ed86414a8ee2059387aad4563d8574b23df4f225f2db54e6bd24f6
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 = -> 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 = -> a {
17
+ @@header_to_curl = ->a {
18
18
  "\\\n -H '#{a[0]}: #{a[1]}'"
19
19
  }
20
20
 
21
- @@print_curl = -> req { $stdout.puts(to_curl.(req)); req }.curry
21
+ @@print_curl = ->req { $stdout.puts(to_curl.(req)); req }.curry
22
22
  end
@@ -0,0 +1 @@
1
+
@@ -6,7 +6,7 @@ module HttpFn::Httpie
6
6
 
7
7
  fn_reader :print_curl, :req
8
8
 
9
- @@req = -> 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 = -> a {
17
+ @@header_to_httpie = ->a {
18
18
  "\\\n '#{a[0]}: #{a[1]}'"
19
19
  }
20
20
  end
@@ -1,23 +1,24 @@
1
- require 'net/http'
2
- require 'http_fn'
1
+ require "net/http"
2
+ require "http_fn"
3
+ require "fn_reader"
3
4
 
4
5
  module HttpFn::NetHttp
5
6
  include HttpFn
6
- mattr_reader :method_str_to_req, :server, :net_resp
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 = -> req {
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 == 'https'
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 = -> resp { {status: resp.code, header: resp.to_hash, body: resp.body} }
23
+ @@net_resp = ->resp { { status: resp.code, header: resp.to_hash, body: resp.body } }
23
24
  end
@@ -2,7 +2,7 @@ require "superators19"
2
2
 
3
3
  class Proc
4
4
  superator ">>~" do |fn|
5
- -> a { fn.(self.(a)) }
5
+ ->a { fn.(self.(a)) }
6
6
  end
7
7
  end
8
8
 
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 = -> rack { to_env >> rack.method(:call) >> rack_resp_to_resp }
10
- @@to_env = -> request {
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 = -> resp {
46
+ @@rack_resp_to_resp = ->resp {
47
47
  { status: resp[0],
48
- header: resp[1],
49
- body: @@body_from_rack_response.(resp[2]) }
48
+ header: resp[1],
49
+ body: @@body_from_rack_response.(resp[2]) }
50
50
  }
51
51
 
52
- @@body_from_rack_response = -> 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 = -> 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" }
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 = -> f, a { a.nil? ? nil : f.(a) }.curry
16
- @@retry_fn = -> fn, a {
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 = -> filename, to_save {
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 = -> filename, _params { YAML.load(File.read(filename)) }.curry
29
+ @@play = ->filename, _params { YAML.load(File.read(filename)) }.curry
30
30
  # (Float -> String -> Bool) -> String -> (a -> b) -> b
31
- @@cache = -> expired, filename, fn, param {
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 = -> sec, a { !File.exist?(a) || (Time.now - File.mtime(a)) > sec }.curry
40
- @@count_by = -> fn, a {
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 = -> print, msg, fn, a {
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 = -> 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
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
@@ -1,3 +1,3 @@
1
1
  module HttpFn
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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_ = -> 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
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 = -> a { $stdout.puts a.pretty_inspect; a }
33
- @@to_uri = -> req {
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]
@@ -1,18 +1,13 @@
1
- require 'minitest_helper'
2
- require 'http_fn'
3
- require 'http_fn/rack'
4
- require 'http_fn/curl'
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
@@ -1,4 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
- require 'http_fn'
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "http_fn"
3
3
 
4
- require 'minitest/autorun'
4
+ require "minitest/autorun"
data/test/test_http_fn.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'minitest_helper'
1
+ require "minitest_helper"
2
2
 
3
3
  class TestHttpFn < MiniTest::Unit::TestCase
4
4
  def test_that_it_has_a_version_number
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.0
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: 2020-12-26 00:00:00.000000000 Z
11
+ date: 2022-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler