richurls 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 3fd453f2b0ccac4f62fb83db5045dd39a24e4d02c401634beb6b8111e93a9e5d
4
- data.tar.gz: '032735823680f9a51df7321cf74947ba53222d524359ae43c0210da0d5df2565'
3
+ metadata.gz: d2072268f21e502e956262e89d03475bed307c6f45d99d2cba65f827dec9ada7
4
+ data.tar.gz: 6711cccbf99ba51376bbd39dd8845f93d7d3030af1f9875ea3848fc8409199c7
5
5
  SHA512:
6
- metadata.gz: ffdd010f40bdbed9b909550f681333c7a52dbba57db58bbc72b501f506929e7640449699f56c6c0d27b03fa174fc3ff926d0de83e91869dcf808ca22e73de6f5
7
- data.tar.gz: eeff6ce8e4860d9e5e802b06c847553e893c42ca1abf3d8a33bcb6aa2a819fd676e795e6545d623813715a497ab6413554291d6a0abfb27977f5a9172cc22d4f
6
+ metadata.gz: 2126826dbb14433abffb4be373b30c93463c736146cceff8c4b9c855b12472c988abdf3d1aa430c9be1ad3c42204c588c00046bc6cc20acfb5b52486d1d4c13f
7
+ data.tar.gz: 20d609d8647b038b39745b7389895401d0223d3fb54d778ece204d0747f6b3ede1717b85601e721607f08a55eadf80d88bf5e1b649a9d496a628c2603411d962
data/README.md CHANGED
@@ -1,2 +1,27 @@
1
1
  # richurls
2
- A gem which can enrich urls
2
+ A gem which can enrich urls with speed.
3
+
4
+ **Installation**
5
+
6
+ ```
7
+ gem install richurls
8
+ ```
9
+
10
+ **Usage:**
11
+
12
+ ```ruby
13
+ require 'richurls'
14
+
15
+ RichUrls.enrich('https://wetransfer.com')
16
+
17
+ # Returns:
18
+ # {
19
+ # "title"=>"WeTransfer",
20
+ # "description"=>"WeTransfer is the simplest way to send your files around the world",
21
+ # "image"=>"https://prod-cdn.wetransfer.net/assets/wt-facebook-568be8def5a86a09cedeb21b8f24cb208e86515a552bd07d856c7d5dfc6a23df.png",
22
+ # "provider_display"=>"https://wetransfer.com",
23
+ # "favicon"=>"https://prod-cdn.wetransfer.net/assets/favicon-d12161435ace47c6883360e08466508593325f134c1852b1d0e6e75d5f76adda.ico",
24
+ # "embed"=>nil
25
+ # }
26
+ ```
27
+
@@ -1,10 +1,13 @@
1
1
  require 'ox'
2
2
 
3
3
  require_relative 'xml_handler'
4
+ require_relative 'url_helper'
4
5
  require_relative 'parsers/title_parser'
5
6
  require_relative 'parsers/description_parser'
6
7
  require_relative 'parsers/image_parser'
7
8
  require_relative 'parsers/embed_parser'
9
+ require_relative 'parsers/provider_display_parser'
10
+ require_relative 'parsers/favicon_parser'
8
11
 
9
12
  module RichUrls
10
13
  class BodyDecorator
@@ -14,6 +17,8 @@ module RichUrls
14
17
  'title' => Parsers::TitleParser,
15
18
  'description' => Parsers::DescriptionParser,
16
19
  'image' => Parsers::ImageParser,
20
+ 'provider_display' => Parsers::ProviderDisplayParser,
21
+ 'favicon' => Parsers::FaviconParser,
17
22
  'embed' => Parsers::EmbedParser
18
23
  }.freeze
19
24
 
@@ -0,0 +1,9 @@
1
+ module Parsers
2
+ FaviconParser = lambda do |document, url|
3
+ favicon_el = document.find do |el|
4
+ el.name == :link && el.attributes[:rel] == 'shortcut icon'
5
+ end
6
+
7
+ favicon_el && UrlHelper.url_for(url, favicon_el.attributes[:href])
8
+ end
9
+ end
@@ -1,37 +1,14 @@
1
1
  module Parsers
2
2
  class ImageParser
3
- TRAILING_SLASH_REGEXP = /(\/)+$/.freeze
4
-
5
3
  def self.call(document, url)
6
- new(document, url).call
7
- end
8
-
9
- private_class_method :new
10
-
11
- def initialize(document, url)
12
- @document = document
13
- @url = url
14
- end
15
-
16
- def call
17
- if image_source =~ URI::DEFAULT_PARSER.make_regexp
18
- image_source
19
- elsif image_source
20
- url_no_trailing_slash = @url.sub(TRAILING_SLASH_REGEXP, '')
21
-
22
- [url_no_trailing_slash, image_source].join('/')
23
- end
24
- end
25
-
26
- private
4
+ meta_image = document.find {|f| f.name == :meta && f.attributes[:property] == 'og:image' }
5
+ image_tag = document.find {|f| f.name == :img }
27
6
 
28
- def image_source
29
- @image_source ||= begin
30
- meta_image = @document.find {|f| f.name == :meta && f.attributes[:property] == 'og:image' }
31
- image_tag = @document.find {|f| f.name == :img }
7
+ image_source =
8
+ (meta_image && meta_image.attributes[:content]) ||
9
+ (image_tag && image_tag.attributes[:src])
32
10
 
33
- (meta_image && meta_image.attributes[:content]) || (image_tag && image_tag.attributes[:src])
34
- end
11
+ image_source && UrlHelper.url_for(url, image_source)
35
12
  end
36
13
  end
37
14
  end
@@ -0,0 +1,7 @@
1
+ module Parsers
2
+ ProviderDisplayParser = lambda do |_, url|
3
+ uri = URI(url)
4
+
5
+ uri.scheme + '://' + uri.host
6
+ end
7
+ end
data/lib/url_helper.rb ADDED
@@ -0,0 +1,15 @@
1
+ module UrlHelper
2
+ def self.url_for(domain, url)
3
+ return if url.nil?
4
+ return url if url =~ URI::DEFAULT_PARSER.make_regexp
5
+
6
+ uri = URI(domain)
7
+ base = uri.scheme + '://' + uri.host
8
+
9
+ if url.start_with?('/')
10
+ base + url
11
+ else
12
+ base + uri.path + '/' + url
13
+ end
14
+ end
15
+ end
data/lib/xml_handler.rb CHANGED
@@ -5,6 +5,7 @@ module RichUrls
5
5
  head
6
6
  title
7
7
  meta
8
+ link
8
9
  img
9
10
  ).freeze
10
11
 
data/richurls.gemspec CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'richurls'
6
- spec.version = '0.0.1'
6
+ spec.version = '0.0.2'
7
7
  spec.authors = ['grdw']
8
8
  spec.email = ['gerard@wetransfer.com']
9
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: richurls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
@@ -126,10 +126,13 @@ files:
126
126
  - lib/parsers/embed_parser/base.rb
127
127
  - lib/parsers/embed_parser/paste.rb
128
128
  - lib/parsers/embed_parser/youtube.rb
129
+ - lib/parsers/favicon_parser.rb
129
130
  - lib/parsers/image_parser.rb
131
+ - lib/parsers/provider_display_parser.rb
130
132
  - lib/parsers/title_parser.rb
131
133
  - lib/richurls.rb
132
134
  - lib/url_fetcher.rb
135
+ - lib/url_helper.rb
133
136
  - lib/xml_handler.rb
134
137
  - richurls.gemspec
135
138
  homepage: https://github.com/wetransfer/richurls