resterl 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/lib/resterl/caches/cache_interface.rb +15 -9
- data/lib/resterl/client.rb +109 -102
- data/lib/resterl/version.rb +1 -1
- metadata +7 -16
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NTEyZGFhZmRlNTc1ZTQxZmVhODliZTYxNjkzYTExMDA2OGFhYzExNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTBiY2I0NTJlNzFhZjcxZGMxYTA0Nzc3ODZjYzQ3MTQxNjA3OTNhYg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDRiOWMwYTgyNWU2MWUyOTEwZDRjYjgwMjhjNmRkNThhZmMzZTY0MWNlYzE5
|
10
|
+
ZjkwODhhOGRhOTQyMmIwYjU3OTJiYzAzYzgxODE4ZjRkOGM3MzE5ZmRmNmFj
|
11
|
+
Yzg0MTcwNTZlNDBkZmEyZmExMmI0NDMxNzI0YjRlOGIzYTY2OGQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZmYxOTg1NWYwZGY2OGI2MTE4ZGYzNjE4NTNjZmU0ODU1M2YwZGRhYzZkMDA5
|
14
|
+
MTQ2ZGNkM2IwZGEzZTgzZTk4ZjkyZTQ4Nzk5MGQyMDVlNzc5MWNlYTU0MzRm
|
15
|
+
NTBkZGVjMTY1NzBhZDRkNzJlNThiMjE5ZGJkYTBiZDFhYmI0YzQ=
|
@@ -1,11 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Resterl
|
2
|
+
module Caches
|
3
|
+
class CacheInterface
|
4
|
+
def read key
|
5
|
+
raise 'Not yet implemented!'
|
6
|
+
end
|
7
|
+
|
8
|
+
def write key, value, expires_in
|
9
|
+
raise 'Not yet implemented!'
|
10
|
+
end
|
11
|
+
|
12
|
+
def delete key
|
13
|
+
raise 'Not yet implemented!'
|
14
|
+
end
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
data/lib/resterl/client.rb
CHANGED
@@ -1,120 +1,127 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
:
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
module Resterl
|
4
|
+
class Client
|
5
|
+
attr_reader :options
|
6
|
+
DEFAULTS = {
|
7
|
+
max_redirect_depth: 10,
|
8
|
+
cache: Resterl::Caches::SimpleCache.new,
|
9
|
+
ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE,
|
10
|
+
expiry_multiplier: 10,
|
11
|
+
minimum_cache_lifetime: 5 * 60 # 5 Minuten
|
12
|
+
}
|
13
|
+
|
14
|
+
class TooManyRedirects < StandardError; end
|
15
|
+
|
16
|
+
def initialize(options = {})
|
17
|
+
@options = DEFAULTS.merge(options).freeze
|
18
|
+
@cache = @options[:cache]
|
19
|
+
end
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
def get url, params, headers
|
22
|
+
url = setup_url(url)
|
20
23
|
|
21
|
-
|
22
|
-
|
24
|
+
# Cache-Schlüssel aus Pfad hashen
|
25
|
+
cache_key = data_to_cache_key url, params, headers
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
# Response aus Cache holen
|
28
|
+
old_response = @cache.read(cache_key)
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
if old_response && !old_response.expired?
|
31
|
+
# Anfrage noch nicht abgelaufen, Ergebnis aus dem Cache verwenden
|
32
|
+
old_response
|
33
|
+
else
|
34
|
+
# Neue Anfrage
|
35
|
+
new_get_request url, cache_key, params, headers, old_response
|
36
|
+
end
|
33
37
|
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def post url, params, data, headers
|
37
|
-
# Caching nicht notwendig
|
38
|
-
url = setup_url url
|
39
|
-
request = Resterl::PostRequest.new(self, url, params, data, headers)
|
40
|
-
response = request.perform.response
|
41
|
-
|
42
|
-
response
|
43
|
-
end
|
44
|
-
|
45
|
-
def put url, params, data, headers
|
46
|
-
# TODO: Testen, durchdenken, refactoring?
|
47
|
-
url = setup_url url
|
48
|
-
request = Resterl::PutRequest.new(self, url, params, data, headers)
|
49
|
-
request.perform.response
|
50
|
-
end
|
51
|
-
|
52
|
-
def delete url, params, data, headers
|
53
|
-
# TODO: Testen, durchdenken, refactoring?
|
54
|
-
url = setup_url url
|
55
|
-
request = Resterl::DeleteRequest.new(self, url, params, data, headers)
|
56
|
-
request.perform.response
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
38
|
|
61
|
-
|
39
|
+
def post url, params, data, headers
|
40
|
+
# Caching nicht notwendig
|
41
|
+
url = setup_url url
|
42
|
+
request = Resterl::PostRequest.new(self, url, params, data, headers)
|
43
|
+
response = request.perform.response
|
62
44
|
|
63
|
-
|
64
|
-
if old_response
|
65
|
-
etag = old_response.net_http_response['ETag']
|
66
|
-
headers['If-None-Match'] = etag if etag
|
67
|
-
|
68
|
-
last_modified = old_response.net_http_response['Last-Modified']
|
69
|
-
headers['If-Modified-Since'] = last_modified if last_modified
|
45
|
+
response
|
70
46
|
end
|
71
47
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
when Net::HTTPClientError, Net::HTTPServerError
|
79
|
-
# Aus dem Cache muss nichts entfernt werden, weil ja auch kein Eintrag
|
80
|
-
# (mehr) drin ist.
|
81
|
-
new_response.error!
|
82
|
-
when Net::HTTPNotModified
|
83
|
-
# Wenn "304 Not Modified", dann altes Ergebnis als neues Ergebnis
|
84
|
-
# verwenden
|
85
|
-
r_temp = Resterl::Response.new(new_response)
|
86
|
-
old_response.update_expires_at(r_temp.expires_at)
|
87
|
-
[old_response, r_temp.expires_at - Time.now]
|
88
|
-
|
89
|
-
when Net::HTTPSuccess
|
90
|
-
r = Resterl::Response.new(new_response)
|
91
|
-
[r, r.expires_at - Time.now]
|
92
|
-
else
|
93
|
-
raise 'unknown response'
|
48
|
+
def put url, params, data, headers
|
49
|
+
# TODO: Testen, durchdenken, refactoring?
|
50
|
+
url = setup_url url
|
51
|
+
request = Resterl::PutRequest.new(self, url, params, data, headers)
|
52
|
+
request.perform.response
|
94
53
|
end
|
95
54
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
55
|
+
def delete url, params, data, headers
|
56
|
+
# TODO: Testen, durchdenken, refactoring?
|
57
|
+
url = setup_url url
|
58
|
+
request = Resterl::DeleteRequest.new(self, url, params, data, headers)
|
59
|
+
request.perform.response
|
60
|
+
end
|
101
61
|
|
102
|
-
|
103
|
-
|
62
|
+
def invalidate url, params, headers
|
63
|
+
@cache.delete data_to_cache_key(url, params, headers)
|
64
|
+
end
|
104
65
|
|
105
|
-
|
106
|
-
|
66
|
+
private
|
67
|
+
|
68
|
+
def new_get_request url, cache_key, params, headers, old_response
|
69
|
+
# Ggf. ETag und Last-Modified auslesen
|
70
|
+
if old_response
|
71
|
+
etag = old_response.net_http_response['ETag']
|
72
|
+
headers['If-None-Match'] = etag if etag
|
73
|
+
|
74
|
+
last_modified = old_response.net_http_response['Last-Modified']
|
75
|
+
headers['If-Modified-Since'] = last_modified if last_modified
|
76
|
+
end
|
77
|
+
|
78
|
+
# Anfrage stellen, ggf. ETag mit übergeben
|
79
|
+
request = Resterl::GetRequest.new(self, url, params, headers)
|
80
|
+
new_response = request.perform.response
|
81
|
+
|
82
|
+
response, max_age_seconds = case new_response
|
83
|
+
when Net::HTTPClientError,
|
84
|
+
Net::HTTPServerError
|
85
|
+
# Aus dem Cache muss nichts entfernt werden,
|
86
|
+
# weil ja auch kein Eintrag (mehr) drin ist.
|
87
|
+
new_response.error!
|
88
|
+
when Net::HTTPNotModified
|
89
|
+
# Wenn "304 Not Modified", dann altes
|
90
|
+
# Ergebnis als neues Ergebnis verwenden
|
91
|
+
r_temp = Resterl::Response.new(new_response)
|
92
|
+
old_response.update_expires_at(
|
93
|
+
r_temp.expires_at)
|
94
|
+
[old_response, r_temp.expires_at - Time.now]
|
95
|
+
when Net::HTTPSuccess
|
96
|
+
r = Resterl::Response.new(new_response)
|
97
|
+
[r, r.expires_at - Time.now]
|
98
|
+
else
|
99
|
+
raise 'unknown response'
|
100
|
+
end
|
101
|
+
|
102
|
+
# Cachezeit berechnen
|
103
|
+
expiry = [
|
104
|
+
max_age_seconds.to_i * options[:expiry_multiplier],
|
105
|
+
options[:minimum_cache_lifetime]
|
106
|
+
].max
|
107
|
+
|
108
|
+
# Ergebnis im Cache speichern
|
109
|
+
@cache.write cache_key, response, expiry
|
110
|
+
|
111
|
+
response
|
112
|
+
end
|
107
113
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
+
def setup_url url
|
115
|
+
if url =~ /^http/
|
116
|
+
url
|
117
|
+
else
|
118
|
+
bu = options[:base_uri]
|
119
|
+
bu ? "#{bu}#{url}" : url
|
120
|
+
end
|
114
121
|
end
|
115
|
-
end
|
116
122
|
|
117
|
-
|
118
|
-
|
123
|
+
def data_to_cache_key *args
|
124
|
+
args.hash
|
125
|
+
end
|
119
126
|
end
|
120
127
|
end
|
data/lib/resterl/version.rb
CHANGED
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resterl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.13
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Florian Dütsch
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-10-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: hashie
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: yajl-ruby
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: shoulda
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -94,28 +87,26 @@ files:
|
|
94
87
|
- test/test_resterl.rb
|
95
88
|
homepage: http://github.com/Nix-wie-weg/resterl
|
96
89
|
licenses: []
|
90
|
+
metadata: {}
|
97
91
|
post_install_message:
|
98
92
|
rdoc_options: []
|
99
93
|
require_paths:
|
100
94
|
- lib
|
101
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
96
|
requirements:
|
104
97
|
- - ! '>='
|
105
98
|
- !ruby/object:Gem::Version
|
106
99
|
version: '0'
|
107
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
101
|
requirements:
|
110
102
|
- - ! '>='
|
111
103
|
- !ruby/object:Gem::Version
|
112
104
|
version: '0'
|
113
105
|
requirements: []
|
114
106
|
rubyforge_project:
|
115
|
-
rubygems_version:
|
107
|
+
rubygems_version: 2.2.2
|
116
108
|
signing_key:
|
117
|
-
specification_version:
|
109
|
+
specification_version: 4
|
118
110
|
summary: Rudimentary HTTP client with focus on caching
|
119
|
-
test_files:
|
120
|
-
|
121
|
-
- test/test_resterl.rb
|
111
|
+
test_files: []
|
112
|
+
has_rdoc:
|