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.



13
14
15
16
17
# File 'lib/k_manager/resources/web_resource.rb', line 13

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



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

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



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

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



25
26
27
# File 'lib/k_manager/resources/web_resource.rb', line 25

def default_scheme
  :https
end

#infer_keyObject

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



20
21
22
23
# File 'lib/k_manager/resources/web_resource.rb', line 20

def infer_key
  last_segment = uri.path.split('/').last
  Cmdlet::Case::Snake.new.call(last_segment)
end

#load_contentObject



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

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



29
30
31
# File 'lib/k_manager/resources/web_resource.rb', line 29

def resource_path
  @resource_path ||= source_path
end

#resource_relative_pathObject



33
34
35
# File 'lib/k_manager/resources/web_resource.rb', line 33

def resource_relative_path
  uri.path
end

#resource_valid?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/k_manager/resources/web_resource.rb', line 37

def resource_valid?
  return @resource_valid if defined? @resource_valid

  @resource_valid = url_exist?(source_path)
end