Module: ImageScraper::Util

Defined in:
lib/image_scraper/util.rb

Class Method Summary collapse

Class Method Details

.absolute_url(url, asset = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/image_scraper/util.rb', line 3

def self.absolute_url(url,asset=nil)
  # TODO - what happens when an index redirect occurs?
  # Example: 'http://example.com/about' specified as url
  #          'style.css' specified as asset
  #          url redirects to 'http://example.com/about/'
  #          and serves http://example.com/about/index.html
  #          which then links to the relative asset path 'style.css'
  #          based on original url (http://example.com/about), 
  #          self.absolute_url gives
  #          'http://example.com/style.css
  #          but should get:
  #          'http://example.com/about/style.css
  URI.parse(url).merge(URI.parse asset.to_s).to_s rescue nil
end

.chomp(image_url) ⇒ Object



36
37
38
# File 'lib/image_scraper/util.rb', line 36

def self.chomp(image_url)
  image_url.gsub(/\s/,'')
end

.cleanup_url(image_url) ⇒ Object



40
41
42
43
44
# File 'lib/image_scraper/util.rb', line 40

def self.cleanup_url(image_url)
  ImageScraper::Util.chomp(
    ImageScraper::Util.strip_quotes(
      ImageScraper::Util.strip_backslashes(image_url || '')))
end

.domain(url) ⇒ Object



18
19
20
21
# File 'lib/image_scraper/util.rb', line 18

def self.domain(url)
  uri = URI.parse(url)
  "#{uri.scheme}://#{uri.host}"
end

.path(url) ⇒ Object



23
24
25
26
# File 'lib/image_scraper/util.rb', line 23

def self.path(url)
  uri = URI.parse(url)
  uri.path
end

.strip_backslashes(image_url) ⇒ Object



28
29
30
# File 'lib/image_scraper/util.rb', line 28

def self.strip_backslashes(image_url)
  image_url.gsub("\\",'')
end

.strip_quotes(image_url) ⇒ Object



32
33
34
# File 'lib/image_scraper/util.rb', line 32

def self.strip_quotes(image_url)
  image_url.gsub("'","").gsub('"','')
end