resterl 0.0.16 → 0.0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/resterl/client.rb +46 -34
- data/lib/resterl/generic_request.rb +6 -0
- data/lib/resterl/version.rb +1 -1
- data/spec/lib/resterl/{client.rb → client_spec.rb} +0 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eefda67e9d4b1b634f09e64d6744a43a6733dfd4
|
4
|
+
data.tar.gz: 302093e3f36f08d49d1918759047255aa9c28867
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ccf4876bf18424c64528b8a257d651b531ce92979f52ebbc9cf54e51a4372f881b5e7072accf557b56f5b0385b47c553f52d9df5ef6b07429a348639815f473
|
7
|
+
data.tar.gz: 654b12d576e911c072d58426edb17b37cbd97ce3626b179cebb7a837536d32c93867387e93cdaca8a6e5acefe4a1d15ce9976968bfcbfb2ff2bea1e14d3a98be
|
data/lib/resterl/client.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# encoding:
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
module Resterl
|
4
4
|
class Client
|
@@ -8,6 +8,8 @@ module Resterl
|
|
8
8
|
cache: Resterl::Caches::SimpleCache.new,
|
9
9
|
ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
10
10
|
expiry_multiplier: 10,
|
11
|
+
open_timeout: 15,
|
12
|
+
read_timeout: 60,
|
11
13
|
minimum_cache_lifetime: 5 * 60 # 5 Minuten
|
12
14
|
}
|
13
15
|
|
@@ -70,39 +72,8 @@ module Resterl
|
|
70
72
|
|
71
73
|
# Anfrage stellen, ggf. ETag mit übergeben
|
72
74
|
request = Resterl::GetRequest.new(self, url, params, headers)
|
73
|
-
|
74
|
-
|
75
|
-
response, max_age_seconds =
|
76
|
-
case new_response
|
77
|
-
when Net::HTTPClientError,
|
78
|
-
Net::HTTPServerError
|
79
|
-
# Aus dem Cache muss nichts entfernt werden,
|
80
|
-
# weil ja auch kein Eintrag (mehr) drin ist.
|
81
|
-
new_response.error!
|
82
|
-
when Net::HTTPNotModified
|
83
|
-
# Wenn "304 Not Modified", dann altes
|
84
|
-
# Ergebnis als neues Ergebnis verwenden
|
85
|
-
r_temp = Resterl::Response.new(new_response,
|
86
|
-
options[:minimum_cache_lifetime])
|
87
|
-
old_response.update_expires_at(
|
88
|
-
r_temp.expires_at)
|
89
|
-
[old_response, r_temp.expires_at - Time.now]
|
90
|
-
when Net::HTTPSuccess
|
91
|
-
r = Resterl::Response.new(new_response,
|
92
|
-
options[:minimum_cache_lifetime])
|
93
|
-
[r, r.expires_at - Time.now]
|
94
|
-
else
|
95
|
-
raise 'unknown response'
|
96
|
-
end
|
97
|
-
|
98
|
-
# Cachezeit berechnen
|
99
|
-
expiry = [
|
100
|
-
max_age_seconds.to_i * options[:expiry_multiplier],
|
101
|
-
options[:minimum_cache_lifetime]
|
102
|
-
].max
|
103
|
-
|
104
|
-
# Ergebnis im Cache speichern
|
105
|
-
@cache.write cache_key, response, expiry
|
75
|
+
response, max_age_seconds = get_response(request, old_response)
|
76
|
+
cache_response(response, max_age_seconds, cache_key)
|
106
77
|
|
107
78
|
response
|
108
79
|
end
|
@@ -129,5 +100,46 @@ module Resterl
|
|
129
100
|
def data_to_cache_key *args
|
130
101
|
args.hash
|
131
102
|
end
|
103
|
+
|
104
|
+
def get_response(request, old_response)
|
105
|
+
# Anfrage stellen, ggf. ETag mit übergeben
|
106
|
+
new_response = request.perform.response
|
107
|
+
|
108
|
+
case new_response
|
109
|
+
when Net::HTTPClientError,
|
110
|
+
Net::HTTPServerError
|
111
|
+
# Aus dem Cache muss nichts entfernt werden,
|
112
|
+
# weil ja auch kein Eintrag (mehr) drin ist.
|
113
|
+
new_response.error!
|
114
|
+
when Net::HTTPNotModified
|
115
|
+
reuse_old_response(new_response, old_response)
|
116
|
+
when Net::HTTPSuccess
|
117
|
+
r = Resterl::Response.new(new_response,
|
118
|
+
options[:minimum_cache_lifetime])
|
119
|
+
[r, r.expires_at - Time.now]
|
120
|
+
else
|
121
|
+
raise 'unknown response'
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def cache_response(response, max_age_seconds, cache_key)
|
126
|
+
# Cachezeit berechnen
|
127
|
+
expiry = [
|
128
|
+
max_age_seconds.to_i * options[:expiry_multiplier],
|
129
|
+
options[:minimum_cache_lifetime]
|
130
|
+
].max
|
131
|
+
|
132
|
+
# Ergebnis im Cache speichern
|
133
|
+
@cache.write cache_key, response, expiry
|
134
|
+
end
|
135
|
+
|
136
|
+
def reuse_old_response(new_response, old_response)
|
137
|
+
# Wenn "304 Not Modified", dann altes
|
138
|
+
# Ergebnis als neues Ergebnis verwenden
|
139
|
+
r_temp = Resterl::Response.new(new_response,
|
140
|
+
options[:minimum_cache_lifetime])
|
141
|
+
old_response.update_expires_at(r_temp.expires_at)
|
142
|
+
[old_response, r_temp.expires_at - Time.now]
|
143
|
+
end
|
132
144
|
end
|
133
145
|
end
|
@@ -17,6 +17,7 @@ module Resterl
|
|
17
17
|
def http_object_and_query_path
|
18
18
|
uri = URI.parse(url)
|
19
19
|
http = Net::HTTP.new(uri.host, uri.port)
|
20
|
+
apply_timeout_settings http
|
20
21
|
apply_ssl http, uri
|
21
22
|
|
22
23
|
path_with_query = uri.path
|
@@ -46,6 +47,11 @@ module Resterl
|
|
46
47
|
http.verify_mode = rest_client.options[:ssl_verify_mode]
|
47
48
|
end
|
48
49
|
|
50
|
+
def apply_timeout_settings http
|
51
|
+
http.open_timeout = rest_client.options[:open_timeout]
|
52
|
+
http.read_timeout = rest_client.options[:read_timeout]
|
53
|
+
end
|
54
|
+
|
49
55
|
def redirect_url
|
50
56
|
response['location'] || response.body.match(/<a href=\"([^>]+)\">/i)[1]
|
51
57
|
end
|
data/lib/resterl/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resterl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Dütsch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -139,7 +139,7 @@ files:
|
|
139
139
|
- lib/resterl/response.rb
|
140
140
|
- lib/resterl/version.rb
|
141
141
|
- resterl.gemspec
|
142
|
-
- spec/lib/resterl/
|
142
|
+
- spec/lib/resterl/client_spec.rb
|
143
143
|
- spec/spec_helper.rb
|
144
144
|
homepage: http://github.com/Nix-wie-weg/resterl
|
145
145
|
licenses: []
|
@@ -165,6 +165,6 @@ signing_key:
|
|
165
165
|
specification_version: 4
|
166
166
|
summary: Rudimentary HTTP client with focus on caching
|
167
167
|
test_files:
|
168
|
-
- spec/lib/resterl/
|
168
|
+
- spec/lib/resterl/client_spec.rb
|
169
169
|
- spec/spec_helper.rb
|
170
170
|
has_rdoc:
|