webstract 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2acd9f72a410808ca0c13b251ea4a90c6a8f16fe
4
- data.tar.gz: 9bf28dbbd5dd17df779ef11bb8172808a5ba0620
3
+ metadata.gz: 2021ffa427679aad2c1ad7953e64bc09c787ff36
4
+ data.tar.gz: 31800e37441971669644353ee402ec9526337007
5
5
  SHA512:
6
- metadata.gz: f477129c1811a7ffce4d1bf3e8d2a77555102d8eaef1a82d9d6ef0a277236cef491e3c87c43e91b38bea994c8d2232d142349993bcca3b6e995002520b1893e0
7
- data.tar.gz: 6ea30f55206ac01ccbb9a30d43928bdc7adfe13960510f84b27f49a27c61ae48e777e193c2c4f61cf1dba009f8d41f983034f116f594190b0299ddcb2a0d795c
6
+ metadata.gz: b1593c55dd96f86c4717a5d734f4659cad3b45ab9552e43f7fa113524f2e90bab792e128102b7ba00f25d7f1639bcdd1704126a55255afa4293adff2a8b75032
7
+ data.tar.gz: 8ccdafd69a089fed785756520933c4977432add9b08c7443d3996b062bb11e36ad76eac7e718a2566c61bc7e9064192414850b6ec6f34ea123f65fc4b9b8e1e0
@@ -9,5 +9,9 @@ module Webstract
9
9
  # raised when the target page could not be reached
10
10
  # but due to server errors like 404, or 500.
11
11
  class PageError < StandardError; ;end
12
+
13
+ # favicon errors
14
+ class MissingFavicon < StandardError; ;end
15
+ class InvalidFavicon < StandardError; ;end
12
16
  end
13
17
  end
@@ -1,8 +1,11 @@
1
- require 'faviconduit'
1
+ require 'open_uri_redirections'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require "addressable/uri"
2
5
 
3
6
  module Webstract
4
7
  class Favicon
5
- attr_accessor :url, :favicon_data
8
+ attr_accessor :url, :data
6
9
 
7
10
  def initialize(options = {})
8
11
  raise(ArgumentError.new("requires url")) unless options.key?(:url)
@@ -10,7 +13,7 @@ module Webstract
10
13
  end
11
14
 
12
15
  def fetch
13
- @favicon_data = Faviconduit.get(url).data
16
+ @data = self.class.get(url).data
14
17
  end
15
18
 
16
19
  def fetch_and_save(path)
@@ -21,5 +24,42 @@ module Webstract
21
24
  end
22
25
  end
23
26
 
27
+ def self.get(url)
28
+
29
+ uri = Addressable::URI.heuristic_parse(url)
30
+ uri = Addressable::URI.encode(uri) # needed for chars with accents in url
31
+
32
+ doc = Nokogiri::HTML(open(uri, allow_redirections: :all))
33
+
34
+ path = doc.xpath("//link[@rel='shortcut icon' or @rel='icon']/@href").first
35
+ path = path ? path.to_s : '/favicon.ico'
36
+
37
+ fav_uri = Addressable::URI.join(uri, path.to_s)
38
+
39
+ begin
40
+
41
+ stream = open(fav_uri, allow_redirections: :all)
42
+
43
+ unless stream.content_type[/^image/]
44
+ raise(Errors::InvalidFavicon, "wrong content_type (#{stream.content_type})")
45
+ end
46
+
47
+ data = stream.read
48
+
49
+ if data.size == 0
50
+ raise(Errors::InvalidFavicon, "zero data")
51
+ end
52
+
53
+ fav = Favicon.new(url: fav_uri.to_s)
54
+ fav.data = data
55
+ return fav
56
+
57
+ rescue OpenURI::HTTPError => e
58
+ raise Errors::MissingFavicon if e.to_s[/404/]
59
+ raise # nevermind, reraise the previous exception
60
+ end
61
+
62
+ end
63
+
24
64
  end
25
65
  end
@@ -1,3 +1,3 @@
1
1
  module Webstract
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstract
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Faucett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-12 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -45,7 +45,35 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: '1.5'
47
47
  - !ruby/object:Gem::Dependency
48
- name: faviconduit
48
+ name: open_uri_redirections
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: addressable
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: nokogiri
49
77
  requirement: !ruby/object:Gem::Requirement
50
78
  requirements:
51
79
  - - ">="