Class: Goodcheck::ImportLoader
- Inherits:
-
Object
- Object
- Goodcheck::ImportLoader
- Defined in:
- lib/goodcheck/import_loader.rb
Defined Under Namespace
Classes: UnexpectedSchemaError
Instance Attribute Summary collapse
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#force_download ⇒ Object
readonly
Returns the value of attribute force_download.
Instance Method Summary collapse
- #cache_name(uri) ⇒ Object
-
#initialize(cache_path:, expires_in: 3 * 60, force_download:, config_path:) ⇒ ImportLoader
constructor
A new instance of ImportLoader.
- #load(name, &block) ⇒ Object
- #load_file(uri) ⇒ Object
- #load_http(uri) ⇒ Object
- #write_cache(uri, content) ⇒ Object
Constructor Details
#initialize(cache_path:, expires_in: 3 * 60, force_download:, config_path:) ⇒ ImportLoader
Returns a new instance of ImportLoader.
16 17 18 19 20 21 |
# File 'lib/goodcheck/import_loader.rb', line 16 def initialize(cache_path:, expires_in: 3 * 60, force_download:, config_path:) @cache_path = cache_path @expires_in = expires_in @force_download = force_download @config_path = config_path end |
Instance Attribute Details
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
11 12 13 |
# File 'lib/goodcheck/import_loader.rb', line 11 def cache_path @cache_path end |
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
14 15 16 |
# File 'lib/goodcheck/import_loader.rb', line 14 def config_path @config_path end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
12 13 14 |
# File 'lib/goodcheck/import_loader.rb', line 12 def expires_in @expires_in end |
#force_download ⇒ Object (readonly)
Returns the value of attribute force_download.
13 14 15 |
# File 'lib/goodcheck/import_loader.rb', line 13 def force_download @force_download end |
Instance Method Details
#cache_name(uri) ⇒ Object
44 45 46 |
# File 'lib/goodcheck/import_loader.rb', line 44 def cache_name(uri) Digest::SHA2.hexdigest(uri.to_s) end |
#load(name, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/goodcheck/import_loader.rb', line 23 def load(name, &block) uri = URI.parse(name) case uri.scheme when nil, "file" load_file uri, &block when "http", "https" load_http uri, &block else raise UnexpectedSchemaError.new("Unexpected URI schema: #{uri.class.name}") end end |
#load_file(uri) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/goodcheck/import_loader.rb', line 36 def load_file(uri) path = (config_path.parent + uri.path) begin yield path.read end end |
#load_http(uri) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/goodcheck/import_loader.rb', line 48 def load_http(uri) hash = cache_name(uri) path = cache_path + hash Goodcheck.logger.info "Calculated cache name: #{hash}" download = false if force_download Goodcheck.logger.debug "Downloading: force flag" download = true end if !download && !path.file? Goodcheck.logger.debug "Downloading: no cache found" download = true end if !download && path.mtime + expires_in < Time.now Goodcheck.logger.debug "Downloading: cache expired" download = true end if download path.rmtree if path.exist? Goodcheck.logger.info "Downloading content..." content = HTTPClient.new.get_content(uri) Goodcheck.logger.debug "Downloaded content: #{content[0, 1024].inspect}#{content.size > 1024 ? "..." : ""}" yield content write_cache uri, content else Goodcheck.logger.info "Reading content from cache..." yield path.read end end |
#write_cache(uri, content) ⇒ Object
84 85 86 87 |
# File 'lib/goodcheck/import_loader.rb', line 84 def write_cache(uri, content) path = cache_path + cache_name(uri) path.write(content) end |