Module: Cyberweb::Favicon
- Defined in:
- lib/cyberweb/modules/favicon.rb
Overview
Cyberweb::Favicon
Defined Under Namespace
Classes: Error, InvalidFavicon, MissingFavicon
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.download(url) ⇒ Object
# === Cyberweb::Favicon.download.
-
.path_of?(i) ⇒ Boolean
# === Cyberweb::Favicon.path_of? ========================================================================= #.
Instance Method Summary collapse
-
#url=(i) ⇒ Object
(also: #set_url)
# === url? ========================================================================= #.
-
#url? ⇒ Boolean
(also: #url)
# === url? ========================================================================= #.
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
32 33 34 |
# File 'lib/cyberweb/modules/favicon.rb', line 32 def data @data end |
Class Method Details
.download(url) ⇒ Object
#
Cyberweb::Favicon.download
Use this method to download a remote favicon. The first argument should be the remote URL to a website, as a String.
Nokogiri will search for any favicon in the remote URL.
#
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/cyberweb/modules/favicon.rb', line 181 def self.download(url) begin require 'open-uri' rescue LoadError; end begin require 'nokogiri' rescue LoadError; end begin require 'addressable/uri' rescue LoadError; end uri = Addressable::URI.heuristic_parse(url) uri = Addressable::URI.encode(uri) # needed for chars with accents in url doc = Nokogiri::HTML(open(uri)) # ======================================================================= # # Search for the favicon-path next: # ======================================================================= # path = doc.xpath("//link[@rel='shortcut icon' or @rel='icon']/@href").first path = path ? path.to_s : '/favicon.ico' favicon_uri = Addressable::URI.join(uri, path.to_s) # ======================================================================= # # Note that favicon_uri will point to something such as: # # http://www.ruppweb.org/favicon.ico # # ======================================================================= # begin stream = open(favicon_uri) unless stream.content_type[/^image/] raise InvalidFavicon, "wrong content_type (#{stream.content_type})" end # ===================================================================== # # Obtain the stream-dataset next. # ===================================================================== # data = stream.read if data.size == 0 raise InvalidFavicon, 'zero data' end # ===================================================================== # # Instantiate a new Favicon class next. # ===================================================================== # fav = Favicon.new fav.url = favicon_uri.to_s fav.data = data # ===================================================================== # # Ok, we now have the dataset required stored in the object fav. # We can next proceed to download this into a file. # ===================================================================== # begin e "A favicon was discovred at: #{fav.url}" location = 'favicon.png' # Where to store it. File.open(location, 'w') { |file| file.write(fav.data) } e 'Stored into the file: '+ (Dir.pwd+'/'+location).squeeze('/') rescue MissingFavicon e 'Favicon is missing.' rescue InvalidFavicon e 'Favicon is invalid.' end rescue OpenURI::HTTPError => e raise MissingFavicon if e.to_s[/404/] # 404 means missing. raise # nevermind, reraise the previous exception end end |
.path_of?(i) ⇒ Boolean
#
Cyberweb::Favicon.path_of?
#
44 45 46 47 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cyberweb/modules/favicon.rb', line 44 def self.path_of?(i) if i.is_a? Array i = i.first end # ======================================================================= # # === Handle Symbols here # ======================================================================= # if i.is_a? Symbol case i # case tag # ======================================================================= # # === :rpg_favicon # ======================================================================= # when :rpg_favicon, :rpg i = 'RPG/RPG_FAVICON.png' # ======================================================================= # # === :wlan_favicon # ======================================================================= # when :wlan_favicon, :wlan i = 'science/funk/WLAN_FAVICON.png' # ======================================================================= # # === :package # ======================================================================= # when :package i = 'STD/DOWNLOAD_PACKAGE.png' # ======================================================================= # # === :dnd_favicon # ======================================================================= # when :dnd_favicon, :dnd_favicons, :dnd i = 'RPG/DnD/DnD_FAVICON.png' # ======================================================================= # # === :ruby_favicon # ======================================================================= # when :ruby_favicon i = 'programming/ruby/RUBY_FAVICON.png' # ======================================================================= # # === :linux_favicon # ======================================================================= # when :linux_favicon i = 'linux/LINUX_FAVICON.png' # ======================================================================= # # === :sitemap_favicon # ======================================================================= # when :sitemap_favicon i = 'SITEMAP/SITEMAP_FAVICON.png' # ======================================================================= # # === :science_favicon # ======================================================================= # when :science_favicon i = 'science/SCIENCE_FAVICON.png' # ======================================================================= # # === :bioroebe_favicon # ======================================================================= # when :bioroebe_favicon i = 'bioroebe/images/BIOROEBE.png' # ======================================================================= # # === :dsa # ======================================================================= # when :dsa, :dsa_favicon i = 'RPG/DSA/DSA_FAVICON.png' # ======================================================================= # # === :shadowrun_favicon # ======================================================================= # when :shadowrun_favicon, :shadowrun i = 'RPG/SR/SHADOWRUN_FAVICON.png' # ======================================================================= # # === :default # ======================================================================= # when :default i = 'STD/ZITRONE.png' # ======================================================================= # # === :tai_favicon # ======================================================================= # when :tai_favicon i = 'RPG/SARLEM/OSTREICH/TAI/TAI_FAVICON.png' # ======================================================================= # # === :sarlem # ======================================================================= # when :sarlem, :sarlem_favicon i = 'RPG/SARLEM/SARLEM_FAVICON.png' # ======================================================================= # # === :random # ======================================================================= # when :random, :rand i = Cyberweb.random_favicon # ======================================================================= # # === :inline # # Add support for "inline" favicons" which are ... no favicons really. # # URLs prefixed with the "data:" scheme allow content creators to # embed small files inline in documents. # ======================================================================= # when :inline, :minimal, :none i = "<link href=data:, rel=icon>#{NL}" else # ===================================================================== # # We assume this symbol to be part of WebImage. This will # handle input such as :big_star, for example. # ===================================================================== # i = Cyberweb.get_webimage(i) end end return i end |