Class: Iconoclasm::Favicon

Inherits:
Object
  • Object
show all
Includes:
Downloader
Defined in:
lib/iconoclasm/favicon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Downloader

#get, #head, user_agent, #user_agent, user_agent=

Constructor Details

#initialize(attributes = {}) ⇒ Favicon

Returns a new instance of Favicon.



12
13
14
15
16
17
18
19
20
# File 'lib/iconoclasm/favicon.rb', line 12

def initialize(attributes = {})
  @url          = attributes[:url]
  @data         = attributes[:data]
  @name         = attributes[:name]           || parse_name_from(@url)
  headers       = attributes[:headers]
  @content_type = attributes[:content_type]   ? attributes[:content_type] : headers ? headers.content_type : nil
  @size         = attributes[:content_length] ? attributes[:content_length] : headers ? headers.content_length : nil
  @save_path    = nil
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



9
10
11
# File 'lib/iconoclasm/favicon.rb', line 9

def content_type
  @content_type
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/iconoclasm/favicon.rb', line 10

def name
  @name
end

#save_pathObject (readonly)

Returns the value of attribute save_path.



9
10
11
# File 'lib/iconoclasm/favicon.rb', line 9

def save_path
  @save_path
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/iconoclasm/favicon.rb', line 9

def url
  @url
end

Instance Method Details

#dataObject



31
32
33
# File 'lib/iconoclasm/favicon.rb', line 31

def data
  @data ||= fetch_data
end

#dump_data(file) ⇒ Object



98
99
100
101
102
# File 'lib/iconoclasm/favicon.rb', line 98

def dump_data(file)
  file.write(data)
  file.close
  file
end

#fetch_dataObject



59
60
61
62
63
64
65
66
# File 'lib/iconoclasm/favicon.rb', line 59

def fetch_data
  response = get(url)
  if response.code == 200
    response.body
  else
    raise Iconoclasm::HTTPError.new(url, response)
  end
end

#inspectObject



22
23
24
# File 'lib/iconoclasm/favicon.rb', line 22

def inspect
  "#<Iconoclasm::Favicon @url=#{url}, @name=#{name}, @content_type=#{content_type}, @size=#{size}, @save_path=#{save_path ? save_path : "nil"}>"
end

#parse_name_from(url) ⇒ Object



94
95
96
# File 'lib/iconoclasm/favicon.rb', line 94

def parse_name_from(url)
  Addressable::URI.parse(url).path.split('/').last
end

#save(path_or_storage = nil, force = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/iconoclasm/favicon.rb', line 68

def save(path_or_storage = nil, force = false)
  if valid? && !force
    warn("Saving an invalid favicon.") if !valid? && force
    @save_path = if path_or_storage.nil?
      save_to_tempfile
    elsif path_or_storage.is_a?(String)
      save_to_file(path_or_storage)
    else
      raise Iconoclasm::RTFMError.new("invalid storage type")
    end
  else
    raise Iconoclasm::InvalidFavicon.new(url, content_type)
  end
end

#save_to_file(path) ⇒ Object



88
89
90
91
92
# File 'lib/iconoclasm/favicon.rb', line 88

def save_to_file(path)
  path = File.expand_path(File.join(path, name))
  dump_data(File.new(path, File::CREAT|File::TRUNC|File::WRONLY))
  @save_path = path
end

#save_to_tempfileObject



83
84
85
86
# File 'lib/iconoclasm/favicon.rb', line 83

def save_to_tempfile
  tfile = dump_data(Tempfile.new(name))
  @save_path = tfile.path
end

#sizeObject Also known as: content_length



26
27
28
# File 'lib/iconoclasm/favicon.rb', line 26

def size
  @size ||= data.size
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iconoclasm/favicon.rb', line 44

def valid?
  @valid ||= if size > 0
    case content_type
    when /^(?:x-)?image/ then true
    when /^text\/html/ then false
    when NilClass then false
    else
      # check the file type using filemagic, maybe?
      false
    end
  else
    false
  end
end