Class: DocJuan::UrlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/doc_juan/url_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, filename, format = 'pdf', options = {}) ⇒ UrlGenerator

Returns a new instance of UrlGenerator.

Raises:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/doc_juan/url_generator.rb', line 12

def initialize url, filename, format = 'pdf', options = {}
  @url = url
  @filename = filename.to_s
  @format = format

  options = {} unless options
  options = options.merge authentication_credentials if has_authentication_credentials?
  @options = options

  raise NoHostGivenError if host == ''
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/doc_juan/url_generator.rb', line 10

def filename
  @filename
end

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/doc_juan/url_generator.rb', line 10

def format
  @format
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/doc_juan/url_generator.rb', line 10

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/doc_juan/url_generator.rb', line 10

def url
  @url
end

Instance Method Details

#authentication_credentialsObject



49
50
51
52
53
54
# File 'lib/doc_juan/url_generator.rb', line 49

def authentication_credentials
  {
    username: DocJuan.config.username,
    password: DocJuan.config.password
  }
end

#generateObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/doc_juan/url_generator.rb', line 24

def generate
  params = []
  params << "url=#{CGI.escape(url)}"
  params << "filename=#{CGI.escape(filename)}"
  params << "format=#{CGI.escape(format)}"
  options.each do |k,v|
    params << "options[#{CGI.escape(k.to_s)}]=#{CGI.escape v.to_s}"
  end
  params << "key=#{public_key}"

  "#{host}/render?#{params.join('&')}"
end

#has_authentication_credentials?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/doc_juan/url_generator.rb', line 56

def has_authentication_credentials?
  authentication_credentials.values.compact.any?
end

#hostObject



41
42
43
44
45
46
47
# File 'lib/doc_juan/url_generator.rb', line 41

def host
  @host ||= DocJuan.config.host.to_s.strip.tap do |host|
    if host != '' && ! host.start_with?('http')
      host.replace "http://#{host}"
    end
  end
end

#public_keyObject



37
38
39
# File 'lib/doc_juan/url_generator.rb', line 37

def public_key
  @public_key ||= DocJuan::Token.new(self).key
end