doc_juan 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ group :test do
8
8
  gem 'minitest', '~> 3.1.0'
9
9
  gem 'minitest-reporters', '~> 0.7.1'
10
10
  gem 'mocha', '~> 0.11.4', require: false
11
+ gem 'addressable', '~> 2.2.8', require: false
11
12
  end
data/README.md CHANGED
@@ -17,6 +17,11 @@ You need to provide your secret key and the host of the Doc-Juan instance.
17
17
 
18
18
  If you are using Rails an initializer would be the appropriate place for this.
19
19
 
20
+ ### Authentication
21
+
22
+ If your site is behind HTTP Basic Authentication `DocJuan.config.username` and `DocJuan.config.password` is your friends.
23
+
24
+ **BEWARE!** Setting these will append your credentials to every DocJuan url generated and, depending on how you use them, can be _fully visible to your users_. The authentication credentials is foremost available for testing in your staging environment.
20
25
 
21
26
  ### Generating urls
22
27
 
@@ -30,7 +35,8 @@ Valid options are:
30
35
  * `height` - Page height in millimeters.
31
36
  * `size` - a4, letter etc. This will be ignored if width and height is set. [List of sizes](http://stackoverflow.com/questions/6394905/wkhtmltopdf-what-paper-sizes-are-valid).
32
37
  * `orientation` - `landscape` or `portrait`. Defaults to portrait.
33
- * `lowquality` - Renders the pdf in low quality if set to `true`
38
+ * `lowquality` - Renders the pdf in low quality if set to `true`.
39
+ * `format` - Output file format, `pdf` or `jpg`. Defaults to `pdf`.
34
40
 
35
41
  #### Example
36
42
 
@@ -0,0 +1,36 @@
1
+ require 'openssl'
2
+ require_relative 'config.rb'
3
+
4
+ module DocJuan
5
+ class NoSecretGivenError < StandardError; end
6
+
7
+ class Token
8
+ def initialize url_generator
9
+ @url_generator = url_generator
10
+
11
+ raise NoSecretGivenError if secret == ''
12
+ end
13
+
14
+ def key
15
+ sha1 = OpenSSL::Digest::Digest.new 'sha1'
16
+ OpenSSL::HMAC.hexdigest sha1, secret, seed
17
+ end
18
+
19
+ def secret
20
+ DocJuan.config.secret.to_s.strip
21
+ end
22
+
23
+ def seed
24
+ seed = []
25
+ seed << "filename:#{@url_generator.filename}"
26
+ seed << "format:#{@url_generator.format}"
27
+ Hash[(@url_generator.options).sort].each do |k,v|
28
+ seed << "options_#{k}:#{v}"
29
+ end
30
+ seed << "url:#{@url_generator.url}"
31
+
32
+ seed.join '-'
33
+ end
34
+
35
+ end
36
+ end
@@ -1,24 +1,23 @@
1
- require 'openssl'
2
1
  require 'cgi'
3
2
 
3
+ require_relative 'token'
4
4
  require_relative 'config'
5
5
 
6
6
  module DocJuan
7
7
  class NoHostGivenError < StandardError; end
8
- class NoSecretGivenError < StandardError; end
9
8
 
10
9
  class UrlGenerator
11
- attr_reader :url, :filename, :options
10
+ attr_reader :url, :filename, :format, :options
12
11
 
13
- def initialize url, filename, options = {}
12
+ def initialize url, filename, format = 'pdf', options = {}
14
13
  @url = url
15
14
  @filename = filename.to_s
15
+ @format = format
16
16
 
17
17
  options = {} unless options
18
18
  options = options.merge authentication_credentials if has_authentication_credentials?
19
- @options = Hash[(options || {}).sort]
19
+ @options = options
20
20
 
21
- raise NoSecretGivenError if secret_key == ''
22
21
  raise NoHostGivenError if host == ''
23
22
  end
24
23
 
@@ -26,6 +25,7 @@ module DocJuan
26
25
  params = []
27
26
  params << "url=#{CGI.escape(url)}"
28
27
  params << "filename=#{CGI.escape(filename)}"
28
+ params << "format=#{CGI.escape(format)}"
29
29
  options.each do |k,v|
30
30
  params << "options[#{CGI.escape(k.to_s)}]=#{CGI.escape v.to_s}"
31
31
  end
@@ -35,19 +35,7 @@ module DocJuan
35
35
  end
36
36
 
37
37
  def public_key
38
- sha1 = OpenSSL::Digest::Digest.new 'sha1'
39
- OpenSSL::HMAC.hexdigest sha1, secret_key, seed_string
40
- end
41
-
42
- def seed_string
43
- seed = []
44
- seed << "filename:#{filename}"
45
- options.each do |k,v|
46
- seed << "options_#{k}:#{v}"
47
- end
48
- seed << "url:#{url}"
49
-
50
- seed.join '-'
38
+ @public_key ||= DocJuan::Token.new(self).key
51
39
  end
52
40
 
53
41
  def host
@@ -58,10 +46,6 @@ module DocJuan
58
46
  end
59
47
  end
60
48
 
61
- def secret_key
62
- DocJuan.config.secret.to_s.strip
63
- end
64
-
65
49
  def authentication_credentials
66
50
  {
67
51
  username: DocJuan.config.username,
@@ -2,7 +2,6 @@ require_relative 'url_generator'
2
2
 
3
3
  module DocJuan
4
4
  def self.url url, filename, options = {}
5
- generator = UrlGenerator.new url, filename, options
6
- generator.generate
5
+ UrlGenerator.new(url, filename, options.delete(:format), options).generate
7
6
  end
8
7
  end
@@ -1,3 +1,3 @@
1
1
  module DocJuan
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,48 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/doc_juan/token.rb'
3
+ require 'mocha'
4
+
5
+ describe DocJuan::Token do
6
+ before :each do
7
+ DocJuan.config.secret = 'zecret'
8
+ end
9
+
10
+ let(:url_generator) do
11
+ stub(url: 'http://example.com', filename: 'file', format: 'pdf', options: {
12
+ title: 'The Site',
13
+ size: 'A4',
14
+ print_stylesheet: true
15
+ })
16
+ end
17
+
18
+ subject { DocJuan::Token.new(url_generator) }
19
+
20
+ it 'compiles into a seed string for the public key computation' do
21
+ subject.seed.must_equal 'filename:file-format:pdf-options_print_stylesheet:true-options_size:A4-options_title:The Site-url:http://example.com'
22
+ end
23
+
24
+ it 'calculates the public key' do
25
+ subject.key.must_equal 'b55142f9fdb148e8844e37e064e8eb2af6aabac6'
26
+ end
27
+
28
+ it 'calculates the public key with no options given' do
29
+ url_generator = stub url: 'http://example.com', filename: 'file', format: 'pdf', options: {}
30
+ token = DocJuan::Token.new(url_generator)
31
+
32
+ token.key.must_equal '539ebb1f6cd3fec40591acdc756e9b047e7093b3'
33
+ end
34
+
35
+ it 'has the secret key' do
36
+ subject.secret.must_equal 'zecret'
37
+ end
38
+
39
+ it 'fails with NoSecretGivenError unless there is a secret set' do
40
+ DocJuan.config.secret = nil
41
+
42
+ proc {
43
+ subject
44
+ }.must_raise DocJuan::NoSecretGivenError
45
+ end
46
+
47
+
48
+ end
@@ -1,8 +1,12 @@
1
+ require 'addressable/uri'
2
+
1
3
  require_relative 'spec_helper'
2
4
 
3
5
  require_relative '../lib/doc_juan/config.rb'
4
6
  require_relative '../lib/doc_juan/url_generator.rb'
5
7
 
8
+ require 'mocha'
9
+
6
10
  describe DocJuan::UrlGenerator do
7
11
  before :each do
8
12
  DocJuan.config.host = 'doc-juan.example.com'
@@ -10,7 +14,7 @@ describe DocJuan::UrlGenerator do
10
14
  end
11
15
 
12
16
  subject do
13
- DocJuan::UrlGenerator.new('http://example.com', 'file.pdf', {
17
+ DocJuan::UrlGenerator.new('http://example.com', 'file', 'pdf', {
14
18
  title: 'The Site',
15
19
  size: 'A4',
16
20
  print_stylesheet: true
@@ -18,32 +22,22 @@ describe DocJuan::UrlGenerator do
18
22
  end
19
23
 
20
24
  it 'generates the url' do
21
- url = subject.generate
22
-
23
- expected = 'http://doc-juan.example.com/render?'
24
- expected << "url=#{CGI.escape subject.url}"
25
- expected << "&filename=#{CGI.escape subject.filename}"
26
- expected << "&options[print_stylesheet]=true"
27
- expected << "&options[size]=A4"
28
- expected << "&options[title]=#{CGI.escape 'The Site'}"
29
- expected << "&key=#{subject.public_key}"
30
-
31
- url.must_equal expected
32
- end
25
+ uri = Addressable::URI.parse subject.generate
26
+ query = CGI.parse uri.query
33
27
 
34
- it 'generates the url with no options' do
35
- subject.stubs(:options).returns Hash.new
36
- url = subject.generate
28
+ uri.host.must_equal 'doc-juan.example.com'
29
+ uri.path.must_equal '/render'
37
30
 
38
- expected = 'http://doc-juan.example.com/render?'
39
- expected << "url=#{CGI.escape subject.url}"
40
- expected << "&filename=#{CGI.escape subject.filename}"
41
- expected << "&key=#{subject.public_key}"
31
+ query['url'].first.must_equal subject.url
32
+ query['filename'].first.must_equal subject.filename
33
+ query['key'].first.must_equal subject.public_key
34
+ query['format'].first.must_equal subject.format
42
35
 
43
- url.must_equal expected
36
+ subject.options.each do |k, v|
37
+ query["options[#{k}]"].first.must_equal v.to_s
38
+ end
44
39
  end
45
40
 
46
-
47
41
  it 'has the host' do
48
42
  DocJuan.config.host = 'example.com'
49
43
 
@@ -58,16 +52,9 @@ describe DocJuan::UrlGenerator do
58
52
  }.must_raise DocJuan::NoHostGivenError
59
53
  end
60
54
 
61
- it 'has the secret key' do
62
- subject.secret_key.must_equal 'zecret'
63
- end
64
-
65
- it 'fails with NoSecretGivenError unless there is a secret set' do
66
- DocJuan.config.secret = nil
67
-
68
- proc {
69
- subject
70
- }.must_raise DocJuan::NoSecretGivenError
55
+ it 'uses the token class to calculate the key' do
56
+ DocJuan::Token.expects(:new).with(subject).returns stub(key: 'verypublickey')
57
+ subject.public_key.must_equal 'verypublickey'
71
58
  end
72
59
 
73
60
  describe '#authentication' do
@@ -92,7 +79,6 @@ describe DocJuan::UrlGenerator do
92
79
  subject.has_authentication_credentials?.must_equal false
93
80
  end
94
81
 
95
-
96
82
  it 'appends username and password to options if set as config variables' do
97
83
  subject.options[:username].must_equal 'xkcd'
98
84
  subject.options[:password].must_equal 'correct horse battery staple'
@@ -100,19 +86,4 @@ describe DocJuan::UrlGenerator do
100
86
 
101
87
  end
102
88
 
103
- it 'compiles into a seed string for the public key computation' do
104
- subject.seed_string.must_equal 'filename:file.pdf-options_print_stylesheet:true-options_size:A4-options_title:The Site-url:http://example.com'
105
- end
106
-
107
- it 'calculates the public key' do
108
- subject.public_key.must_equal 'b55142f9fdb148e8844e37e064e8eb2af6aabac6'
109
- end
110
-
111
- it 'calculates the public key with no options given' do
112
- url_generator = DocJuan::UrlGenerator.new 'http://example.com', 'file.pdf'
113
- key = url_generator.public_key
114
-
115
- key.must_equal '539ebb1f6cd3fec40591acdc756e9b047e7093b3'
116
- end
117
-
118
89
  end
@@ -13,12 +13,22 @@ describe 'DocJuan#url' do
13
13
  generator = stub
14
14
  generator.expects(:generate).returns 'generated-url'
15
15
  DocJuan::UrlGenerator.expects(:new).
16
- with('http://example.com', 'the_file.pdf', size: 'A4').
16
+ with('http://example.com', 'the_file', nil, size: 'A4').
17
17
  returns(generator)
18
18
 
19
- url = DocJuan.url 'http://example.com', 'the_file.pdf', size: 'A4'
19
+ url = DocJuan.url 'http://example.com', 'the_file', size: 'A4'
20
20
 
21
21
  url.must_equal 'generated-url'
22
22
  end
23
23
 
24
+ it 'passes format if set to UrlGenerator instance' do
25
+ generator = stub
26
+ generator.expects(:generate)
27
+ DocJuan::UrlGenerator.expects(:new).
28
+ with('http://example.com', 'the_file', 'jpg', {}).
29
+ returns(generator)
30
+
31
+ DocJuan.url 'http://example.com', 'the_file', format: 'jpg'
32
+ end
33
+
24
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doc_juan
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-28 00:00:00.000000000 Z
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A small helper class to generate urls to a Doc-Juan instance
15
15
  email:
@@ -26,11 +26,13 @@ files:
26
26
  - doc_juan.gemspec
27
27
  - lib/doc_juan.rb
28
28
  - lib/doc_juan/config.rb
29
+ - lib/doc_juan/token.rb
29
30
  - lib/doc_juan/url_generator.rb
30
31
  - lib/doc_juan/url_helper.rb
31
32
  - lib/doc_juan/version.rb
32
33
  - spec/configuration_spec.rb
33
34
  - spec/spec_helper.rb
35
+ - spec/token_spec.rb
34
36
  - spec/url_generator_spec.rb
35
37
  - spec/url_helper_spec.rb
36
38
  homepage: https://github.com/Oktavilla/Doc-Juan-Helper
@@ -45,21 +47,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
47
  - - ! '>='
46
48
  - !ruby/object:Gem::Version
47
49
  version: '0'
48
- segments:
49
- - 0
50
- hash: -3331118814664929665
51
50
  required_rubygems_version: !ruby/object:Gem::Requirement
52
51
  none: false
53
52
  requirements:
54
53
  - - ! '>='
55
54
  - !ruby/object:Gem::Version
56
55
  version: '0'
57
- segments:
58
- - 0
59
- hash: -3331118814664929665
60
56
  requirements: []
61
57
  rubyforge_project:
62
- rubygems_version: 1.8.10
58
+ rubygems_version: 1.8.24
63
59
  signing_key:
64
60
  specification_version: 3
65
61
  summary: Given a url and options the DocJuan generates a url to a DocJuan server including
@@ -67,5 +63,6 @@ summary: Given a url and options the DocJuan generates a url to a DocJuan server
67
63
  test_files:
68
64
  - spec/configuration_spec.rb
69
65
  - spec/spec_helper.rb
66
+ - spec/token_spec.rb
70
67
  - spec/url_generator_spec.rb
71
68
  - spec/url_helper_spec.rb