icanhazpdf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +68 -2
- data/Guardfile +33 -0
- data/README.md +69 -16
- data/Rakefile +9 -1
- data/icanhazpdf.gemspec +4 -0
- data/lib/i_can_haz_pdf.rb +2 -47
- data/lib/i_can_haz_pdf/client.rb +38 -0
- data/lib/i_can_haz_pdf/controller.rb +44 -0
- data/lib/i_can_haz_pdf/version.rb +1 -1
- data/spec/lib/i_can_haz_pdf/client_spec.rb +97 -0
- data/spec/lib/i_can_haz_pdf/controller_spec.rb +143 -0
- data/spec/spec_helper.rb +1 -0
- metadata +77 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1186b80b82b56eb8a535e717497bbcd898d8891b
|
4
|
+
data.tar.gz: 70ca09023da1bda53aa73f142816f1bd394607ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b991aff701eb9af0fc7ae3a7bad40157d888dcbfdd2446f595e6ad2cb6212f5e428c444c629b3f72c3d2f3a2a82a19ebadf66af71d8aeb9364ee506a81cfc5cf
|
7
|
+
data.tar.gz: 90d718616f829f512129f22d5f756e513f7f49e857c4f20d6bf94f513c431e615ff6c5c69c405e5abbb69eed8c752b7e9096f1f89ad5f5aad7d574e97c320a8f
|
data/Gemfile.lock
CHANGED
@@ -1,17 +1,83 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
icanhazpdf (0.0.
|
4
|
+
icanhazpdf (0.0.2)
|
5
|
+
activesupport (~> 4.0)
|
6
|
+
httparty (~> 0.13)
|
5
7
|
|
6
8
|
GEM
|
7
9
|
remote: https://rubygems.org/
|
8
10
|
specs:
|
11
|
+
activesupport (4.1.6)
|
12
|
+
i18n (~> 0.6, >= 0.6.9)
|
13
|
+
json (~> 1.7, >= 1.7.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
thread_safe (~> 0.1)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
celluloid (0.16.0)
|
18
|
+
timers (~> 4.0.0)
|
19
|
+
coderay (1.1.0)
|
20
|
+
diff-lcs (1.2.5)
|
21
|
+
ffi (1.9.3)
|
22
|
+
formatador (0.2.5)
|
23
|
+
guard (2.6.1)
|
24
|
+
formatador (>= 0.2.4)
|
25
|
+
listen (~> 2.7)
|
26
|
+
lumberjack (~> 1.0)
|
27
|
+
pry (>= 0.9.12)
|
28
|
+
thor (>= 0.18.1)
|
29
|
+
guard-rspec (4.3.1)
|
30
|
+
guard (~> 2.1)
|
31
|
+
rspec (>= 2.14, < 4.0)
|
32
|
+
hitimes (1.2.2)
|
33
|
+
httparty (0.13.1)
|
34
|
+
json (~> 1.8)
|
35
|
+
multi_xml (>= 0.5.2)
|
36
|
+
i18n (0.6.11)
|
37
|
+
json (1.8.1)
|
38
|
+
listen (2.7.9)
|
39
|
+
celluloid (>= 0.15.2)
|
40
|
+
rb-fsevent (>= 0.9.3)
|
41
|
+
rb-inotify (>= 0.9)
|
42
|
+
lumberjack (1.0.9)
|
43
|
+
method_source (0.8.2)
|
44
|
+
minitest (5.4.1)
|
45
|
+
multi_xml (0.5.5)
|
46
|
+
pry (0.10.1)
|
47
|
+
coderay (~> 1.1.0)
|
48
|
+
method_source (~> 0.8.1)
|
49
|
+
slop (~> 3.4)
|
9
50
|
rake (10.3.2)
|
51
|
+
rb-fsevent (0.9.4)
|
52
|
+
rb-inotify (0.9.5)
|
53
|
+
ffi (>= 0.5.0)
|
54
|
+
rspec (3.1.0)
|
55
|
+
rspec-core (~> 3.1.0)
|
56
|
+
rspec-expectations (~> 3.1.0)
|
57
|
+
rspec-mocks (~> 3.1.0)
|
58
|
+
rspec-core (3.1.3)
|
59
|
+
rspec-support (~> 3.1.0)
|
60
|
+
rspec-expectations (3.1.1)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.1.0)
|
63
|
+
rspec-mocks (3.1.0)
|
64
|
+
rspec-support (~> 3.1.0)
|
65
|
+
rspec-support (3.1.0)
|
66
|
+
slop (3.6.0)
|
67
|
+
thor (0.19.1)
|
68
|
+
thread_safe (0.3.4)
|
69
|
+
timers (4.0.1)
|
70
|
+
hitimes
|
71
|
+
tzinfo (1.2.2)
|
72
|
+
thread_safe (~> 0.1)
|
10
73
|
|
11
74
|
PLATFORMS
|
12
75
|
ruby
|
13
76
|
|
14
77
|
DEPENDENCIES
|
15
78
|
bundler (~> 1.6)
|
79
|
+
guard-rspec (~> 4.3)
|
16
80
|
icanhazpdf!
|
17
|
-
|
81
|
+
pry (~> 0.10)
|
82
|
+
rake (~> 10.3)
|
83
|
+
rspec (~> 3.0)
|
data/Guardfile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
5
|
+
# rspec may be run, below are examples of the most common uses.
|
6
|
+
# * bundler: 'bundle exec rspec'
|
7
|
+
# * bundler binstubs: 'bin/rspec'
|
8
|
+
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
9
|
+
# installed the spring binstubs per the docs)
|
10
|
+
# * zeus: 'zeus rspec' (requires the server to be started separetly)
|
11
|
+
# * 'just' rspec: 'rspec'
|
12
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
13
|
+
watch(%r{^spec/.+_spec\.rb$})
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
|
17
|
+
# Rails example
|
18
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
+
watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
20
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
21
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
22
|
+
watch('config/routes.rb') { "spec/routing" }
|
23
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
24
|
+
watch('spec/rails_helper.rb') { "spec" }
|
25
|
+
|
26
|
+
# Capybara features specs
|
27
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
28
|
+
|
29
|
+
# Turnip features and steps
|
30
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
31
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
32
|
+
end
|
33
|
+
|
data/README.md
CHANGED
@@ -1,29 +1,82 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
TODO: Write a gem description
|
1
|
+
# ICanHazPDF?
|
2
|
+
Makes using the ICanHazPdf in a Rails app simples
|
4
3
|
|
5
4
|
## Installation
|
6
5
|
|
7
6
|
Add this line to your application's Gemfile:
|
8
7
|
|
9
|
-
gem 'icanhazpdf'
|
8
|
+
gem 'icanhazpdf', '~> 0.0.2'
|
9
|
+
|
10
|
+
## Client
|
11
|
+
|
12
|
+
To request a pdf is generated (and sent back to you synchronously) use the
|
13
|
+
api Client class.
|
14
|
+
|
15
|
+
pdf_response = ICanHazPdf::Client.new.pdf_from_url my_resource_url(resource)
|
16
|
+
|
17
|
+
Your api key is automatically appended to the request.
|
18
|
+
The response is the web response from HTTParty. You can then render this or save
|
19
|
+
it or do what you like with it. The PDF is in the body of the response - check
|
20
|
+
the pdf_response.code to ensure it was successful (should be 200).
|
21
|
+
|
22
|
+
## Controller
|
23
|
+
|
24
|
+
To render a pdf (send the file to the browser) from a url use the renderer module.
|
25
|
+
|
26
|
+
class MyController < ApplicationController
|
27
|
+
include ICanHazPdf::Controller::Renderer
|
28
|
+
|
29
|
+
def my_action
|
30
|
+
render_pdf_from my_resource_url(resource), 'myfilename.pdf'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
The method takes a url from which to render the pdf and a filename - which is sent
|
35
|
+
to the browser when it saves the file. You can omit the filename and a default
|
36
|
+
filename is used.
|
37
|
+
|
38
|
+
To render a pdf using a previous response from the Client use:
|
39
|
+
|
40
|
+
class MyController < ApplicationController
|
41
|
+
include ICanHazPdf::Controller::Renderer
|
42
|
+
|
43
|
+
def my_action
|
44
|
+
pdf_response = ICanHazPdf::Client.new.pdf_from_url my_resource_url(resource)
|
45
|
+
|
46
|
+
# do something with the response? save somewhere? upload somewhere?
|
47
|
+
|
48
|
+
render_response_for pdf_response, {filename: 'myfilename.pdf'}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
## Authentication
|
53
|
+
|
54
|
+
Checks the current request being made is coming from the icanhazpdf service and
|
55
|
+
has your configured icanhazapi key in the parameters
|
56
|
+
|
57
|
+
require 'i_can_haz_pdf/controller'
|
58
|
+
|
59
|
+
class Api::ApiController < ActionController::Base
|
60
|
+
include ICanHazPdf::Controller::Authentication
|
10
61
|
|
11
|
-
|
62
|
+
before_filter :authenticate
|
12
63
|
|
13
|
-
|
64
|
+
private
|
14
65
|
|
15
|
-
|
66
|
+
def authenticate
|
67
|
+
head 401 unless authenticate_as_icanhazpdf
|
68
|
+
end
|
69
|
+
end
|
16
70
|
|
17
|
-
|
71
|
+
### With devise
|
18
72
|
|
19
|
-
|
73
|
+
Checks if the request is either from icanhazpdf or if that fails falls back to
|
74
|
+
devise authentication
|
20
75
|
|
21
|
-
|
76
|
+
require 'i_can_haz_pdf/controller'
|
22
77
|
|
23
|
-
|
78
|
+
class Api::ApiController < ActionController::Base
|
79
|
+
include ICanHazPdf::Controller::Authentication
|
24
80
|
|
25
|
-
|
26
|
-
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create a new Pull Request
|
81
|
+
before_filter :authenticate_as_icanhazpdf_or_authenticate_user!
|
82
|
+
end
|
data/Rakefile
CHANGED
@@ -1,2 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require 'bundler/gem_tasks'
|
2
3
|
|
4
|
+
# Default directory to look in is `/specs`
|
5
|
+
# Run with `rake spec`
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
7
|
+
task.rspec_opts = ['--color', '--format', 'documentation']
|
8
|
+
end
|
9
|
+
|
10
|
+
task :default => :spec
|
data/icanhazpdf.gemspec
CHANGED
@@ -20,6 +20,10 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.3"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "guard-rspec", "~> 4.3"
|
25
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
23
26
|
|
27
|
+
spec.add_runtime_dependency 'activesupport', "~> 4.0"
|
24
28
|
spec.add_runtime_dependency 'httparty', "~> 0.13"
|
25
29
|
end
|
data/lib/i_can_haz_pdf.rb
CHANGED
@@ -1,48 +1,3 @@
|
|
1
1
|
require "i_can_haz_pdf/version"
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# include in order to request generation of pdfs from the icanhazpf service
|
6
|
-
# requires that icanhazpdf_api_key is configured in your environment config
|
7
|
-
module Client
|
8
|
-
# your icanhazpf api key
|
9
|
-
def self.api_key
|
10
|
-
Rails.configuration.icanhazpdf_api_key
|
11
|
-
end
|
12
|
-
|
13
|
-
# generate a pdf from the url passed
|
14
|
-
def pdf_from_url(full_url)
|
15
|
-
uri = URI(full_url)
|
16
|
-
params = URI.decode_www_form(uri.query || "") << ['icanhazpdf', ICanHazPdf::Client::api_key]
|
17
|
-
uri.query = URI.encode_www_form(params)
|
18
|
-
encoded_url = "#{Rails.configuration.icanhazpdf_url}/#{URI.encode(uri.to_s).gsub(':', '%3A').gsub('/', '%2F').gsub('?', '%3F')}"
|
19
|
-
HTTParty.get(encoded_url, :timeout => 10000)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# include in a controller and use to render pdfs or authenticate requests
|
24
|
-
# are from icanhazpf
|
25
|
-
module Renderer
|
26
|
-
# send the pdf to the user if its a valid file
|
27
|
-
# optionally pass the filename in the options hash
|
28
|
-
# raises an exception if something went wrong
|
29
|
-
def render_response_for(pdf_response, options = {})
|
30
|
-
raise "Failed to generate pdf:\nCode: #{pdf_response.code}\nBody:\n#{pdf_response.body}" unless pdf_response.code == 200
|
31
|
-
|
32
|
-
filename = options.has_key?(:filename) ? options[:filename] : "#{DateTime.now.to_formatted_s(:number)}-icanhaz.pdf"
|
33
|
-
send_data pdf_response, :filename => filename, :type => :pdf
|
34
|
-
end
|
35
|
-
|
36
|
-
# true if the request includes the correct icanhazpdf api key
|
37
|
-
def authenticate_as_icanhazpdf
|
38
|
-
return false unless params[:icanhazpdf].present?
|
39
|
-
return params[:icanhazpdf] == ICanHazPdf::Client::api_key
|
40
|
-
end
|
41
|
-
|
42
|
-
# attemps to authenticate as icanhazpdf and falls back to devise
|
43
|
-
def authenticate_as_icanhazpdf_or_authenticate_user!
|
44
|
-
authenticate_as_icanhazpdf || authenticate_user!
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
2
|
+
require "i_can_haz_pdf/client"
|
3
|
+
require "i_can_haz_pdf/controller"
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
module ICanHazPdf
|
3
|
+
|
4
|
+
# include in order to request generation of pdfs from the icanhazpf service
|
5
|
+
# requires that icanhazpdf_api_key is configured in your environment config
|
6
|
+
class Client
|
7
|
+
|
8
|
+
# your icanhazpf api key
|
9
|
+
def self.api_key
|
10
|
+
begin
|
11
|
+
Rails.configuration.icanhazpdf_api_key
|
12
|
+
rescue
|
13
|
+
raise "No API Key Configured"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.default_service_url
|
18
|
+
'http://icanhazpdf.lsfapp.com'
|
19
|
+
end
|
20
|
+
|
21
|
+
# generate a pdf from the url passed
|
22
|
+
def pdf_from_url(full_url)
|
23
|
+
uri = URI(full_url)
|
24
|
+
params = URI.decode_www_form(uri.query || "") << ['icanhazpdf', ICanHazPdf::Client::api_key]
|
25
|
+
uri.query = URI.encode_www_form(params)
|
26
|
+
begin
|
27
|
+
service_url = Rails.configuration.icanhazpdf_url
|
28
|
+
rescue
|
29
|
+
service_url = ICanHazPdf::Client.default_service_url
|
30
|
+
end
|
31
|
+
encoded_url = "#{service_url}/#{URI.encode(uri.to_s).gsub(':', '%3A').gsub('/', '%2F').gsub('?', '%3F').gsub('=', '%3D')}"
|
32
|
+
|
33
|
+
HTTParty.get(encoded_url, :timeout => 10000)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'i_can_haz_pdf/client'
|
2
|
+
require 'active_support/core_ext/date_time/conversions'
|
3
|
+
|
4
|
+
module ICanHazPdf
|
5
|
+
|
6
|
+
module Controller
|
7
|
+
# include in a controller and use to render pdfs or authenticate requests
|
8
|
+
# are from icanhazpf
|
9
|
+
module Renderer
|
10
|
+
|
11
|
+
# generate and render a pdf from a url
|
12
|
+
def render_pdf_from(url, filename = "")
|
13
|
+
options = {}
|
14
|
+
options[:filename] = filename if filename.present?
|
15
|
+
render_response_for ICanHazPdf::Client.new.pdf_from_url(url), options
|
16
|
+
end
|
17
|
+
|
18
|
+
# send the pdf to the user if its a valid file
|
19
|
+
# optionally pass the filename in the options hash
|
20
|
+
# raises an exception if something went wrong
|
21
|
+
def render_response_for(pdf_response, options = {})
|
22
|
+
raise "Failed to generate pdf:\nCode: #{pdf_response.code}\nBody:\n#{pdf_response.body}" unless pdf_response.code == 200
|
23
|
+
|
24
|
+
filename = options.has_key?(:filename) ? options[:filename] : "#{DateTime.now.to_formatted_s(:number)}-icanhaz.pdf"
|
25
|
+
send_data pdf_response, :filename => filename, :type => :pdf
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
module Authentication
|
31
|
+
# true if the request includes the correct icanhazpdf api key
|
32
|
+
def authenticate_as_icanhazpdf
|
33
|
+
return false unless params[:icanhazpdf].present?
|
34
|
+
return params[:icanhazpdf] == ICanHazPdf::Client::api_key
|
35
|
+
end
|
36
|
+
|
37
|
+
# attemps to authenticate as icanhazpdf and falls back to devise
|
38
|
+
def authenticate_as_icanhazpdf_or_authenticate_user!
|
39
|
+
authenticate_as_icanhazpdf || authenticate_user!
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'httparty'
|
3
|
+
|
4
|
+
describe 'ICanHazPdf::Client' do
|
5
|
+
|
6
|
+
let(:a_url) { "http://a.url.to/generate_a_pdf_from?with=querystring" }
|
7
|
+
subject { ICanHazPdf::Client.new }
|
8
|
+
|
9
|
+
describe 'generating a pdf from' do
|
10
|
+
let(:http_response) { double("http_response") }
|
11
|
+
let(:icanhazpdf_service_url) { 'http://icanhazpdf.service.url' }
|
12
|
+
let(:rails_config) { double 'rails_config' }
|
13
|
+
let(:icanhazpdf_api_key) { 'abcderfgkjdjhjdh1376375' }
|
14
|
+
|
15
|
+
before(:each) do
|
16
|
+
Rails = class_double('Rails').as_stubbed_const
|
17
|
+
allow(rails_config).to receive(:icanhazpdf_api_key).and_return(icanhazpdf_api_key)
|
18
|
+
allow(rails_config).to receive(:icanhazpdf_url).and_return(icanhazpdf_service_url)
|
19
|
+
allow(Rails).to receive(:configuration).and_return(rails_config)
|
20
|
+
allow(HTTParty).to receive(:get).and_return(http_response)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'building the request for icanhazpdf' do
|
24
|
+
|
25
|
+
it 'uses the icanhazpdf service url from the rails config' do
|
26
|
+
expect(HTTParty).to receive(:get).with(include(icanhazpdf_service_url), be_an(Hash))
|
27
|
+
@result = subject.pdf_from_url a_url
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'appends the api key on the request to icanhazpdf' do
|
31
|
+
expect(HTTParty).to receive(:get).with(include("icanhazpdf%3D#{icanhazpdf_api_key}"), be_an(Hash))
|
32
|
+
@result = subject.pdf_from_url a_url
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'url encodes the address of the page to create a pdf from' do
|
36
|
+
expect(HTTParty).to receive(:get).with(include("http%3A%2F%2Fa.url.to%2Fgenerate_a_pdf_from%3Fwith%3Dquerystring"), be_an(Hash))
|
37
|
+
@result = subject.pdf_from_url a_url
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'sets the timeout to 10 seconds' do
|
41
|
+
expect(HTTParty).to receive(:get).with(be_a(String), hash_including(timeout: 10000))
|
42
|
+
@result = subject.pdf_from_url a_url
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'no icanhazpdf api key in the config' do
|
46
|
+
before(:each) do
|
47
|
+
allow(rails_config).to receive(:icanhazpdf_api_key).and_raise("Undefined config value")
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'raises an error' do
|
51
|
+
expect{subject.pdf_from_url a_url}.to raise_error("No API Key Configured")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'no icanhazpdf service url in the config' do
|
56
|
+
before(:each) do
|
57
|
+
allow(rails_config).to receive(:icanhazpdf_url).and_raise("Undefined config value")
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'uses the default service url' do
|
61
|
+
expect(HTTParty).to receive(:get).with(include(ICanHazPdf::Client.default_service_url), be_an(Hash))
|
62
|
+
@result = subject.pdf_from_url a_url
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'requests pdf from icanhazpdf' do
|
68
|
+
|
69
|
+
context 'a valid page to generate a pdf from' do
|
70
|
+
let(:http_status) { 200 }
|
71
|
+
let(:http_body) { "The pdf content" }
|
72
|
+
|
73
|
+
before(:each) do
|
74
|
+
allow(http_response).to receive(:status).and_return http_status
|
75
|
+
allow(http_response).to receive(:body).and_return http_body
|
76
|
+
@result = subject.pdf_from_url a_url
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should return whatever httparty returns as a status' do
|
80
|
+
expect(@result.status).to eq(http_status)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return whatever httparty returns as the body' do
|
84
|
+
expect(@result.body).to eq(http_body)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'an invalid url' do
|
89
|
+
it 'should raise an error' do
|
90
|
+
expect{subject.pdf_from_url 'an27632&@^&^£%^£?_invalid_url_3746376&^^%$'}.to raise_error
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "ICanHazPdf::Controller::Renderer" do
|
4
|
+
|
5
|
+
class DummyRailsController
|
6
|
+
include ICanHazPdf::Controller::Renderer
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:url) { 'http://google.com' }
|
10
|
+
let(:filename) { 'ascreenshot.pdf' }
|
11
|
+
let(:http_response) { double('response') }
|
12
|
+
let(:client) { double('client') }
|
13
|
+
let(:http_status) { 200 }
|
14
|
+
let(:http_body) { "pdf content" }
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
allow(ICanHazPdf::Client).to receive(:new).and_return(client)
|
18
|
+
allow(client).to receive(:pdf_from_url).and_return(http_response)
|
19
|
+
allow(http_response).to receive(:code).and_return http_status
|
20
|
+
allow(http_response).to receive(:body).and_return http_body
|
21
|
+
allow(subject).to receive(:send_data)
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { DummyRailsController.new }
|
25
|
+
|
26
|
+
describe 'rendering a pdf from a url' do
|
27
|
+
|
28
|
+
it 'creates a client to request the pdf' do
|
29
|
+
expect(ICanHazPdf::Client).to receive(:new)
|
30
|
+
subject.render_pdf_from url, filename
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'requests the pdf from the client supplying the url' do
|
34
|
+
expect(client).to receive(:pdf_from_url).with(url)
|
35
|
+
subject.render_pdf_from url, filename
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'calls render response with the output of the client and the filename supplied' do
|
39
|
+
expect(subject).to receive(:render_response_for).with(http_response, {filename: filename})
|
40
|
+
subject.render_pdf_from url, filename
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'no filename supplied' do
|
44
|
+
it 'calls render response with the output of the client and no options' do
|
45
|
+
expect(subject).to receive(:render_response_for).with(http_response, {})
|
46
|
+
subject.render_pdf_from url
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'rendering the response from the client' do
|
52
|
+
|
53
|
+
it "sends the file using the controller's send data method specifying file type and name" do
|
54
|
+
expect(subject).to receive(:send_data).with(http_response, {filename: filename, type: :pdf})
|
55
|
+
subject.render_response_for http_response, {filename: filename}
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when the pdf response is not success' do
|
59
|
+
let(:http_status) { 400 }
|
60
|
+
let(:http_body) { "error: no pdf content" }
|
61
|
+
|
62
|
+
it 'raises an error' do
|
63
|
+
expect{subject.render_response_for http_response}.to raise_error
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when no filename is supplied' do
|
68
|
+
let(:the_date_and_time_now) { DateTime.now }
|
69
|
+
before(:each) do
|
70
|
+
allow(DateTime).to receive(:now).and_return(the_date_and_time_now)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'uses the current date to provide a filename' do
|
74
|
+
expect(subject).to receive(:send_data).with(http_response, {filename: "#{the_date_and_time_now.to_formatted_s(:number)}-icanhaz.pdf", type: :pdf})
|
75
|
+
subject.render_response_for http_response
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "ICanHazPdf::Controller::Authentication" do
|
83
|
+
|
84
|
+
class DummyRailsController
|
85
|
+
include ICanHazPdf::Controller::Authentication
|
86
|
+
end
|
87
|
+
|
88
|
+
subject { DummyRailsController.new }
|
89
|
+
|
90
|
+
let(:the_api_key) { '34765236754673256735' }
|
91
|
+
|
92
|
+
before(:each) do
|
93
|
+
allow(ICanHazPdf::Client).to receive(:api_key).and_return(the_api_key)
|
94
|
+
allow(subject).to receive(:params).and_return(params)
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'authenticating that a request is from icanhazpdf' do
|
98
|
+
|
99
|
+
context 'request params collection includes the api key' do
|
100
|
+
let(:params) { {icanhazpdf: the_api_key} }
|
101
|
+
|
102
|
+
it 'returns true' do
|
103
|
+
expect(subject.authenticate_as_icanhazpdf).to eq(true)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'request params collection does not include the icanhazpdf parameter' do
|
108
|
+
let(:params) { {} }
|
109
|
+
|
110
|
+
it 'returns false' do
|
111
|
+
expect(subject.authenticate_as_icanhazpdf).to eq(false)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'request params has an invalid api key' do
|
116
|
+
let(:params) { {icanhazpdf: '374636754365436'} }
|
117
|
+
|
118
|
+
it 'returns false' do
|
119
|
+
expect(subject.authenticate_as_icanhazpdf).to eq(false)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'authenticate request is either from icanhazpdf or fall back to devise' do
|
125
|
+
|
126
|
+
context 'request is from icanhazpdf' do
|
127
|
+
let(:params) { {icanhazpdf: the_api_key} }
|
128
|
+
|
129
|
+
it 'returns true' do
|
130
|
+
expect(subject.authenticate_as_icanhazpdf_or_authenticate_user!).to eq(true)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'request is not from icanhazpdf' do
|
135
|
+
let(:params) { {} }
|
136
|
+
|
137
|
+
it 'falls back to devise' do
|
138
|
+
expect(subject).to receive(:authenticate_user!).and_return(false)
|
139
|
+
expect(subject.authenticate_as_icanhazpdf_or_authenticate_user!).to eq(false)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'i_can_haz_pdf'
|
metadata
CHANGED
@@ -1,55 +1,111 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icanhazpdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nic Pillinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.10'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.0'
|
41
97
|
- !ruby/object:Gem::Dependency
|
42
98
|
name: httparty
|
43
99
|
requirement: !ruby/object:Gem::Requirement
|
44
100
|
requirements:
|
45
|
-
- - ~>
|
101
|
+
- - "~>"
|
46
102
|
- !ruby/object:Gem::Version
|
47
103
|
version: '0.13'
|
48
104
|
type: :runtime
|
49
105
|
prerelease: false
|
50
106
|
version_requirements: !ruby/object:Gem::Requirement
|
51
107
|
requirements:
|
52
|
-
- - ~>
|
108
|
+
- - "~>"
|
53
109
|
- !ruby/object:Gem::Version
|
54
110
|
version: '0.13'
|
55
111
|
description: Rails gem for generating and serving pdfs using ICanHazPdf service
|
@@ -59,15 +115,21 @@ executables: []
|
|
59
115
|
extensions: []
|
60
116
|
extra_rdoc_files: []
|
61
117
|
files:
|
62
|
-
- .gitignore
|
118
|
+
- ".gitignore"
|
63
119
|
- Gemfile
|
64
120
|
- Gemfile.lock
|
121
|
+
- Guardfile
|
65
122
|
- LICENSE.txt
|
66
123
|
- README.md
|
67
124
|
- Rakefile
|
68
125
|
- icanhazpdf.gemspec
|
69
126
|
- lib/i_can_haz_pdf.rb
|
127
|
+
- lib/i_can_haz_pdf/client.rb
|
128
|
+
- lib/i_can_haz_pdf/controller.rb
|
70
129
|
- lib/i_can_haz_pdf/version.rb
|
130
|
+
- spec/lib/i_can_haz_pdf/client_spec.rb
|
131
|
+
- spec/lib/i_can_haz_pdf/controller_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
71
133
|
homepage: http://icanhazpdf.lsfapp.com
|
72
134
|
licenses:
|
73
135
|
- MIT
|
@@ -78,12 +140,12 @@ require_paths:
|
|
78
140
|
- lib
|
79
141
|
required_ruby_version: !ruby/object:Gem::Requirement
|
80
142
|
requirements:
|
81
|
-
- -
|
143
|
+
- - ">="
|
82
144
|
- !ruby/object:Gem::Version
|
83
145
|
version: '0'
|
84
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
147
|
requirements:
|
86
|
-
- -
|
148
|
+
- - ">="
|
87
149
|
- !ruby/object:Gem::Version
|
88
150
|
version: '0'
|
89
151
|
requirements: []
|
@@ -92,4 +154,7 @@ rubygems_version: 2.2.2
|
|
92
154
|
signing_key:
|
93
155
|
specification_version: 4
|
94
156
|
summary: ICanHazPdf Client
|
95
|
-
test_files:
|
157
|
+
test_files:
|
158
|
+
- spec/lib/i_can_haz_pdf/client_spec.rb
|
159
|
+
- spec/lib/i_can_haz_pdf/controller_spec.rb
|
160
|
+
- spec/spec_helper.rb
|