hpa-ruby 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ddf9c859a6954e4cbc841a602d51ac80f5fde86b
4
+ data.tar.gz: 1caec3d42ff90a113b2121a33e6d66a881440d89
5
+ SHA512:
6
+ metadata.gz: 4303eb9d8d976f413bff2013ebed2fd5fe7912d4879618b999a87982909116e48fd1252e208aa4bf56485e04c091cd2d5c1f11e57b84d4148980dce8c2dde49d
7
+ data.tar.gz: dcc76bf2b84f769e942c85425b5328a21ac205421524652155f61717fb9b7354a754c1273b83a82a64807febdacd9bbe4dfbce9cf1899c76614eb82200f7fa8a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hpa.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Zoran Žabčić
4
+
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,208 @@
1
+ hpa-ruby
2
+ =====
3
+
4
+ This is a Ruby wrapper for [HTML PDF API](http://htmlpdfapi.com) - [Documentation](http://htmlpdfapi.com/documentation)
5
+
6
+ HTML PDF API is a service that allows you to convert HTML to PDF using standard technologies (HTML, CSS and JavaScript).
7
+
8
+
9
+ Installation
10
+ =====
11
+
12
+ ### Rails
13
+
14
+ Add this line to your application"s Gemfile:
15
+
16
+ gem "hpa-ruby", require: "hpa"
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+
23
+ ### Or install it yourself
24
+
25
+ $ gem install hpa-ruby
26
+
27
+
28
+ Usage
29
+ =====
30
+
31
+ Require the Hpa gem
32
+
33
+ require "hpa"
34
+
35
+ Then you have to set your API token:
36
+
37
+ Hpa.api_token = "<your token>"
38
+
39
+ ## Configuration
40
+
41
+ Set API base (endpoint and version)
42
+
43
+ Hpa.api_base = "htmlpdfapi.com/api/v1" # default
44
+
45
+
46
+ Assets
47
+ -------
48
+
49
+ ### Get Assets List
50
+
51
+ response = Hpa::Asset.get
52
+ assets = JSON.parse(response)
53
+
54
+ on success returns:
55
+
56
+ response # json object
57
+ response.code # 200
58
+
59
+ JSON.parse(response) # [{"id":"53a99cc76d64be7c26b5e57b", "name":"hpa_logo.png", "mime":"image/png", "size":650}, {...}, {...}]
60
+
61
+
62
+ ### Create Asset
63
+
64
+ response = Hpa::Asset.create(:file => File.open("path/to/file.png"))
65
+
66
+ on success returns:
67
+
68
+ response # json object
69
+ response.code # 201
70
+
71
+ JSON.parse(response) # {"id":"53a99cc76d64be7c26b5e57b", "name":"hpa_logo.png", "mime":"image/png", "size":650}
72
+
73
+
74
+ ### Find Asset
75
+
76
+ response = Hpa::Asset.find(id)
77
+
78
+ on success returns:
79
+
80
+ response # file
81
+ response.code # 200
82
+
83
+
84
+ ### Delete Asset
85
+
86
+ response = Hpa::Asset.delete(id)
87
+
88
+ on success returns:
89
+
90
+ response # ""
91
+ response.code # 204
92
+
93
+
94
+ Credits
95
+ -------
96
+
97
+ response = Hpa::Credit.get
98
+
99
+ on success returns:
100
+
101
+ response # current number of credits
102
+ response.code # 200
103
+
104
+
105
+ PDFs
106
+ -------
107
+
108
+ ### Generate PDF
109
+
110
+ From url:
111
+
112
+ response = Hpa::Pdf.create(:url => "http://htmlpdfapi.com/examples/example.html")
113
+
114
+ From HTML string:
115
+
116
+ response = Hpa::Pdf.create(:html => "<h1>Generate PDF from HTML string</h1>")
117
+
118
+ From HTML file:
119
+
120
+ response = Hpa::Pdf.create(:file => File.open("index.html"))
121
+
122
+ From compressed file:
123
+
124
+ response = Hpa::Pdf.create(:file => File.open("sample.zip"))
125
+
126
+ on success returns:
127
+
128
+ response # file (PDF)
129
+ response.code # 200
130
+
131
+ save it to a file
132
+
133
+ File.open("result.pdf", "w+") do |f|
134
+ f.write response
135
+ end
136
+
137
+
138
+ Notice:
139
+
140
+ response == response.body
141
+
142
+
143
+ ### Passing additional options
144
+
145
+ Hpa::Pdf.create(
146
+ :file => File.open("index.html"),
147
+ :dpi => 150,
148
+ :margin_top => 20mm
149
+ )
150
+
151
+ [Full options list](https://htmlpdfapi.com/documentation#pdf)
152
+
153
+ Rails example
154
+ =====
155
+
156
+ Set your api token inside config/application.rb
157
+
158
+ module HpaExample
159
+ class Application < Rails::Application
160
+
161
+ Hpa.api_token = "<your token>"
162
+
163
+ end
164
+ end
165
+
166
+
167
+
168
+ controllers/example_controller.rb
169
+
170
+ class ExampleController < ApplicationController
171
+
172
+ def show
173
+ respond_to do |format|
174
+ format.html { render(layout: "pdf") }
175
+
176
+ format.pdf do
177
+ pdf = Hpa::Pdf.create(
178
+ html: render_to_string(layout: "pdf").to_str,
179
+ header: render_to_string(template: "example/header.erb", layout: "pdf").to_str,
180
+ footer: render_to_string(template: "example/footer.erb", layout: "pdf").to_str,
181
+ header_spacing: 10,
182
+ footer_spacing: 10,
183
+ engine_version: 12
184
+ )
185
+
186
+ send_data(pdf, filename: "hpa_rails_example.pdf", type: "application/pdf", disposition: "inline")
187
+ end
188
+
189
+ end
190
+ end
191
+
192
+ end
193
+
194
+
195
+ [Sample Rails application](https://github.com/htmlpdfapi/hpa_rails_example)
196
+
197
+
198
+ Testing
199
+ -------
200
+ rake test api_token="<your token>"
201
+
202
+ ## Contributing
203
+
204
+ 1. Fork it ( https://github.com/htmlpdfapi/hpa-ruby/fork )
205
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
206
+ 3. Commit your changes (`git commit -am "Add some feature"`)
207
+ 4. Push to the branch (`git push origin my-new-feature`)
208
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
data/hpa.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hpa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hpa-ruby"
8
+ spec.version = Hpa::VERSION
9
+ spec.authors = ["Zoran Žabčić"]
10
+ spec.email = ["zoran@effectiva.hr"]
11
+ spec.summary = %q{HTML PDF API wrapper}
12
+ spec.description = %q{HTML PDF API is a service that allows you to convert HTML to PDF using standard technologies (HTML, CSS and JavaScript).}
13
+ spec.homepage = "https://github.com/htmlpdfapi/hpa-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rest-client"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.5"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ end
data/lib/hpa/asset.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Hpa
2
+ class Asset
3
+
4
+ def self.get
5
+ Hpa.get(:assets)
6
+ end
7
+
8
+ def self.find(id)
9
+ Hpa.find(:assets, id)
10
+ end
11
+
12
+ def self.create(options={})
13
+ Hpa.create(:assets, options)
14
+ end
15
+
16
+ def self.delete(id)
17
+ Hpa.delete(:assets, id)
18
+ end
19
+
20
+ end
21
+ end
data/lib/hpa/credit.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Hpa
2
+ class Credit
3
+
4
+ def self.get
5
+ Hpa.get(:credits)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Hpa
2
+ class ApiError < HpaError; end
3
+ end
@@ -0,0 +1,16 @@
1
+ module Hpa
2
+ class HpaError < StandardError
3
+
4
+ attr_reader :status, :message
5
+
6
+ def initialize(status=nil, message=nil)
7
+ @status = status
8
+ @message = message
9
+ end
10
+
11
+ def to_s
12
+ [status, message].compact.join(" ")
13
+ end
14
+
15
+ end
16
+ end
data/lib/hpa/pdf.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Hpa
2
+ class Pdf
3
+
4
+ def self.create(options={})
5
+ Hpa.create(:pdf, options)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Hpa
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ end
data/lib/hpa.rb ADDED
@@ -0,0 +1,68 @@
1
+ require "rest-client"
2
+
3
+ # Version
4
+ require "hpa/version"
5
+
6
+ # Resources
7
+ require "hpa/asset"
8
+ require "hpa/credit"
9
+ require "hpa/pdf"
10
+
11
+ # Errors
12
+ require "hpa/errors/hpa_error"
13
+ require "hpa/errors/api_error"
14
+
15
+ module Hpa
16
+
17
+ @@api_token = nil
18
+ @@api_base = "htmlpdfapi.com/api/v1"
19
+
20
+ def self.api_token
21
+ @@api_token
22
+ end
23
+
24
+ def self.api_token=(api_token)
25
+ @@api_token = api_token
26
+ end
27
+
28
+ def self.api_base
29
+ @@api_base
30
+ end
31
+
32
+ def self.api_base=(api_base)
33
+ @@api_base = api_base
34
+ end
35
+
36
+ def self.get(resource)
37
+ handle_api_error { RestClient.get(endpoint(resource), authentication) }
38
+ end
39
+
40
+ def self.find(resource, id)
41
+ handle_api_error { RestClient.get(endpoint(resource, id), authentication) }
42
+ end
43
+
44
+ def self.create(resource, options={})
45
+ handle_api_error { RestClient.post(endpoint(resource), options, authentication) }
46
+ end
47
+
48
+ def self.delete(resource, id)
49
+ handle_api_error { RestClient.delete(endpoint(resource, id), authentication) }
50
+ end
51
+
52
+ private
53
+
54
+ def self.authentication
55
+ {:authentication => ["Token", api_token].join(" ")}
56
+ end
57
+
58
+ def self.endpoint(resource, id=nil)
59
+ [api_base, resource, id].compact.join("/")
60
+ end
61
+
62
+ def self.handle_api_error
63
+ yield
64
+ rescue RestClient::Exception => e
65
+ raise ApiError.new(e.http_code, e.http_body)
66
+ end
67
+
68
+ end
Binary file
Binary file
data/test/hpa_test.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'test_helper'
2
+
3
+ class HpaTest < MiniTest::Unit::TestCase
4
+
5
+
6
+ def setup
7
+ # add 2 sec delay between tests
8
+ sleep 2
9
+ end
10
+
11
+
12
+ def test_get_assets
13
+ response = Hpa::Asset.get
14
+ assert_equal 200, response.code
15
+ end
16
+
17
+
18
+ def test_create_find_and_delete_asset
19
+ # first, check if asset named "hpa-4ff6375e12f1.png"
20
+ # already exists and delete it
21
+ assets = JSON.parse(Hpa::Asset.get.body)
22
+ asset = assets.detect { |a| a["name"] == "hpa-4ff6375e12f1.png"}
23
+ Hpa::Asset.delete(asset["id"]) if asset
24
+
25
+ # create asset
26
+ sleep 1
27
+ image = File.open(fixtures_path("hpa-4ff6375e12f1.png"))
28
+ response = Hpa::Asset.create(:file => image)
29
+ assert_equal 201, response.code
30
+
31
+ asset = JSON.parse(response.body)
32
+
33
+ # find asset
34
+ sleep 1
35
+ response = Hpa::Asset.find(asset["id"])
36
+ assert_equal 200, response.code
37
+
38
+ # delete asset
39
+ sleep 1
40
+ response = Hpa::Asset.delete(asset["id"])
41
+ assert_equal 204, response.code
42
+ assert_empty response.body
43
+ end
44
+
45
+
46
+ def test_get_credits
47
+ response = Hpa::Credit.get
48
+
49
+ assert_equal 200, response.code
50
+ assert_match /\d+/, response.body
51
+ end
52
+
53
+
54
+ def test_create_pdf_from_string
55
+ response = Hpa::Pdf.create(:html => "<h1>Title</h1>")
56
+
57
+ assert_equal 200, response.code
58
+ assert_equal "%PDF", response.body[0, 4]
59
+ end
60
+
61
+
62
+ def test_create_pdf_from_url
63
+ response = Hpa::Pdf.create(:url => "http://htmlpdfapi.com/examples/example.html")
64
+
65
+ assert_equal 200, response.code
66
+ assert_equal "%PDF", response.body[0, 4]
67
+ end
68
+
69
+
70
+ def test_create_pdf_from_file
71
+ file = File.open(fixtures_path("compressed_file.zip"))
72
+ response = Hpa::Pdf.create(:file => file)
73
+
74
+ assert_equal 200, response.code
75
+ assert_equal "%PDF", response.body[0, 4]
76
+ end
77
+
78
+
79
+ end
@@ -0,0 +1,16 @@
1
+ require 'hpa'
2
+ require 'minitest/unit'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+
6
+
7
+ class MiniTest::Unit::TestCase
8
+
9
+ Hpa.api_token = ENV['api_token']
10
+ Hpa.api_base = ENV['api_base'] if ENV['api_base']
11
+
12
+ def fixtures_path(filename)
13
+ File.join(File.dirname(__FILE__), "fixtures", filename)
14
+ end
15
+
16
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hpa-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zoran Žabčić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: HTML PDF API is a service that allows you to convert HTML to PDF using
70
+ standard technologies (HTML, CSS and JavaScript).
71
+ email:
72
+ - zoran@effectiva.hr
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - hpa.gemspec
83
+ - lib/hpa.rb
84
+ - lib/hpa/asset.rb
85
+ - lib/hpa/credit.rb
86
+ - lib/hpa/errors/api_error.rb
87
+ - lib/hpa/errors/hpa_error.rb
88
+ - lib/hpa/pdf.rb
89
+ - lib/hpa/version.rb
90
+ - test/fixtures/compressed_file.zip
91
+ - test/fixtures/hpa-4ff6375e12f1.png
92
+ - test/hpa_test.rb
93
+ - test/test_helper.rb
94
+ homepage: https://github.com/htmlpdfapi/hpa-ruby
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: HTML PDF API wrapper
118
+ test_files:
119
+ - test/fixtures/compressed_file.zip
120
+ - test/fixtures/hpa-4ff6375e12f1.png
121
+ - test/hpa_test.rb
122
+ - test/test_helper.rb