Module: FaviconParty::Utils
- Extended by:
- Utils
- Included in:
- Fetcher, HTTPClient, Image, Utils
- Defined in:
- lib/favicon_party/utils.rb
Instance Method Summary collapse
- #encode_utf8(text) ⇒ Object
- #get_mime_type(data, use_file_cmd = true) ⇒ Object
- #prefix_url(url) ⇒ Object
- #with_temp_data_file(data, &block) ⇒ Object
Instance Method Details
#encode_utf8(text) ⇒ Object
18 19 20 21 |
# File 'lib/favicon_party/utils.rb', line 18 def encode_utf8(text) return text if text.valid_encoding? text.encode("UTF-8", :invalid => :replace, :undef => :replace, :replace => '') end |
#get_mime_type(data, use_file_cmd = true) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/favicon_party/utils.rb', line 23 def get_mime_type(data, use_file_cmd = true) if use_file_cmd with_temp_data_file(data) {|t| `file -b --mime-type #{t.path.to_s}`.strip } else FileMagic.new(:mime_type).buffer(data) end end |
#prefix_url(url) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/favicon_party/utils.rb', line 9 def prefix_url(url) url = URI.encode url.strip.downcase if url =~ /https?:\/\// url else "http://#{url}" end end |
#with_temp_data_file(data, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/favicon_party/utils.rb', line 31 def with_temp_data_file(data, &block) begin t = Tempfile.new(["favicon", ".ico"]) t.binmode t.write data t.close result = block.call(t) ensure t.unlink end result end |