web 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/web.rb +7 -2
- data/lib/web/ext/net_http.rb +21 -1
- data/lib/web/ext/typhoeus.rb +64 -0
- data/lib/web/faker.rb +7 -0
- data/lib/web/http_response.rb +0 -11
- data/lib/web/version.rb +1 -1
- data/spec/spec_helper.rb +6 -1
- metadata +24 -14
data/lib/web.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/web/ext/net_http'
|
1
|
+
require File.dirname(__FILE__) + '/web/ext/net_http' if defined?(Net::HTTP)
|
2
|
+
require File.dirname(__FILE__) + '/web/ext/typhoeus' if defined?(Typhoeus)
|
2
3
|
require File.dirname(__FILE__) + '/web/faker'
|
3
4
|
|
4
5
|
module Web
|
@@ -19,6 +20,10 @@ module Web
|
|
19
20
|
registered << options
|
20
21
|
end
|
21
22
|
|
23
|
+
def unregister_all
|
24
|
+
@registered = []
|
25
|
+
end
|
26
|
+
|
22
27
|
# an array of registrations
|
23
28
|
def registered
|
24
29
|
@registered ||= []
|
@@ -26,7 +31,7 @@ module Web
|
|
26
31
|
|
27
32
|
# Get the cache we're using
|
28
33
|
def cache
|
29
|
-
@cache ||=
|
34
|
+
@cache ||= MemoryCache.new
|
30
35
|
end
|
31
36
|
|
32
37
|
end
|
data/lib/web/ext/net_http.rb
CHANGED
@@ -104,8 +104,28 @@ module Net
|
|
104
104
|
protocol = use_ssl? ? 'https' : 'http'
|
105
105
|
path = request.path
|
106
106
|
path = URI.parse(request.path).request_uri if request.path =~ /^http/
|
107
|
+
path = path.chop if path.end_with?('/') # remove trailing slashes in caching layer
|
107
108
|
# TODO handle basic auth
|
108
|
-
|
109
|
+
if (use_ssl? && port == 443) || (!use_ssl? && port == 80)
|
110
|
+
"#{protocol}://#{address}#{path}"
|
111
|
+
else
|
112
|
+
"#{protocol}://#{address}:#{port}#{path}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
module Web
|
121
|
+
|
122
|
+
module ReadableHTTPResponse
|
123
|
+
|
124
|
+
include HTTPResponse
|
125
|
+
|
126
|
+
def read_body(dest = nil, &block)
|
127
|
+
yield @body if block_given?
|
128
|
+
@body
|
109
129
|
end
|
110
130
|
|
111
131
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../http_response'
|
4
|
+
|
5
|
+
module Typhoeus
|
6
|
+
|
7
|
+
class Response
|
8
|
+
|
9
|
+
def cached?
|
10
|
+
!!@cached
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Request
|
16
|
+
|
17
|
+
# A dummy cache timeout so that our cache always
|
18
|
+
# gets called
|
19
|
+
def cache_timeout
|
20
|
+
1
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Hydra
|
26
|
+
|
27
|
+
# Thank you to the creators of Typhoeus for making this so easy
|
28
|
+
# to do.
|
29
|
+
|
30
|
+
def initialize_with_web(options = {})
|
31
|
+
initialize_without_web(options)
|
32
|
+
# On cache set, record this if we should
|
33
|
+
cache_setter do |req|
|
34
|
+
web = Web::Faker.new req.method, req.url, req.body, req.headers
|
35
|
+
if web.desired?
|
36
|
+
res = req.response
|
37
|
+
web.record res.code, res.body, res.headers_hash
|
38
|
+
end
|
39
|
+
end
|
40
|
+
# On cache get, reconstruct the response and send it back
|
41
|
+
cache_getter do |req|
|
42
|
+
web = Web::Faker.new req.method, req.url, req.body, req.headers
|
43
|
+
if web.desired? && web_response = web.response_for
|
44
|
+
# put the headers back together, since we cache them as a dict
|
45
|
+
h_str = web_response.headers ?
|
46
|
+
web_response.headers.map { |k, v| "#{k}: #{v}" }.join("\n") : ''
|
47
|
+
# and then reconstruct the response
|
48
|
+
response = Typhoeus::Response.new({
|
49
|
+
:code => web_response.code,
|
50
|
+
:headers => h_str,
|
51
|
+
:body => web_response.body
|
52
|
+
})
|
53
|
+
response.instance_variable_set :@cached, true
|
54
|
+
response
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
alias_method :initialize_without_web, :initialize
|
60
|
+
alias_method :initialize, :initialize_with_web
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/lib/web/faker.rb
CHANGED
@@ -7,6 +7,10 @@ module Web
|
|
7
7
|
|
8
8
|
attr_reader :cache, :key
|
9
9
|
|
10
|
+
# method is expected to be a symbol, downcase
|
11
|
+
# url is expected to be a full url, trailing slash chopped off
|
12
|
+
# body is expected to be nil or a string
|
13
|
+
# headers is expected to be a hash
|
10
14
|
def initialize(method, url, body, headers)
|
11
15
|
@key = "#{method}:#{url}"
|
12
16
|
@cache = Web.cache
|
@@ -24,6 +28,9 @@ module Web
|
|
24
28
|
end
|
25
29
|
|
26
30
|
# Given a response, marshall down and record in redis
|
31
|
+
# code is expected to be Fixnum
|
32
|
+
# body is expected to be a string or nil
|
33
|
+
# headers is expected to be a hash
|
27
34
|
def record(code, body, headers)
|
28
35
|
# save and return the response
|
29
36
|
res = Web::Response.new code, body, headers
|
data/lib/web/http_response.rb
CHANGED
data/lib/web/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-08-31 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: rspec
|
17
|
-
requirement: &
|
16
|
+
requirement: &17525600 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,25 +21,36 @@ dependencies:
|
|
22
21
|
version: '0'
|
23
22
|
type: :development
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *17525600
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: typhoeus
|
27
|
+
requirement: &17525180 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *17525180
|
26
36
|
description: web is a library for caching HTTP responses
|
27
37
|
email: john.crepezzi@gmail.com
|
28
38
|
executables: []
|
29
39
|
extensions: []
|
30
40
|
extra_rdoc_files: []
|
31
41
|
files:
|
32
|
-
- lib/web
|
33
|
-
- lib/web/
|
34
|
-
- lib/web/
|
42
|
+
- lib/web.rb
|
43
|
+
- lib/web/response.rb
|
44
|
+
- lib/web/ext/typhoeus.rb
|
35
45
|
- lib/web/ext/net_http.rb
|
46
|
+
- lib/web/version.rb
|
47
|
+
- lib/web/status_codes.rb
|
36
48
|
- lib/web/faker.rb
|
37
49
|
- lib/web/http_response.rb
|
38
|
-
- lib/web/
|
39
|
-
- lib/web/
|
40
|
-
- lib/web/
|
41
|
-
- lib/web.rb
|
50
|
+
- lib/web/cache/memory_cache.rb
|
51
|
+
- lib/web/cache/memcached_cache.rb
|
52
|
+
- lib/web/cache/redis_cache.rb
|
42
53
|
- spec/spec_helper.rb
|
43
|
-
has_rdoc: true
|
44
54
|
homepage: http://github.com/seejohnrun/web
|
45
55
|
licenses: []
|
46
56
|
post_install_message:
|
@@ -61,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
71
|
version: '0'
|
62
72
|
requirements: []
|
63
73
|
rubyforge_project: web
|
64
|
-
rubygems_version: 1.
|
74
|
+
rubygems_version: 1.8.10
|
65
75
|
signing_key:
|
66
76
|
specification_version: 3
|
67
77
|
summary: cache HTTP responses
|