doc_juan 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ Gemfile.lock
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in doc_juan.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ gem 'minitest', '~> 3.1.0'
9
+ gem 'minitest-reporters', '~> 0.7.1'
10
+ gem 'mocha', '~> 0.11.4', require: false
11
+ end
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
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:spec) do |test|
7
+ test.libs << 'lib' << 'spec'
8
+ test.pattern = 'spec/**/*_spec.rb'
9
+ test.verbose = true
10
+ end
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,15 @@
1
+ module DocJuan
2
+
3
+ def self.configure
4
+ yield config
5
+ end
6
+
7
+ def self.config
8
+ @config ||= Configuration.new
9
+ end
10
+
11
+ class Configuration
12
+ attr_accessor :host, :secret
13
+ end
14
+
15
+ 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
@@ -0,0 +1,8 @@
1
+ require_relative 'url_generator'
2
+
3
+ module DocJuan
4
+ def self.url url, filename, options = {}
5
+ generator = UrlGenerator.new url, filename, options
6
+ generator.generate
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module DocJuan
2
+ VERSION = "1.0.0"
3
+ end
data/lib/doc_juan.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "doc_juan/version"
2
+ require "doc_juan/config"
3
+ require "doc_juan/url_generator"
4
+ require "doc_juan/url_helper"
@@ -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
@@ -0,0 +1,7 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/reporters'
5
+
6
+ MiniTest::Unit.runner = MiniTest::SuiteRunner.new
7
+ MiniTest::Unit.runner.reporters << MiniTest::Reporters::SpecReporter.new
@@ -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