Class: KManager::Resources::WebResource

Inherits:
BaseResource show all
Includes:
KLog::Logging
Defined in:
lib/k_manager/resources/web_resource.rb

Overview

A web resource represents content that is loaded via a web URI.

Web resources do not support watchers and so if you want to handle content changes then you will either need to poll the resource periodically or have a server with open channel for web-hooks.

Constant Summary

Constants inherited from BaseResource

BaseResource::ACTIONS

Instance Attribute Summary

Attributes inherited from BaseResource

#area, #content, #content_type, #documents, #namespace, #status, #uri

Instance Method Summary collapse

Methods inherited from BaseResource

#activated?, #alive?, #attach_document, #content_loaded?, #default_content_type, #documents_loaded?, #documents_preloaded?, #documents_registered?, #fire_action, #fire_next_action, #host, #infer_content_type, #load_document, #new_document, #preload_document, #register_document, #scheme, #source_path, valid_action?

Constructor Details

#initialize(uri, **opts) ⇒ WebResource

Returns a new instance of WebResource.



15
16
17
18
19
# File 'lib/k_manager/resources/web_resource.rb', line 15

def initialize(uri, **opts)
  warn('URI::HTTP/HTTPS type is expected for Web Resource') unless uri.is_a?(URI::HTTP)
  super(uri, **opts)
  log_any_messages unless valid?
end

Instance Method Details

#attribute_values(prefix = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/k_manager/resources/web_resource.rb', line 57

def attribute_values(prefix = nil)
  result = super(prefix)
  result["#{prefix}path".to_sym]          = resource_path
  result["#{prefix}relative_path".to_sym] = resource_relative_path
  result["#{prefix}exist".to_sym]         = resource_valid?
  result
end

#debugObject



65
66
67
68
69
70
71
72
# File 'lib/k_manager/resources/web_resource.rb', line 65

def debug
  super do
    log.kv 'infer_key'        , infer_key         , 20
    log.kv 'url'              , source_path       , 20
    log.kv 'resource_path'    , resource_path     , 20
    log.kv 'resource_valid?'  , resource_valid?   , 20
  end
end

#default_schemeObject



27
28
29
# File 'lib/k_manager/resources/web_resource.rb', line 27

def default_scheme
  :https
end

#infer_keyObject

Infer key is the file name without the extension stored in dash-case



22
23
24
25
# File 'lib/k_manager/resources/web_resource.rb', line 22

def infer_key
  last_segment = uri.path.split('/').last
  Handlebars::Helpers::StringFormatting::Snake.new.parse(last_segment)
end

#load_contentObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/k_manager/resources/web_resource.rb', line 45

def load_content
  if resource_valid?
    begin
      @content = fetch(source_path)
    rescue StandardError => e
      log.error e
    end
  else
    guard("Source url not valid: #{resource_path}")
  end
end

#resource_pathObject



31
32
33
# File 'lib/k_manager/resources/web_resource.rb', line 31

def resource_path
  @resource_path ||= source_path
end

#resource_relative_pathObject



35
36
37
# File 'lib/k_manager/resources/web_resource.rb', line 35

def resource_relative_path
  uri.path
end

#resource_valid?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/k_manager/resources/web_resource.rb', line 39

def resource_valid?
  return @resource_valid if defined? @resource_valid

  @resource_valid = url_exist?(source_path)
end