Class: Manifique::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/manifique/metadata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Metadata

Returns a new instance of Metadata.



9
10
11
12
13
14
# File 'lib/manifique/metadata.rb', line 9

def initialize(data={})
  self.url = data[:url]
  self.from_web_manifest = Set.new
  self.from_html = Set.new
  self.icons = []
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def background_color
  @background_color
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def description
  @description
end

#displayObject

Returns the value of attribute display.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def display
  @display
end

#from_htmlObject

Returns the value of attribute from_html.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def from_html
  @from_html
end

#from_web_manifestObject

Returns the value of attribute from_web_manifest.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def from_web_manifest
  @from_web_manifest
end

#iconsObject

Returns the value of attribute icons.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def icons
  @icons
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def scope
  @scope
end

#share_targetObject

Returns the value of attribute share_target.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def share_target
  @share_target
end

#short_nameObject

Returns the value of attribute short_name.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def short_name
  @short_name
end

#start_urlObject

Returns the value of attribute start_url.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def start_url
  @start_url
end

#theme_colorObject

Returns the value of attribute theme_color.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def theme_color
  @theme_color
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/manifique/metadata.rb', line 4

def url
  @url
end

Instance Method Details

#load_from_html(html) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/manifique/metadata.rb', line 26

def load_from_html(html)
  @html = html
  parse_title_from_html
  parse_meta_elements_from_html
  parse_display_mode_from_html
  parse_icons_from_html
end

#load_from_web_manifest(manifest) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/manifique/metadata.rb', line 16

def load_from_web_manifest(manifest)
  [ :name, :short_name, :description, :icons,
    :theme_color, :background_color, :display,
    :start_url, :scope, :share_target ].map(&:to_s).each do |prop|
     next unless manifest[prop] && !manifest[prop].to_s.empty?
     self.send("#{prop}=", manifest[prop])
     self.from_web_manifest.add(prop)
  end
end

#select_icon(options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/manifique/metadata.rb', line 34

def select_icon(options={})
  if options[:type].nil? && options[:sizes].nil? && options[:purpose].nil?
    raise ArgumentError, "Tell me what to do!"
  end

  results = icons.dup

  if options[:purpose]
    results.reject! { |r| r["purpose"] != options[:purpose] }
  end

  if options[:type]
    if options[:type].is_a?(String)
      results.reject! { |r| r["type"] != options[:type] }
    elsif options[:type].is_a?(Regexp)
      results.reject! { |r| r["type"].match(options[:type]).nil? }
    else
      raise ArgumentError, "Type must be a string or a regular expression"
    end
  end

  if options[:sizes]
    results.reject! { |r| r["sizes"].nil? || r["sizes"].match(/(\d+)x/).nil? }
    results.sort!   { |a, b| sizes_to_i(b["sizes"]) <=> sizes_to_i(a["sizes"]) }

    if icon = select_exact_size(results, options[:sizes])
      return icon
    else
      return select_best_size(results, options[:sizes])
    end
  end

  results.first
end

#to_jsonObject



69
70
71
# File 'lib/manifique/metadata.rb', line 69

def to_json
  # TODO serialize into JSON
end