rack-cachely 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/rack-cachely/config.rb +1 -2
- data/lib/rack-cachely/store.rb +13 -5
- data/lib/rack-cachely/version.rb +1 -1
- data/spec/rack-cachely/store_spec.rb +9 -5
- data/spec/spec_helper.rb +3 -1
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/lib/rack-cachely/config.rb
CHANGED
@@ -6,8 +6,7 @@ module Rack
|
|
6
6
|
|
7
7
|
def initialize(options = {})
|
8
8
|
self.options = options
|
9
|
-
self.options[:
|
10
|
-
self.options[:cachely_url] = "#{(self.options[:cachely_url] ||= ENV["CACHELY_URL"])}/v1/cache?_token=#{self.options[:cachely_api_key]}"
|
9
|
+
self.options[:cachely_url] = "#{(self.options[:cachely_url] ||= ENV["CACHELY_URL"])}/v1/cache"
|
11
10
|
end
|
12
11
|
|
13
12
|
def method_missing(sym, *args, &block)
|
data/lib/rack-cachely/store.rb
CHANGED
@@ -9,8 +9,17 @@ module Rack
|
|
9
9
|
|
10
10
|
def get(key)
|
11
11
|
remote do
|
12
|
-
|
13
|
-
|
12
|
+
url = "#{config.cachely_url}?url=#{CGI.escape(key.to_s)}"
|
13
|
+
uri = URI(url)
|
14
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
15
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
16
|
+
matches = url.scan(/\/\/(.+)@/).flatten
|
17
|
+
|
18
|
+
if matches.any?
|
19
|
+
u, p = matches.first.split(":")
|
20
|
+
request.basic_auth(u, p)
|
21
|
+
end
|
22
|
+
response = http.request(request)
|
14
23
|
if config.verbose
|
15
24
|
logger.info "Cachely [uri]: #{uri}"
|
16
25
|
logger.info "Cachely [response.code]: #{response.code}"
|
@@ -34,8 +43,7 @@ module Rack
|
|
34
43
|
"document[url]" => key,
|
35
44
|
"document[status]" => rack[0].to_i,
|
36
45
|
"document[body]" => body,
|
37
|
-
"document[age]" => options[:age] || 0
|
38
|
-
"_token" => Rack::Cachely.config.cachely_api_key
|
46
|
+
"document[age]" => options[:age] || 0
|
39
47
|
}
|
40
48
|
rack[1].each do |key, value|
|
41
49
|
data["document[headers][#{key}]"] = value
|
@@ -53,7 +61,7 @@ module Rack
|
|
53
61
|
end
|
54
62
|
|
55
63
|
def delete(pattern)
|
56
|
-
Net::HTTP.post_form(URI(config.cachely_url), {_method: "DELETE", pattern: pattern
|
64
|
+
Net::HTTP.post_form(URI(config.cachely_url), {_method: "DELETE", pattern: pattern})
|
57
65
|
end
|
58
66
|
|
59
67
|
def remote(&block)
|
data/lib/rack-cachely/version.rb
CHANGED
@@ -8,9 +8,14 @@ describe Rack::Cachely::Store do
|
|
8
8
|
describe "get" do
|
9
9
|
|
10
10
|
it "retreives a page from the service" do
|
11
|
-
|
12
|
-
|
13
|
-
Net::HTTP.
|
11
|
+
response_mock = double(code: "200", body: "hello!")
|
12
|
+
response_mock.stub!(:each_header).and_yield("Content-Type", "text/html")
|
13
|
+
Net::HTTP::Get.should_receive(:new).with("/v1/cache?url=http%3A%2F%2Fexample.com").and_return {
|
14
|
+
m = double
|
15
|
+
m.should_receive(:basic_auth).with("1234567890", nil)
|
16
|
+
m
|
17
|
+
}
|
18
|
+
Net::HTTP.should_receive(:new).with("www.cachelyapp.com", 80).and_return(double(request: response_mock))
|
14
19
|
|
15
20
|
res = store.get(key)
|
16
21
|
res[0].should eql(200)
|
@@ -42,8 +47,7 @@ describe Rack::Cachely::Store do
|
|
42
47
|
"document[status]" => 200,
|
43
48
|
"document[body]" => "hello!",
|
44
49
|
"document[age]" => 30,
|
45
|
-
"document[headers][Content-Type]"=>"text/html"
|
46
|
-
"_token" => Rack::Cachely.config.cachely_api_key
|
50
|
+
"document[headers][Content-Type]" => "text/html"
|
47
51
|
}
|
48
52
|
end
|
49
53
|
|
data/spec/spec_helper.rb
CHANGED
@@ -6,12 +6,14 @@ require 'rack-cachely' # and any other gems you need
|
|
6
6
|
require 'active_support/core_ext'
|
7
7
|
require 'vcr'
|
8
8
|
|
9
|
+
ENV["CACHELY_URL"] = "http://1234567890@www.cachelyapp.com"
|
10
|
+
|
9
11
|
Dir[File.expand_path(File.join("support", "**", "*.rb"), File.dirname(__FILE__))].each {|f| require f}
|
10
12
|
|
11
13
|
RSpec.configure do |config|
|
12
14
|
|
13
15
|
config.before do
|
14
|
-
Rack::Cachely.config = Rack::Cachely::Config.new
|
16
|
+
Rack::Cachely.config = Rack::Cachely::Config.new
|
15
17
|
end
|
16
18
|
|
17
19
|
end
|