restfulie 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +83 -7
- data/Rakefile +98 -13
- data/lib/restfulie/client/base.rb +48 -53
- data/lib/restfulie/client/configuration.rb +69 -0
- data/lib/restfulie/client/http/adapter.rb +487 -0
- data/lib/restfulie/client/http/atom_ext.rb +87 -0
- data/lib/restfulie/client/http/cache.rb +28 -0
- data/lib/restfulie/client/http/error.rb +80 -0
- data/lib/restfulie/client/http/marshal.rb +147 -0
- data/lib/restfulie/client/http.rb +13 -0
- data/lib/restfulie/client.rb +8 -56
- data/lib/restfulie/common/builder/builder_base.rb +58 -0
- data/lib/restfulie/common/builder/helpers.rb +22 -0
- data/lib/restfulie/common/builder/marshalling/atom.rb +197 -0
- data/lib/restfulie/common/builder/marshalling/base.rb +12 -0
- data/lib/restfulie/common/builder/marshalling/json.rb +2 -0
- data/lib/restfulie/common/builder/marshalling.rb +16 -0
- data/lib/restfulie/common/builder/rules/collection_rule.rb +10 -0
- data/lib/restfulie/common/builder/rules/link.rb +20 -0
- data/lib/restfulie/common/builder/rules/links.rb +9 -0
- data/lib/restfulie/common/builder/rules/member_rule.rb +8 -0
- data/lib/restfulie/common/builder/rules/namespace.rb +25 -0
- data/lib/restfulie/common/builder/rules/rules_base.rb +76 -0
- data/lib/restfulie/common/builder.rb +16 -0
- data/lib/restfulie/common/errors.rb +9 -0
- data/lib/restfulie/{logger.rb → common/logger.rb} +3 -5
- data/lib/restfulie/common/representation/atom.rb +48 -0
- data/lib/restfulie/common/representation/generic.rb +33 -0
- data/lib/restfulie/common/representation/xml.rb +24 -0
- data/lib/restfulie/common/representation.rb +10 -0
- data/lib/restfulie/common.rb +23 -0
- data/lib/restfulie/server/action_controller/base.rb +31 -0
- data/lib/restfulie/server/action_controller/params_parser.rb +62 -0
- data/lib/restfulie/server/action_controller/restful_responder.rb +39 -0
- data/lib/restfulie/server/action_controller/routing/restful_route.rb +14 -0
- data/lib/restfulie/server/action_controller/routing.rb +12 -0
- data/lib/restfulie/server/action_controller.rb +15 -0
- data/lib/restfulie/server/action_view/helpers.rb +45 -0
- data/lib/restfulie/server/action_view/template_handlers/tokamak.rb +15 -0
- data/lib/restfulie/server/action_view/template_handlers.rb +13 -0
- data/lib/restfulie/server/action_view.rb +8 -0
- data/lib/restfulie/server/configuration.rb +21 -0
- data/lib/restfulie/server/core_ext/array.rb +45 -0
- data/lib/restfulie/server/core_ext.rb +1 -0
- data/lib/restfulie/server/restfulie_controller.rb +1 -17
- data/lib/restfulie/server.rb +15 -0
- data/lib/restfulie.rb +4 -72
- data/lib/vendor/atom/configuration.rb +24 -0
- data/lib/vendor/atom/pub.rb +250 -0
- data/lib/vendor/atom/xml/parser.rb +373 -0
- data/lib/vendor/atom.rb +771 -0
- metadata +94 -33
- data/lib/restfulie/client/atom_media_type.rb +0 -75
- data/lib/restfulie/client/cache.rb +0 -103
- data/lib/restfulie/client/entry_point.rb +0 -94
- data/lib/restfulie/client/extensions/http.rb +0 -116
- data/lib/restfulie/client/helper.rb +0 -28
- data/lib/restfulie/client/instance.rb +0 -158
- data/lib/restfulie/client/request_execution.rb +0 -321
- data/lib/restfulie/client/state.rb +0 -36
- data/lib/restfulie/media_type.rb +0 -143
- data/lib/restfulie/media_type_control.rb +0 -115
- data/lib/restfulie/media_type_defaults.rb +0 -51
- data/lib/restfulie/server/atom_media_type.rb +0 -115
- data/lib/restfulie/server/base.rb +0 -91
- data/lib/restfulie/server/controller.rb +0 -122
- data/lib/restfulie/server/instance.rb +0 -102
- data/lib/restfulie/server/marshalling.rb +0 -47
- data/lib/restfulie/server/opensearch/description.rb +0 -54
- data/lib/restfulie/server/opensearch.rb +0 -18
- data/lib/restfulie/server/transition.rb +0 -93
- data/lib/restfulie/unmarshalling.rb +0 -131
- data/lib/vendor/jeokkarak/hashi.rb +0 -65
- data/lib/vendor/jeokkarak/jeokkarak.rb +0 -81
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restfulie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 7
|
8
|
+
- 0
|
9
|
+
version: 0.7.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Guilherme Silveira, Caue Guerra
|
@@ -9,19 +14,51 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-04-12 00:00:00 -03:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
21
|
+
name: actionpack
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 3
|
30
|
+
- 2
|
31
|
+
version: 2.3.2
|
17
32
|
type: :runtime
|
18
|
-
|
19
|
-
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activesupport
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
20
38
|
requirements:
|
21
39
|
- - ">="
|
22
40
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 3
|
44
|
+
- 2
|
45
|
+
version: 2.3.2
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: responders_backport
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
- 1
|
58
|
+
- 0
|
59
|
+
version: 0.1.0
|
60
|
+
type: :runtime
|
61
|
+
version_requirements: *id003
|
25
62
|
description:
|
26
63
|
email: guilherme.silveira@caelum.com.br
|
27
64
|
executables: []
|
@@ -31,33 +68,55 @@ extensions: []
|
|
31
68
|
extra_rdoc_files: []
|
32
69
|
|
33
70
|
files:
|
34
|
-
- lib/restfulie/client/atom_media_type.rb
|
35
71
|
- lib/restfulie/client/base.rb
|
36
|
-
- lib/restfulie/client/
|
37
|
-
- lib/restfulie/client/
|
38
|
-
- lib/restfulie/client/
|
39
|
-
- lib/restfulie/client/
|
40
|
-
- lib/restfulie/client/
|
41
|
-
- lib/restfulie/client/
|
42
|
-
- lib/restfulie/client/
|
72
|
+
- lib/restfulie/client/configuration.rb
|
73
|
+
- lib/restfulie/client/http/adapter.rb
|
74
|
+
- lib/restfulie/client/http/atom_ext.rb
|
75
|
+
- lib/restfulie/client/http/cache.rb
|
76
|
+
- lib/restfulie/client/http/error.rb
|
77
|
+
- lib/restfulie/client/http/marshal.rb
|
78
|
+
- lib/restfulie/client/http.rb
|
43
79
|
- lib/restfulie/client.rb
|
44
|
-
- lib/restfulie/
|
45
|
-
- lib/restfulie/
|
46
|
-
- lib/restfulie/
|
47
|
-
- lib/restfulie/
|
48
|
-
- lib/restfulie/
|
49
|
-
- lib/restfulie/
|
50
|
-
- lib/restfulie/
|
51
|
-
- lib/restfulie/
|
52
|
-
- lib/restfulie/
|
53
|
-
- lib/restfulie/
|
54
|
-
- lib/restfulie/
|
80
|
+
- lib/restfulie/common/builder/builder_base.rb
|
81
|
+
- lib/restfulie/common/builder/helpers.rb
|
82
|
+
- lib/restfulie/common/builder/marshalling/atom.rb
|
83
|
+
- lib/restfulie/common/builder/marshalling/base.rb
|
84
|
+
- lib/restfulie/common/builder/marshalling/json.rb
|
85
|
+
- lib/restfulie/common/builder/marshalling.rb
|
86
|
+
- lib/restfulie/common/builder/rules/collection_rule.rb
|
87
|
+
- lib/restfulie/common/builder/rules/link.rb
|
88
|
+
- lib/restfulie/common/builder/rules/links.rb
|
89
|
+
- lib/restfulie/common/builder/rules/member_rule.rb
|
90
|
+
- lib/restfulie/common/builder/rules/namespace.rb
|
91
|
+
- lib/restfulie/common/builder/rules/rules_base.rb
|
92
|
+
- lib/restfulie/common/builder.rb
|
93
|
+
- lib/restfulie/common/errors.rb
|
94
|
+
- lib/restfulie/common/logger.rb
|
95
|
+
- lib/restfulie/common/representation/atom.rb
|
96
|
+
- lib/restfulie/common/representation/generic.rb
|
97
|
+
- lib/restfulie/common/representation/xml.rb
|
98
|
+
- lib/restfulie/common/representation.rb
|
99
|
+
- lib/restfulie/common.rb
|
100
|
+
- lib/restfulie/server/action_controller/base.rb
|
101
|
+
- lib/restfulie/server/action_controller/params_parser.rb
|
102
|
+
- lib/restfulie/server/action_controller/restful_responder.rb
|
103
|
+
- lib/restfulie/server/action_controller/routing/restful_route.rb
|
104
|
+
- lib/restfulie/server/action_controller/routing.rb
|
105
|
+
- lib/restfulie/server/action_controller.rb
|
106
|
+
- lib/restfulie/server/action_view/helpers.rb
|
107
|
+
- lib/restfulie/server/action_view/template_handlers/tokamak.rb
|
108
|
+
- lib/restfulie/server/action_view/template_handlers.rb
|
109
|
+
- lib/restfulie/server/action_view.rb
|
110
|
+
- lib/restfulie/server/configuration.rb
|
111
|
+
- lib/restfulie/server/core_ext/array.rb
|
112
|
+
- lib/restfulie/server/core_ext.rb
|
55
113
|
- lib/restfulie/server/restfulie_controller.rb
|
56
|
-
- lib/restfulie/server
|
57
|
-
- lib/restfulie/unmarshalling.rb
|
114
|
+
- lib/restfulie/server.rb
|
58
115
|
- lib/restfulie.rb
|
59
|
-
- lib/vendor/
|
60
|
-
- lib/vendor/
|
116
|
+
- lib/vendor/atom/configuration.rb
|
117
|
+
- lib/vendor/atom/pub.rb
|
118
|
+
- lib/vendor/atom/xml/parser.rb
|
119
|
+
- lib/vendor/atom.rb
|
61
120
|
- LICENSE
|
62
121
|
- Rakefile
|
63
122
|
- README.textile
|
@@ -74,18 +133,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
133
|
requirements:
|
75
134
|
- - ">="
|
76
135
|
- !ruby/object:Gem::Version
|
136
|
+
segments:
|
137
|
+
- 0
|
77
138
|
version: "0"
|
78
|
-
version:
|
79
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
140
|
requirements:
|
81
141
|
- - ">="
|
82
142
|
- !ruby/object:Gem::Version
|
143
|
+
segments:
|
144
|
+
- 0
|
83
145
|
version: "0"
|
84
|
-
version:
|
85
146
|
requirements: []
|
86
147
|
|
87
148
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.3.
|
149
|
+
rubygems_version: 1.3.6
|
89
150
|
signing_key:
|
90
151
|
specification_version: 3
|
91
152
|
summary: Hypermedia aware resource based library in ruby (client side) and ruby on rails (server side).
|
@@ -1,75 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
|
20
|
-
# TODO break media type registering for DECODING and ENCODING appart, so we can have two files
|
21
|
-
module Server
|
22
|
-
|
23
|
-
# a decoder from atom an feed
|
24
|
-
class AtomMediaType < Restfulie::MediaType::Type
|
25
|
-
|
26
|
-
def self.from_xml(xml)
|
27
|
-
hash = Hash.from_xml xml
|
28
|
-
AtomFeedDecoded.new(hash.values.first)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
# an atom feed
|
34
|
-
class AtomFeedDecoded < Hashi::CustomHash
|
35
|
-
|
36
|
-
def initialize(hash)
|
37
|
-
super(hash)
|
38
|
-
end
|
39
|
-
|
40
|
-
# retrieves the nth element from an atom feed
|
41
|
-
def [](position)
|
42
|
-
|
43
|
-
hash = entry[position].content.internal_hash
|
44
|
-
hash = hash.dup
|
45
|
-
hash.delete("type")
|
46
|
-
result = Restfulie::MediaType::DefaultMediaTypeDecoder.from_hash(hash)
|
47
|
-
|
48
|
-
add_links_to(result, entry[position]) if entry[position].respond_to?(:link)
|
49
|
-
result
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def add_links_to(result, entry)
|
56
|
-
links = entry.link.internal_hash
|
57
|
-
links = [links] if links.kind_of? Hash
|
58
|
-
self_definition = self_from(links)
|
59
|
-
links << {:rel => "destroy", :method => "delete", :href => self_definition["href"]} unless self_definition.nil?
|
60
|
-
result.add_transitions(links)
|
61
|
-
end
|
62
|
-
|
63
|
-
def self_from(links)
|
64
|
-
links.find do |link|
|
65
|
-
link["rel"] == "self" || link[:rel] == "self"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
Restfulie::MediaType.register(Restfulie::MediaType.rendering_type('application/atom+xml', Server::AtomMediaType))
|
74
|
-
|
75
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
# Basic cache implementation for restfulie.
|
19
|
-
#
|
20
|
-
# It uses the request headers and uri to store it in memory.
|
21
|
-
# This cache might not be optimal for long running clients, which should use a memcached based one.
|
22
|
-
# Use Restfulie.cache_provider to change the provider
|
23
|
-
class Restfulie::BasicCache
|
24
|
-
|
25
|
-
def put(url, req, response)
|
26
|
-
if Restfulie::Cache::Restrictions.may_cache?(req, response)
|
27
|
-
Restfulie.logger.debug "caching #{url} #{req} #{response}"
|
28
|
-
cache[key_for(url, req)] = response
|
29
|
-
end
|
30
|
-
response
|
31
|
-
end
|
32
|
-
|
33
|
-
def get(url, req)
|
34
|
-
|
35
|
-
response = cache[key_for(url, req)]
|
36
|
-
return nil if response.nil?
|
37
|
-
|
38
|
-
if response.has_expired_cache?
|
39
|
-
remove(key_for(url, req))
|
40
|
-
else
|
41
|
-
Restfulie.logger.debug "RETURNING cache #{url} #{req}"
|
42
|
-
response
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
# removes all elements from the cache
|
48
|
-
def clear
|
49
|
-
cache.clear
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def remove(what)
|
55
|
-
@cache.delete(what)
|
56
|
-
nil
|
57
|
-
end
|
58
|
-
|
59
|
-
def cache
|
60
|
-
@cache ||= {}
|
61
|
-
end
|
62
|
-
|
63
|
-
def key_for(url, req)
|
64
|
-
[url, req.class]
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
# Fake cache that does not cache anything
|
70
|
-
# Use Restfulie.cache_provider = Restfulie::FakeCache.new
|
71
|
-
class Restfulie::FakeCache
|
72
|
-
|
73
|
-
def put(url, req, response)
|
74
|
-
response
|
75
|
-
end
|
76
|
-
|
77
|
-
def get(url, req)
|
78
|
-
end
|
79
|
-
|
80
|
-
def clear
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
module Restfulie::Cache
|
86
|
-
module Restrictions
|
87
|
-
|
88
|
-
class << self
|
89
|
-
|
90
|
-
# checks whether this request verb and its cache headers allow caching
|
91
|
-
def may_cache?(request,response)
|
92
|
-
may_cache_method?(request) && response.may_cache?
|
93
|
-
end
|
94
|
-
|
95
|
-
# only Post and Get requests are cacheable so far
|
96
|
-
def may_cache_method?(verb)
|
97
|
-
verb.kind_of?(Net::HTTP::Post) || verb.kind_of?(Net::HTTP::Get)
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
module Client
|
20
|
-
module Base
|
21
|
-
|
22
|
-
# configures an entry point
|
23
|
-
def entry_point_for
|
24
|
-
@entry_points ||= EntryPointControl.new(self)
|
25
|
-
@entry_points
|
26
|
-
end
|
27
|
-
|
28
|
-
# executes a POST request to create this kind of resource at the server
|
29
|
-
def remote_create(content)
|
30
|
-
content = content.to_xml unless content.kind_of? String
|
31
|
-
remote_post content
|
32
|
-
end
|
33
|
-
|
34
|
-
# handles which types of responses should be automatically followed
|
35
|
-
def follows
|
36
|
-
@follower ||= FollowConfig.new
|
37
|
-
@follower
|
38
|
-
end
|
39
|
-
|
40
|
-
# retrieves a resource form a specific uri
|
41
|
-
def from_web(uri, options = {})
|
42
|
-
RequestExecution.new(self, nil).at(uri).get(options)
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
def remote_post(content)
|
47
|
-
RequestExecution.new(self, nil).at(entry_point_for.create.uri).post(content)
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
class FollowConfig
|
53
|
-
def initialize
|
54
|
-
@entries = {
|
55
|
-
:moved_permanently => [:get, :head]
|
56
|
-
}
|
57
|
-
end
|
58
|
-
def method_missing(name, *args)
|
59
|
-
return value_for(name) if name.to_s[-1,1] == "?"
|
60
|
-
set_all_for name
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
def set_all_for(name)
|
65
|
-
@entries[name] = :all
|
66
|
-
end
|
67
|
-
def value_for(name)
|
68
|
-
return @entries[name.to_s.chop.to_sym]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
class EntryPointControl
|
73
|
-
|
74
|
-
def initialize(type)
|
75
|
-
@type = type
|
76
|
-
@entries = {}
|
77
|
-
end
|
78
|
-
|
79
|
-
def method_missing(name, *args)
|
80
|
-
@entries[name] ||= EntryPoint.new
|
81
|
-
@entries[name]
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
class EntryPoint
|
87
|
-
attr_accessor :uri
|
88
|
-
def at(uri)
|
89
|
-
@uri = uri
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
@@ -1,116 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
require 'time'
|
19
|
-
|
20
|
-
# an extesion to http responses
|
21
|
-
module Restfulie::Client::HTTPResponse
|
22
|
-
|
23
|
-
attr_accessor :previous
|
24
|
-
|
25
|
-
# determines if this response code was successful (according to http specs: 200~299)
|
26
|
-
def is_successful?
|
27
|
-
code.to_i >= 200 && code.to_i <= 299
|
28
|
-
end
|
29
|
-
|
30
|
-
# determines if this response code was successful (according to http specs: 100~199)
|
31
|
-
def is_informational?
|
32
|
-
code.to_i >= 100 && code.to_i <= 199
|
33
|
-
end
|
34
|
-
|
35
|
-
# determines if this response code was successful (according to http specs: 300~399)
|
36
|
-
def is_redirection?
|
37
|
-
code.to_i >= 300 && code.to_i <= 399
|
38
|
-
end
|
39
|
-
|
40
|
-
# determines if this response code was successful (according to http specs: 400~499)
|
41
|
-
def is_client_error?
|
42
|
-
code.to_i >= 400 && code.to_i <= 499
|
43
|
-
end
|
44
|
-
|
45
|
-
# determines if this response code was successful (according to http specs: 500~599)
|
46
|
-
def is_server_error?
|
47
|
-
code.to_i >= 500 && code.to_i <= 599
|
48
|
-
end
|
49
|
-
|
50
|
-
def etag
|
51
|
-
self['Etag']
|
52
|
-
end
|
53
|
-
|
54
|
-
def last_modified
|
55
|
-
self['Last-Modified']
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
module Restfulie::Client::HTTPResponse
|
61
|
-
|
62
|
-
def cache_max_age
|
63
|
-
val = header_value_from('Cache-control', /^\s*max-age=(\d+)/)
|
64
|
-
if val
|
65
|
-
val.to_i
|
66
|
-
else
|
67
|
-
0
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def header_value_from(header, expression)
|
72
|
-
h = value_for(get_fields(header)[0], expression)
|
73
|
-
return nil if h.nil?
|
74
|
-
h.match(expression)[1]
|
75
|
-
end
|
76
|
-
|
77
|
-
def has_expired_cache?
|
78
|
-
return true if self['Date'].nil?
|
79
|
-
Time.now > Time.rfc2822(self['Date']) + cache_max_age.seconds
|
80
|
-
end
|
81
|
-
|
82
|
-
# checks if the header's max-age is available and no no-store if available.
|
83
|
-
def may_cache?
|
84
|
-
may_cache_field?(get_fields('Cache-control'))
|
85
|
-
end
|
86
|
-
|
87
|
-
def may_cache_field?(field)
|
88
|
-
return false if field.nil?
|
89
|
-
|
90
|
-
if field.kind_of? Array
|
91
|
-
field.each do |f|
|
92
|
-
return false if !may_cache_field?(f)
|
93
|
-
end
|
94
|
-
return true
|
95
|
-
end
|
96
|
-
|
97
|
-
max_age_header = value_for(field, /^max-age=(\d+)/)
|
98
|
-
return false if max_age_header.nil?
|
99
|
-
max_age = max_age_header[1]
|
100
|
-
|
101
|
-
return false if value_for(field, /^no-store/)
|
102
|
-
|
103
|
-
true
|
104
|
-
end
|
105
|
-
|
106
|
-
# extracts the header value for an specific expression, which can be located at the start or in the middle
|
107
|
-
# of the expression
|
108
|
-
def value_for(value, expression)
|
109
|
-
value.split(",").find { |obj| obj.strip =~ expression }
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
class Net::HTTPResponse
|
115
|
-
include Restfulie::Client::HTTPResponse
|
116
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
|
3
|
-
# All rights reserved.
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Restfulie
|
19
|
-
module Client
|
20
|
-
module Helper
|
21
|
-
# retrieves the invoking method's name
|
22
|
-
def self.current_method
|
23
|
-
caller[0]=~/`(.*?)'/
|
24
|
-
$1
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|