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_csv(url) ⇒ Object
- #read_json(url) ⇒ Object
-
#record(url, response, encoding: 'UTF-8', 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?
72 73 74 75 |
# File 'lib/webget/webcache.rb', line 72 def cached?( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" File.exist?( body_path ) end |
#read(url) ⇒ Object
79 80 81 82 |
# File 'lib/webget/webcache.rb', line 79 def read( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" File.open( body_path, 'r:utf-8' ) {|f| f.read } end |
#read_csv(url) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/webget/webcache.rb', line 91 def read_csv( url ) body_path = "#{Webcache.root}/#{url_to_path( url )}" txt = File.open( body_path, 'r:utf-8' ) {|f| f.read } data = CsvHash.parse( txt ) data end |
#read_json(url) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/webget/webcache.rb', line 84 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, encoding: 'UTF-8', format: 'html') ⇒ Object
add more save / put / etc. aliases - why? why not?
rename to record_html - why? why not?
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/webget/webcache.rb', line 101 def record( url, response, encoding: 'UTF-8', 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? ## note - for now respone.text always assume (converted) to utf8!!!!!!!!! if format == 'json' File.open( body_path, 'w:utf-8' ) {|f| f.write( JSON.pretty_generate( response.json )) } elsif format == 'csv' ## fix: newlines - always use "unix" style" - why? why not? text = response.text( encoding: encoding ).gsub( "\r\n", "\n" ) File.open( body_path, 'w:utf-8' ) {|f| f.write( text ) } else text = response.text( encoding: encoding ) File.open( body_path, 'w:utf-8' ) {|f| f.write( text ) } end File.open( , 'w:utf-8' ) do |f| ## todo/check: ## do headers also need to converted (like text) if encoding is NOT utf-8 ??? 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?
140 |
# File 'lib/webget/webcache.rb', line 140 def url_to_id( str ) "disk://#{url_to_path( str )}"; end |
#url_to_path(str) ⇒ Object
helpers
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/webget/webcache.rb', line 144 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.co.uk' ) req_path = req_path.sub( 'mmz4281/', '' ) # shorten - cut off mmz4281/ req_path = req_path.sub( 'new/', '' ) # shorten - cut off new/ 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 |