Module: FaviconParty::Utils

Extended by:
Utils
Included in:
Fetcher, HTTPClient, Image, Utils
Defined in:
lib/favicon_party/utils.rb

Instance Method Summary collapse

Instance Method Details

#encode_utf8(text) ⇒ Object



22
23
24
25
# File 'lib/favicon_party/utils.rb', line 22

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



27
28
29
30
31
32
33
# File 'lib/favicon_party/utils.rb', line 27

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, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/favicon_party/utils.rb', line 9

def prefix_url(url, options = {})
  unless options[:downcase] == false
    url = URI.encode url.strip.downcase
  else
    url = URI.encode url.strip
  end
  if url =~ /https?:\/\//
    url
  else
    "http://#{url}"
  end
end

#with_temp_data_file(data, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/favicon_party/utils.rb', line 35

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