Class: Webcache::DiskCache
- Inherits:
-
Object
- Object
- Webcache::DiskCache
- Defined in:
- lib/webget/webcache.rb
Instance Method Summary collapse
- #cached?(url) ⇒ Boolean (also: #exist?)
- #read(url) ⇒ Object
- #read_json(url) ⇒ Object
-
#record(url, response, format: 'html') ⇒ Object
add more save / put / etc.
-
#url_to_id(str) ⇒ Object
note: use file path as id for DiskCache (is different for DbCache/SqlCache?) use file:// instead of disk:// - why? why not?.
-
#url_to_path(str) ⇒ Object
helpers.
Instance Method Details
#cached?(url) ⇒ Boolean Also known as: exist?
70 71 72 73 |
# File 'lib/webget/webcache.rb', line 70 def cached?( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" File.exist?( body_path ) end |
#read(url) ⇒ Object
77 78 79 80 |
# File 'lib/webget/webcache.rb', line 77 def read( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" File.open( body_path, 'r:utf-8' ) {|f| f.read } end |
#read_json(url) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/webget/webcache.rb', line 82 def read_json( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" txt = File.open( body_path, 'r:utf-8' ) {|f| f.read } data = JSON.parse( txt ) data end |
#record(url, response, format: 'html') ⇒ Object
add more save / put / etc. aliases - why? why not? rename to record_html - why? why not?
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/webget/webcache.rb', line 93 def record( url, response, format: 'html' ) body_path = "#{Webcache.root}/#{url_to_path( url )}" = "#{body_path}.meta.txt" ## make sure path exits FileUtils.mkdir_p( File.dirname( body_path ) ) puts "[cache] saving #{body_path}..." ## todo/check: verify content-type - why? why not? if format == 'json' File.open( body_path, 'w:utf-8' ) {|f| f.write( JSON.pretty_generate( response.json )) } else ## note - for now always assume utf8!!!!!!!!! File.open( body_path, 'w:utf-8' ) {|f| f.write( response.text ) } end File.open( , 'w:utf-8' ) do |f| response.headers.each do |key, value| # iterate all response headers f.write( "#{key}: #{value}" ) f.write( "\n" ) end end end |
#url_to_id(str) ⇒ Object
note: use file path as id for DiskCache (is different for DbCache/SqlCache?) use file:// instead of disk:// - why? why not?
124 |
# File 'lib/webget/webcache.rb', line 124 def url_to_id( str ) "disk://#{url_to_path( str )}"; end |
#url_to_path(str) ⇒ Object
helpers
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/webget/webcache.rb', line 128 def url_to_path( str ) ## map url to file path uri = URI.parse( str ) ## note: ignore scheme (e.g. http/https) ## and post (e.g. 80, 8080, etc.) for now ## always downcase for now (internet domain is case insensitive) host_dir = uri.host.downcase ## "/this/is/everything?query=params" ## cut-off leading slash and ## convert query ? = req_path = uri.request_uri[1..-1] ### special "prettify" rule for weltfussball ## /eng-league-one-2019-2020/ => /eng-league-one-2019-2020.html if host_dir.index( 'weltfussball.de' ) || host_dir.index( 'worldfootball.net' ) if req_path.end_with?( '/' ) req_path = "#{req_path[0..-2]}.html" else puts "ERROR: expected request_uri for >#{host_dir}< ending with '/'; got: >#{req_path}<" exit 1 end elsif host_dir.index( 'tipp3.at' ) req_path = req_path.sub( '.jsp', '' ) # shorten - cut off .jsp extension ## change ? to -I- ## change = to ~ ## Example: ## sportwetten/classicresults.jsp?oddsetProgramID=888 ## => ## sportwetten/classicresults-I-oddsetProgramID~888 req_path = req_path.gsub( '?', '-I-' ) .gsub( '=', '~') req_path = "#{req_path}.html" elsif host_dir.index( 'football-data.org' ) req_path = req_path.sub( 'v2/', '' ) # shorten - cut off v2/ ## flattern - make a file path - for auto-save ## change ? to -I- ## change / to ~~ ## change = to ~ req_path = req_path.gsub( '?', '-I-' ) .gsub( '/', '~~' ) .gsub( '=', '~') req_path = "#{req_path}.json" else ## no special rule end page_path = "#{host_dir}/#{req_path}" page_path end |