Class: Prawn::Svg::UrlLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/url_loader.rb

Constant Summary collapse

DATAURL_REGEXP =
/(data:image\/(png|jpg);base64(;[a-z0-9]+)*,)/
URL_REGEXP =
/^https?:\/\/|#{DATAURL_REGEXP}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ UrlLoader


11
12
13
14
15
# File 'lib/prawn/svg/url_loader.rb', line 11

def initialize(opts = {})
  @url_cache = {}
  @enable_cache = opts.fetch(:enable_cache, false)
  @enable_web = opts.fetch(:enable_web, true)
end

Instance Attribute Details

#enable_cacheObject

Returns the value of attribute enable_cache


5
6
7
# File 'lib/prawn/svg/url_loader.rb', line 5

def enable_cache
  @enable_cache
end

#enable_webObject

Returns the value of attribute enable_web


5
6
7
# File 'lib/prawn/svg/url_loader.rb', line 5

def enable_web
  @enable_web
end

#url_cacheObject (readonly)

Returns the value of attribute url_cache


6
7
8
# File 'lib/prawn/svg/url_loader.rb', line 6

def url_cache
  @url_cache
end

Instance Method Details

#load(url) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prawn/svg/url_loader.rb', line 21

def load(url)
  @url_cache[url] || begin
    if m = url.match(DATAURL_REGEXP)
      data = Base64.decode64(url[m[0].length .. -1])
    elsif enable_web
      data = open(url).read
    else
      raise "No handler available to retrieve URL #{url}"
    end
    @url_cache[url] = data if enable_cache
    data
  end
end

#valid?(url) ⇒ Boolean


17
18
19
# File 'lib/prawn/svg/url_loader.rb', line 17

def valid?(url)
  !!url.match(URL_REGEXP)
end