Class: Archaeo::AssetList
- Inherits:
-
Object
- Object
- Archaeo::AssetList
- Includes:
- Enumerable
- Defined in:
- lib/archaeo/asset_list.rb
Overview
Categorized collection of asset URLs extracted from an archived page.
Assets are grouped by type (css, js, image, font, media) for convenient access during bulk download or local archiving.
Constant Summary collapse
- CATEGORIES =
%i[css js image font media].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #add(url, type:) ⇒ Object
- #all ⇒ Object
- #counts ⇒ Object
- #css ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #filter(*types) ⇒ Object
- #fonts ⇒ Object
- #images ⇒ Object
-
#initialize ⇒ AssetList
constructor
A new instance of AssetList.
- #js ⇒ Object
- #media ⇒ Object
- #merge(other) ⇒ Object
- #size ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #urls_by_type(type) ⇒ Object
Constructor Details
#initialize ⇒ AssetList
Returns a new instance of AssetList.
15 16 17 18 |
# File 'lib/archaeo/asset_list.rb', line 15 def initialize @urls_by_type = {} CATEGORIES.each { |c| @urls_by_type[c] = [] } end |
Class Method Details
.from_json(json_string) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/archaeo/asset_list.rb', line 94 def self.from_json(json_string) data = JSON.parse(json_string) list = new data.each do |type, urls| sym = type.to_sym next unless CATEGORIES.include?(sym) Array(urls).each { |url| list.add(url, type: sym) } end list end |
Instance Method Details
#add(url, type:) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/archaeo/asset_list.rb', line 20 def add(url, type:) return if url.nil? || url.empty? return if @urls_by_type[type].include?(url) @urls_by_type[type] << url end |
#all ⇒ Object
55 56 57 |
# File 'lib/archaeo/asset_list.rb', line 55 def all @urls_by_type.values.flatten.uniq end |
#counts ⇒ Object
75 76 77 |
# File 'lib/archaeo/asset_list.rb', line 75 def counts @urls_by_type.transform_values(&:size) end |
#css ⇒ Object
31 32 33 |
# File 'lib/archaeo/asset_list.rb', line 31 def css @urls_by_type[:css] end |
#each(&block) ⇒ Object
27 28 29 |
# File 'lib/archaeo/asset_list.rb', line 27 def each(&block) all.each(&block) end |
#empty? ⇒ Boolean
63 64 65 |
# File 'lib/archaeo/asset_list.rb', line 63 def empty? all.empty? end |
#filter(*types) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/archaeo/asset_list.rb', line 79 def filter(*types) result = self.class.new types.each do |type| @urls_by_type[type]&.each { |url| result.add(url, type: type) } end result end |
#fonts ⇒ Object
47 48 49 |
# File 'lib/archaeo/asset_list.rb', line 47 def fonts @urls_by_type[:font] end |
#images ⇒ Object
39 40 41 |
# File 'lib/archaeo/asset_list.rb', line 39 def images @urls_by_type[:image] end |
#js ⇒ Object
35 36 37 |
# File 'lib/archaeo/asset_list.rb', line 35 def js @urls_by_type[:js] end |
#media ⇒ Object
51 52 53 |
# File 'lib/archaeo/asset_list.rb', line 51 def media @urls_by_type[:media] end |
#merge(other) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/archaeo/asset_list.rb', line 87 def merge(other) CATEGORIES.each do |type| other.urls_by_type(type).each { |url| add(url, type: type) } end self end |
#size ⇒ Object
59 60 61 |
# File 'lib/archaeo/asset_list.rb', line 59 def size all.size end |
#to_h ⇒ Object
67 68 69 |
# File 'lib/archaeo/asset_list.rb', line 67 def to_h @urls_by_type.transform_values(&:dup) end |
#to_json(*args) ⇒ Object
71 72 73 |
# File 'lib/archaeo/asset_list.rb', line 71 def to_json(*args) to_h.to_json(*args) end |
#urls_by_type(type) ⇒ Object
43 44 45 |
# File 'lib/archaeo/asset_list.rb', line 43 def urls_by_type(type) @urls_by_type[type] || [] end |