Class: ImagePaths

Inherits:
Object
  • Object
show all
Defined in:
lib/image_paths.rb,
lib/image_paths/version.rb

Overview

Main gem class

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.at(page_path) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/image_paths.rb', line 9

def self.at(page_path)
  begin
    return collect_paths(page_path) if path_correct?(page_path)
  rescue => e
    puts "StandardError: #{e}"
    return []
  end
  []
end

.collect_paths(page_path) ⇒ Object



49
50
51
52
53
# File 'lib/image_paths.rb', line 49

def self.collect_paths(page_path)
  links = []
  Nokogiri::HTML(open(page_path)).css('img').select { |link| links << link.attr('src') }
  links
end

.path_correct?(page_path) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/image_paths.rb', line 19

def self.path_correct?(page_path)
  uri_ok?(page_path) && response_ok?(page_path)
end

.response_ok?(page_path) ⇒ Boolean

response.body, .code, .message, .headers.inspect

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/image_paths.rb', line 32

def self.response_ok?(page_path)
  begin
    response = HTTParty.get(page_path)
    if response.code.to_i >= 400
      puts "Error: bad response: response.code = #{response.code}"
      return false
    end
  rescue HTTParty::Error
    puts 'HTTParty::Error: bad response. HTTParty::Error'
    return false
  rescue StandardError
    puts 'StandardError: bad response'
    return false
  end
  true
end

.uri_ok?(page_path) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/image_paths.rb', line 23

def self.uri_ok?(page_path)
  unless page_path =~ /\A#{URI.regexp(%w(http https))}\z/
    puts 'Error: bad uri'
    return false
  end
  true
end