hyperpdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +1 -0
- data/hyperpdf.gemspec +20 -0
- data/lib/hyperpdf/exceptions.rb +13 -0
- data/lib/hyperpdf/version.rb +3 -0
- data/lib/hyperpdf.rb +67 -0
- data/spec/hyperpdf_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +60 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 redfield (up.redfield@gmail.com)
|
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,73 @@
|
|
1
|
+
# HyperPDF
|
2
|
+
|
3
|
+
Ruby wrapper around the [HyperPDF](https://addons.heroku.com/hyperpdf) API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hyperpdf'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hyperpdf
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Initialize new HyperPDF instance
|
22
|
+
|
23
|
+
pdf = HyperPDF.new('<htlm>...your html code here...</html>', user: "your_user_name", password: "your_password")
|
24
|
+
|
25
|
+
Get pdf document directly
|
26
|
+
|
27
|
+
# Write it to file
|
28
|
+
File.open('filename.pdf', 'wb') do |f|
|
29
|
+
f.write pdf.get
|
30
|
+
end
|
31
|
+
|
32
|
+
# Or send it to user (Rails example)
|
33
|
+
send_data(pdf.get, filename: 'hyperpdf.pdf', type: 'application/pdf')
|
34
|
+
|
35
|
+
|
36
|
+
Or upload it to your AWS S3 bucket
|
37
|
+
|
38
|
+
pdf.upload_to_s3('YOUR_BUCKET_NAME', 'filename.pdf', true)
|
39
|
+
|
40
|
+
NOTE! Before using 'upload_to_s3' method you need to give permission HyperPDF upload files to your bucket.
|
41
|
+
|
42
|
+
* Log into your AWS Console
|
43
|
+
* Choose S3 service
|
44
|
+
* Right click on the bucket you wish to give HyperPDF permission to write to
|
45
|
+
* From the context menu, click "Properties"
|
46
|
+
* On properties panel select "Permissions" and click on "Add bucket policy" button
|
47
|
+
* Paste the following policy into the dialog that appears (Replace YOUR_BUCKET_NAME with the name of the bucket you are adding this permission to):
|
48
|
+
|
49
|
+
{
|
50
|
+
"Version": "2008-10-17",
|
51
|
+
"Statement": [
|
52
|
+
{
|
53
|
+
"Sid": "AddCannedAcl",
|
54
|
+
"Effect": "Allow",
|
55
|
+
"Principal": {
|
56
|
+
"AWS": "arn:aws:iam::888261251835:root"
|
57
|
+
},
|
58
|
+
"Action": [
|
59
|
+
"s3:PutObjectAcl",
|
60
|
+
"s3:PutObject"
|
61
|
+
],
|
62
|
+
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
|
63
|
+
}
|
64
|
+
]
|
65
|
+
}
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hyperpdf.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hyperpdf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hyperpdf"
|
8
|
+
gem.version = HyperPDF::VERSION
|
9
|
+
gem.authors = ["redfield"]
|
10
|
+
gem.email = ["up.redfield@gmail.com"]
|
11
|
+
gem.description = %q{Ruby wrapper around the HyperPDF API}
|
12
|
+
gem.summary = %q{Ruby wrapper around the HyperPDF API}
|
13
|
+
gem.homepage = "http://www.hyper-pdf.com"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class HyperPDF
|
2
|
+
|
3
|
+
class ContentRequired < ArgumentError; end
|
4
|
+
|
5
|
+
class AuthorizationRequired < StandardError; end
|
6
|
+
|
7
|
+
class PaymentRequired < StandardError; end
|
8
|
+
|
9
|
+
class NoSuchBucket < StandardError; end
|
10
|
+
|
11
|
+
class InternalServerError < StandardError; end
|
12
|
+
|
13
|
+
end
|
data/lib/hyperpdf.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require "json"
|
3
|
+
require "hyperpdf/exceptions"
|
4
|
+
|
5
|
+
class HyperPDF
|
6
|
+
|
7
|
+
# Initializes new HyperPDF object
|
8
|
+
#
|
9
|
+
# @param content [String] HTML document or URL
|
10
|
+
# @param options [Hash] Authorization and PDF options
|
11
|
+
# @option options [String] :user If provided, sets user name (by default uses ENV['HYPERPDF_USER'] variable)
|
12
|
+
# @option options [String] :password If provided, sets user password (by default uses ENV['HYPERPDF_PASSWORD'] variable)
|
13
|
+
#
|
14
|
+
# Full list of PDF options see on {http://docs.heroku.com HyperPDF page}
|
15
|
+
def initialize(content, options = {})
|
16
|
+
raise HyperPDF::ContentRequired if content.nil? || content.empty?
|
17
|
+
@content = content
|
18
|
+
|
19
|
+
@options = options
|
20
|
+
@user = @options.delete(:user) || ENV["HYPERPDF_USER"]
|
21
|
+
@password = @options.delete(:password) || ENV["HYPERPDF_PASSWORD"]
|
22
|
+
|
23
|
+
@req = Net::HTTP::Post.new('/pdf', {'Content-Type' => 'application/json'})
|
24
|
+
@request_body = { user: @user, password: @password, content: @content, options: @options }
|
25
|
+
end
|
26
|
+
|
27
|
+
# Generates PDF
|
28
|
+
#
|
29
|
+
# @return [String] Binary string containing the generated document
|
30
|
+
def get
|
31
|
+
make_request.body
|
32
|
+
end
|
33
|
+
|
34
|
+
# Generates PDF and uploads it to AWS S3
|
35
|
+
#
|
36
|
+
# @param bucket [String] Your S3 bucket name
|
37
|
+
# @param key [String] Name for generated file
|
38
|
+
# @param public [Boolean] Sets public read access
|
39
|
+
# @return [String] Url to generated document
|
40
|
+
def upload_to_s3(bucket, key, public = false)
|
41
|
+
@request_body.merge!(bucket: bucket, key: key, public: public)
|
42
|
+
resp = JSON.parse make_request.body
|
43
|
+
resp["url"]
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def make_request
|
49
|
+
@req.body = @request_body.to_json
|
50
|
+
session = Net::HTTP.new('api.hyper-pdf.com', 443)
|
51
|
+
session.use_ssl = true
|
52
|
+
resp = session.request(@req)
|
53
|
+
|
54
|
+
if resp.is_a? Net::HTTPOK
|
55
|
+
resp
|
56
|
+
else
|
57
|
+
case resp.code
|
58
|
+
when "400" then raise HyperPDF::ContentRequired
|
59
|
+
when "401" then raise HyperPDF::AuthorizationRequired
|
60
|
+
when "402" then raise HyperPDF::PaymentRequired
|
61
|
+
when "404" then raise HyperPDF::NoSuchBucket
|
62
|
+
when "500" then raise HyperPDF::InternalServerError
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hyperpdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- redfield
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-18 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ruby wrapper around the HyperPDF API
|
15
|
+
email:
|
16
|
+
- up.redfield@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- hyperpdf.gemspec
|
28
|
+
- lib/hyperpdf.rb
|
29
|
+
- lib/hyperpdf/exceptions.rb
|
30
|
+
- lib/hyperpdf/version.rb
|
31
|
+
- spec/hyperpdf_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
homepage: http://www.hyper-pdf.com
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.23
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: Ruby wrapper around the HyperPDF API
|
58
|
+
test_files:
|
59
|
+
- spec/hyperpdf_spec.rb
|
60
|
+
- spec/spec_helper.rb
|