Class: Rack::Cachely::Store
- Inherits:
-
Object
- Object
- Rack::Cachely::Store
- Includes:
- Singleton
- Defined in:
- lib/rack-cachely/store.rb
Class Method Summary collapse
Instance Method Summary collapse
- #config ⇒ Object
- #delete(pattern) ⇒ Object
- #get(key) ⇒ Object
- #logger ⇒ Object
- #post(key, rack, options = {}) ⇒ Object
- #remote(&block) ⇒ Object
Class Method Details
.method_missing(sym, *args, &block) ⇒ Object
6 7 8 |
# File 'lib/rack-cachely/store.rb', line 6 def self.method_missing(sym, *args, &block) self.instance.send(sym, *args, &block) end |
Instance Method Details
#config ⇒ Object
80 81 82 |
# File 'lib/rack-cachely/store.rb', line 80 def config Rack::Cachely.config end |
#delete(pattern) ⇒ Object
67 68 69 |
# File 'lib/rack-cachely/store.rb', line 67 def delete(pattern) Net::HTTP.post_form(URI(config.cachely_url), {_method: "DELETE", pattern: pattern}) end |
#get(key) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rack-cachely/store.rb', line 10 def get(key) remote do url = "#{config.cachely_url}?_version=#{Rack::Cachely::VERSION}&url=#{CGI.escape(key.to_s)}" if config.verbose logger.debug "Cachely [URL]: #{url}" end uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) matches = url.scan(/\/\/(.+)@/).flatten if matches.any? u, p = matches.first.split(":") request.basic_auth(u, p) end response = http.request(request) if config.verbose logger.info "Cachely [uri]: #{uri}" logger.info "Cachely [response.code]: #{response.code}" logger.info "Cachely [response.body]: #{response.body}" end if (200...300).include?(response.code.to_i) headers = {} response.each_header do |key, value| headers[key] = value end return [response.code.to_i, headers, StringIO.new(response.body)] end end return nil end |
#logger ⇒ Object
84 85 86 |
# File 'lib/rack-cachely/store.rb', line 84 def logger Rack::Cachely.config.logger end |
#post(key, rack, options = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rack-cachely/store.rb', line 42 def post(key, rack, = {}) remote do body = rack[2].respond_to?(:body) ? rack[2].body : rack[2] data = { "document[url]" => key, "document[status]" => rack[0].to_i, "document[body]" => body, "document[age]" => [:age] || 0, "_version" => Rack::Cachely::VERSION } rack[1].each do |key, value| data["document[headers][#{key}]"] = value end if config.verbose logger.debug "Cachely [data]: #{data.inspect}" end response = Net::HTTP.post_form(URI(config.cachely_url), data) if config.verbose logger.debug "Cachely [response.code]: #{response.code}" logger.debug "Cachely [response.body]: #{response.body}" end end return true end |
#remote(&block) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/rack-cachely/store.rb', line 71 def remote(&block) begin ::Timeout::timeout(config.timeout, &block) rescue ::Timeout::Error, Errno::ECONNREFUSED => e # raise e logger.error(e) end end |