Class: EmbeddableHost

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/embeddable_host.rb

Class Method Summary collapse

Class Method Details

.record_for_url(uri) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/embeddable_host.rb', line 16

def self.record_for_url(uri)
  if uri.is_a?(String)
    uri =
      begin
        URI(UrlHelper.normalized_encode(uri))
      rescue URI::Error, Addressable::URI::InvalidURIError
      end
  end

  return false unless uri.present?

  host = uri.host
  return false unless host.present?

  host << ":#{uri.port}" if uri.port.present? && uri.port != 80 && uri.port != 443

  path = uri.path
  path << "?" << uri.query if uri.query.present?

  where("lower(host) = ?", host).each do |eh|
    return eh if eh.allowed_paths.blank?

    path_regexp = Regexp.new(eh.allowed_paths)
    return eh if path_regexp.match(path) || path_regexp.match(UrlHelper.unencode(path))
  end

  nil
end

.url_allowed?(url) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/embeddable_host.rb', line 45

def self.url_allowed?(url)
  return false if url.nil?

  uri =
    begin
      URI(UrlHelper.normalized_encode(url))
    rescue URI::Error
    end

  uri.present? && record_for_url(uri).present?
end