Validate URLs
This commit is contained in:
parent
1b142006fa
commit
ac3efc7c15
@ -1,15 +1,31 @@
|
||||
require 'uri'
|
||||
require "manifique/web_client"
|
||||
|
||||
module Manifique
|
||||
class Agent
|
||||
def initialize(options)
|
||||
def initialize(options={})
|
||||
@options = options
|
||||
@url = options[:url]
|
||||
|
||||
if url_valid?(options[:url])
|
||||
@url = options[:url]
|
||||
else
|
||||
raise "No valid URL specified"
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_metadata
|
||||
web_client = WebClient.new(url: @url)
|
||||
web_client.fetch_web_manifest
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def url_valid?(str)
|
||||
return false unless str.class == String
|
||||
url = URI.parse(str)
|
||||
url.kind_of?(URI::HTTP) || url.kind_of?(URI::HTTPS)
|
||||
rescue URI::InvalidURIError
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -5,6 +5,14 @@ class ManifiqueTest < Minitest::Test
|
||||
refute_nil ::Manifique::VERSION
|
||||
end
|
||||
|
||||
def test_init_without_url
|
||||
assert_raises(RuntimeError) { Manifique::Agent.new }
|
||||
end
|
||||
|
||||
def test_init_with_invalid_url
|
||||
assert_raises(RuntimeError) { Manifique::Agent.new(url: "htp:/foo.com") }
|
||||
end
|
||||
|
||||
def test_fetch_metadata
|
||||
agent = Manifique::Agent.new(url: 'https://example.com')
|
||||
assert_equal agent.fetch_metadata, 'https://example.com'
|
||||
|
Loading…
x
Reference in New Issue
Block a user