http_fn 0.1.1 → 0.1.2
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/utils.rb +57 -55
- data/lib/http_fn/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04320754f24479e425e4e8517bd34414e1174be0346d96a4e12cbb5f8508e97d
|
4
|
+
data.tar.gz: d15ef868864f0bf47f16057bcb5bfe5be0f843949a492fcfe8606ff1c26243b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e483e80dea589e759fcdbe8525ef5eb4a87e951e337410e24af5f3db98b90cedbd19924df584dcd1ee3ed6eb5c3327cbf217fa1fe97f32760d82f8075ffd35b2
|
7
|
+
data.tar.gz: 852ce1cbd0cdd7090da788e70603282d172639925d75acf0673ff6ccd8f9e7d435af7115ec42638dba785c12e2d6774552370c2912b83a5f53355a8f83f002fb
|
data/lib/http_fn/utils.rb
CHANGED
@@ -2,61 +2,63 @@ require "json"
|
|
2
2
|
require "pp"
|
3
3
|
require "yaml"
|
4
4
|
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module HttpFn
|
6
|
+
module Utils
|
7
|
+
fn_reader :parse_json, :debug, :at, :retry_fn,
|
8
|
+
:record, :player, :count_by, :cache, :red, :time,
|
9
|
+
:expired, :apply, :and_then, :default, :map, :get, :try
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
11
|
+
@@parse_json = JSON.method(:parse)
|
12
|
+
@@debug = ->print, a, b { print.(a); print.(b.to_s); b }.curry
|
13
|
+
@@at = ->key, hash { hash[key] }.curry
|
14
|
+
@@red = ->a { "\033[31m#{a}\033[0m" }
|
15
|
+
# ( a -> b ) -> a -> b
|
16
|
+
@@try = ->f, a { a.nil? ? nil : f.(a) }.curry
|
17
|
+
@@retry_fn = ->fn, a {
|
18
|
+
begin
|
19
|
+
fn.(a)
|
20
|
+
rescue
|
21
|
+
sleep(1)
|
22
|
+
puts "RETRYING"
|
23
|
+
@@retry_fn.(fn).(a)
|
24
|
+
end
|
25
|
+
}.curry
|
26
|
+
@@record = ->filename, to_save {
|
27
|
+
File.open(filename, "w+") { |a| a << to_save.to_yaml }
|
28
|
+
to_save
|
29
|
+
}.curry
|
30
|
+
@@play = ->filename, _params { YAML.load(File.read(filename)) }.curry
|
31
|
+
# (Float -> String -> Bool) -> String -> (a -> b) -> b
|
32
|
+
@@cache = ->expired, filename, fn, param {
|
33
|
+
if expired.(filename)
|
34
|
+
@@record.(filename).(fn.(param))
|
35
|
+
else
|
36
|
+
puts "reading from cache"
|
37
|
+
@@play.(filename).(nil)
|
38
|
+
end
|
39
|
+
}.curry
|
40
|
+
@@expired = ->sec, a { !File.exist?(a) || (Time.now - File.mtime(a)) > sec }.curry
|
41
|
+
@@count_by = ->fn, a {
|
42
|
+
a.inject({}) do |res, a|
|
43
|
+
by = fn.(a)
|
44
|
+
res[by] ||= 0
|
45
|
+
res[by] += 1
|
46
|
+
res
|
47
|
+
end
|
48
|
+
}.curry
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
50
|
+
# (String -> String) -> String -> ( a -> b ) -> a -> b
|
51
|
+
@@time = ->print, msg, fn, a {
|
52
|
+
start_time = Time.now
|
53
|
+
res = fn.(a)
|
54
|
+
print.("Time duration for #{msg} = #{Time.now - start_time}")
|
55
|
+
res
|
56
|
+
}.curry
|
57
|
+
@@apply = ->method, a { a.send(method) }.curry
|
58
|
+
@@default = ->default, a { a.nil? ? default : a }.curry
|
59
|
+
@@and_then = ->f, a { a.nil? ? nil : f.(a) }.curry
|
60
|
+
@@map = ->f, enum { enum.map(&f) }.curry
|
61
|
+
@@at = ->key, hash { hash; hash[key] }.curry
|
62
|
+
@@get = ->method, obj { obj.send(method) }.curry
|
63
|
+
end
|
62
64
|
end
|
data/lib/http_fn/version.rb
CHANGED