doc_juan 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +20 -0
- data/Gemfile +11 -0
- data/LICENSE +22 -0
- data/README.md +63 -0
- data/Rakefile +10 -0
- data/doc_juan.gemspec +17 -0
- data/lib/doc_juan/config.rb +15 -0
- data/lib/doc_juan/url_generator.rb +62 -0
- data/lib/doc_juan/url_helper.rb +8 -0
- data/lib/doc_juan/version.rb +3 -0
- data/lib/doc_juan.rb +4 -0
- data/spec/configuration_spec.rb +15 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/url_generator_spec.rb +73 -0
- data/spec/url_helper_spec.rb +24 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joel Junström, Oktavilla
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Doc-Juan's Helper
|
2
|
+
|
3
|
+
A small helper class to generate urls to a Doc-Juan instance.
|
4
|
+
|
5
|
+
Give a url, a filename and options the DocJuan to generate a url to a DocJuan server including the calculated hmac key.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
### Required configuration
|
10
|
+
|
11
|
+
You need to provide your secret key and the host of the Doc-Juan instance.
|
12
|
+
|
13
|
+
DocJuan.configure do |config|
|
14
|
+
config.host = 'doc-juan.it'
|
15
|
+
config.secret = 'my-special-secret'
|
16
|
+
end
|
17
|
+
|
18
|
+
If you are using Rails an initializer would be the appropriate place for this.
|
19
|
+
|
20
|
+
|
21
|
+
### Generating urls
|
22
|
+
|
23
|
+
The `DocJuan.url` method generates a url to your Doc-Juan instance. It takes an url and a filename as the required arguments. You can supply an (optional) hash of options as the third argument.
|
24
|
+
|
25
|
+
Valid options are:
|
26
|
+
|
27
|
+
* `title` - PDF title, defaults to the title of the HTML document.
|
28
|
+
* `print_stylesheet` - If set to `true` the print stylesheet of the resource will be used.
|
29
|
+
* `width` - Page width in millimeters.
|
30
|
+
* `height` - Page height in millimeters.
|
31
|
+
* `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
|
+
* `orientation` - `landscape` or `portrait`. Defaults to portrait.
|
33
|
+
* `lowquality` - Renders the pdf in low quality if set to `true`
|
34
|
+
|
35
|
+
#### Example
|
36
|
+
|
37
|
+
url = DocJuan.url 'http://example.com', 'example.pdf'
|
38
|
+
|
39
|
+
url = DocJuan.url 'http://example.com', 'le-pdf.pdf', title: 'My PDF-version', size: 'A5'
|
40
|
+
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
gem 'doc_juan'
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install doc_juan
|
55
|
+
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/doc_juan.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/doc_juan/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Joel Junström"]
|
6
|
+
gem.email = ["joel.junstrom@oktavilla.se"]
|
7
|
+
gem.description = %q{A small helper class to generate urls to a Doc-Juan instance}
|
8
|
+
gem.summary = %q{Given a url and options the DocJuan generates a url to a DocJuan server including the calculated hmac key}
|
9
|
+
gem.homepage = "https://github.com/Oktavilla/Doc-Juan-Helper"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "doc_juan"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = DocJuan::VERSION
|
17
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
require_relative 'config'
|
5
|
+
|
6
|
+
module DocJuan
|
7
|
+
class NoHostGivenError < StandardError; end
|
8
|
+
class NoSecretGivenError < StandardError; end
|
9
|
+
|
10
|
+
class UrlGenerator
|
11
|
+
attr_reader :url, :filename, :options
|
12
|
+
|
13
|
+
def initialize url, filename, options = {}
|
14
|
+
@url = url
|
15
|
+
@filename = filename.to_s
|
16
|
+
@options = Hash[(options || {}).sort]
|
17
|
+
|
18
|
+
raise NoSecretGivenError if secret_key == ''
|
19
|
+
raise NoHostGivenError if host == ''
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate
|
23
|
+
params = []
|
24
|
+
params << "url=#{CGI.escape(url)}"
|
25
|
+
params << "filename=#{CGI.escape(filename)}"
|
26
|
+
options.each do |k,v|
|
27
|
+
params << "options[#{CGI.escape(k.to_s)}]=#{CGI.escape v}"
|
28
|
+
end
|
29
|
+
params << "key=#{public_key}"
|
30
|
+
|
31
|
+
"#{host}/render?#{params.join('&')}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def public_key
|
35
|
+
sha1 = OpenSSL::Digest::Digest.new 'sha1'
|
36
|
+
OpenSSL::HMAC.hexdigest sha1, secret_key, seed_string
|
37
|
+
end
|
38
|
+
|
39
|
+
def seed_string
|
40
|
+
seed = []
|
41
|
+
seed << "filename:#{filename}"
|
42
|
+
options.each do |k,v|
|
43
|
+
seed << "options_#{k}:#{v}"
|
44
|
+
end
|
45
|
+
seed << "url:#{url}"
|
46
|
+
|
47
|
+
seed.join '-'
|
48
|
+
end
|
49
|
+
|
50
|
+
def host
|
51
|
+
@host ||= DocJuan.config.host.to_s.strip.tap do |host|
|
52
|
+
if host != '' && ! host.start_with?('http')
|
53
|
+
host.replace "http://#{host}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def secret_key
|
59
|
+
DocJuan.config.secret.to_s.strip
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/doc_juan.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
require_relative '../lib/doc_juan/config.rb'
|
4
|
+
|
5
|
+
describe DocJuan::Configuration do
|
6
|
+
it 'is configurable with a block' do
|
7
|
+
DocJuan.configure do |config|
|
8
|
+
config.secret = 'very-secret'
|
9
|
+
config.host = 'http://my-doc-juan-host.com'
|
10
|
+
end
|
11
|
+
|
12
|
+
DocJuan.config.secret.must_equal 'very-secret'
|
13
|
+
DocJuan.config.host.must_equal 'http://my-doc-juan-host.com'
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
require_relative '../lib/doc_juan/config.rb'
|
4
|
+
require_relative '../lib/doc_juan/url_generator.rb'
|
5
|
+
|
6
|
+
describe DocJuan::UrlGenerator do
|
7
|
+
before :each do
|
8
|
+
DocJuan.config.host = 'doc-juan.example.com'
|
9
|
+
DocJuan.config.secret = 'zecret'
|
10
|
+
end
|
11
|
+
|
12
|
+
subject do
|
13
|
+
DocJuan::UrlGenerator.new('http://example.com', 'file.pdf', {
|
14
|
+
title: 'The Site',
|
15
|
+
size: 'A4'
|
16
|
+
})
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'generates the url' do
|
20
|
+
url = subject.generate
|
21
|
+
|
22
|
+
expected = 'http://doc-juan.example.com/render?'
|
23
|
+
expected << "url=#{CGI.escape subject.url}"
|
24
|
+
expected << "&filename=#{CGI.escape subject.filename}"
|
25
|
+
expected << "&options[size]=A4"
|
26
|
+
expected << "&options[title]=#{CGI.escape 'The Site'}"
|
27
|
+
expected << "&key=#{subject.public_key}"
|
28
|
+
|
29
|
+
url.must_equal expected
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'has the host' do
|
33
|
+
DocJuan.config.host = 'example.com'
|
34
|
+
|
35
|
+
subject.host.must_equal 'http://example.com'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'fails with NoHostGivenError unless there is a host set' do
|
39
|
+
DocJuan.config.host = nil
|
40
|
+
|
41
|
+
proc {
|
42
|
+
subject
|
43
|
+
}.must_raise DocJuan::NoHostGivenError
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'has the secret key' do
|
47
|
+
subject.secret_key.must_equal 'zecret'
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'fails with NoSecretGivenError unless there is a secret set' do
|
51
|
+
DocJuan.config.secret = nil
|
52
|
+
|
53
|
+
proc {
|
54
|
+
subject
|
55
|
+
}.must_raise DocJuan::NoSecretGivenError
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'compiles into a seed string for the public key computation' do
|
59
|
+
subject.seed_string.must_equal 'filename:file.pdf-options_size:A4-options_title:The Site-url:http://example.com'
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'calculates the public key' do
|
63
|
+
subject.public_key.must_equal 'df9515da45dd0cd445a678928bc3e575cc106793'
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'calculates the public key with no options given' do
|
67
|
+
url_generator = DocJuan::UrlGenerator.new 'http://example.com', 'file.pdf'
|
68
|
+
key = url_generator.public_key
|
69
|
+
|
70
|
+
key.must_equal '539ebb1f6cd3fec40591acdc756e9b047e7093b3'
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
require_relative '../lib/doc_juan/url_helper'
|
5
|
+
|
6
|
+
describe 'DocJuan#url' do
|
7
|
+
before :each do
|
8
|
+
DocJuan.config.host = 'doc-juan.example.com'
|
9
|
+
DocJuan.config.secret = 'zecret'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'passes its arguments to a UrlGenerator instance and uses it to generate the url' do
|
13
|
+
generator = stub
|
14
|
+
generator.expects(:generate).returns 'generated-url'
|
15
|
+
DocJuan::UrlGenerator.expects(:new).
|
16
|
+
with('http://example.com', 'the_file.pdf', size: 'A4').
|
17
|
+
returns(generator)
|
18
|
+
|
19
|
+
url = DocJuan.url 'http://example.com', 'the_file.pdf', size: 'A4'
|
20
|
+
|
21
|
+
url.must_equal 'generated-url'
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: doc_juan
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joel Junström
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-28 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A small helper class to generate urls to a Doc-Juan instance
|
15
|
+
email:
|
16
|
+
- joel.junstrom@oktavilla.se
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- doc_juan.gemspec
|
27
|
+
- lib/doc_juan.rb
|
28
|
+
- lib/doc_juan/config.rb
|
29
|
+
- lib/doc_juan/url_generator.rb
|
30
|
+
- lib/doc_juan/url_helper.rb
|
31
|
+
- lib/doc_juan/version.rb
|
32
|
+
- spec/configuration_spec.rb
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
- spec/url_generator_spec.rb
|
35
|
+
- spec/url_helper_spec.rb
|
36
|
+
homepage: https://github.com/Oktavilla/Doc-Juan-Helper
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
hash: -3357457256720695940
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
hash: -3357457256720695940
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Given a url and options the DocJuan generates a url to a DocJuan server including
|
66
|
+
the calculated hmac key
|
67
|
+
test_files:
|
68
|
+
- spec/configuration_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
- spec/url_generator_spec.rb
|
71
|
+
- spec/url_helper_spec.rb
|