api2pdf 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +15 -6
- data/api2pdf.gemspec +3 -3
- data/lib/api2pdf.rb +40 -43
- data/lib/api2pdf/version.rb +1 -1
- metadata +5 -5
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,19 @@
|
|
1
|
-
#
|
1
|
+
# API2PDF
|
2
2
|
|
3
|
-
|
3
|
+
Pretty-prints JSON-based API to PDF.
|
4
|
+
|
5
|
+
![DEMO](http://cl.ly/image/3z3D3R2g1o3T/api2pdf.jpg)
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
API2PDF.export(
|
10
|
+
:url => "http://ohanapi.herokuapp.com/api/organizations/51a9fd0328217f89770001b2",
|
11
|
+
:file_name => "HSC",
|
12
|
+
:heading => "Human Services Providersss"
|
13
|
+
:columns => 2,
|
14
|
+
:page_layout => :landscape,
|
15
|
+
:page_size => "B5",
|
16
|
+
)
|
4
17
|
|
5
18
|
## Installation
|
6
19
|
|
@@ -16,10 +29,6 @@ Or install it yourself as:
|
|
16
29
|
|
17
30
|
$ gem install api2pdf
|
18
31
|
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
32
|
## Contributing
|
24
33
|
|
25
34
|
1. Fork it
|
data/api2pdf.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Api2pdf::VERSION
|
9
9
|
spec.authors = ["Tu Hoang"]
|
10
10
|
spec.email = ["rebyn@me.com"]
|
11
|
-
spec.description = "
|
12
|
-
spec.summary = "
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.description = "Pretty-prints JSON-based API to PDF."
|
12
|
+
spec.summary = "Pretty-prints JSON-based API to PDF."
|
13
|
+
spec.homepage = "https://github.com/rebyn/api2pdf"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/api2pdf.rb
CHANGED
@@ -1,63 +1,60 @@
|
|
1
1
|
require "api2pdf/version"
|
2
|
-
|
3
|
-
# ---------------------------------------------------------------------------------
|
4
|
-
# A TAILORED VERSION TO OHANA API
|
5
|
-
# ---------------------------------------------------------------------------------
|
6
|
-
# Call:
|
7
|
-
# API2PDF.export(
|
8
|
-
# url: "http://ohanapi.herokuapp.com/api/organizations/51a9fd0628217f89770003ab",
|
9
|
-
# h1: "Code for America: Human Services Finder",
|
10
|
-
# h2: "name"
|
11
|
-
# )
|
12
|
-
# ---------------------------------------------------------------------------------
|
13
|
-
|
14
2
|
require 'httparty'
|
15
3
|
require 'active_support'
|
16
4
|
require 'prawn'
|
17
5
|
|
18
6
|
class API2PDF
|
19
7
|
class << self
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
obj.each_with_index
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
8
|
+
|
9
|
+
def titlize(pdf, text)
|
10
|
+
pdf.text "<u>#{ActiveSupport::Inflector.humanize(text).upcase}</u>:", style: :bold, size: 8, :inline_format => true
|
11
|
+
end
|
12
|
+
|
13
|
+
def pdf_body_print(pdf, obj)
|
14
|
+
# FIELD VALUE CAN BE A STRING, HASH, ARRAY OF STRING, HASH, ETC
|
15
|
+
case obj.class.to_s
|
16
|
+
when "Array"
|
17
|
+
obj.each_with_index { |ele, index| pdf_body_print(pdf, ele) }
|
18
|
+
when "Hash"
|
19
|
+
obj.each do |key, value|
|
20
|
+
case value.class.to_s
|
21
|
+
when "Array"
|
22
|
+
pdf.pad_bottom(5) { pdf.pad_bottom(3) {titlize(pdf, key)}; pdf.indent(20) {pdf_body_print(pdf, value)}; }
|
23
|
+
when "Hash"
|
24
|
+
titlize(pdf, key)
|
25
|
+
pdf.indent(20) { value.each { |key, value| pdf.pad_bottom(5) {titlize(pdf, key); pdf_body_print(pdf, value);} } }
|
26
|
+
when "String"
|
27
|
+
pdf.pad_bottom(5) { titlize(pdf, key); pdf.indent(20) {pdf.text "#{value}", size: 10, align: :justify}; }
|
35
28
|
end
|
36
|
-
output << ", " if (index != obj.length-1)
|
37
29
|
end
|
38
30
|
else
|
39
|
-
|
31
|
+
pdf.text "#{obj.to_s}", size: 10, align: :justify
|
40
32
|
end
|
41
|
-
return output
|
42
33
|
end
|
43
34
|
|
44
35
|
def export(arg_hash)
|
36
|
+
default_param = {
|
37
|
+
:heading => nil,
|
38
|
+
:file_name => "#{Time.now}",
|
39
|
+
:page_layout => :portrait,
|
40
|
+
:page_size => "LETTER",
|
41
|
+
:columns => 1
|
42
|
+
}
|
43
|
+
arg_hash = default_param.merge(arg_hash)
|
44
|
+
@safe_file_name = "#{arg_hash[:file_name].gsub(/[^0-9A-Za-z.\-]/, '_').gsub('.','')}.pdf"
|
45
|
+
|
45
46
|
@fetch = HTTParty.get("#{arg_hash[:url]}")
|
46
|
-
|
47
|
-
@safe_file_name = @fetch["response"]["name"].gsub(/[^0-9A-Za-z.\-]/, '_').gsub('.','') || @fetch["response"]["_id"]
|
48
|
-
Prawn::Document.generate("#{@safe_file_name}.pdf", page_layout: :landscape, ) do |pdf|
|
47
|
+
Prawn::Document.generate(@safe_file_name, arg_hash) do |pdf|
|
49
48
|
# PDF headings
|
50
|
-
|
51
|
-
pdf.text "#{arg_hash[:
|
52
|
-
|
53
|
-
}
|
49
|
+
if (!arg_hash[:heading].nil?)
|
50
|
+
pdf.pad_bottom(20) { pdf.text "#{arg_hash[:heading]}", size: 20, align: :center, style: :bold }
|
51
|
+
end
|
54
52
|
# PDF body
|
55
|
-
pdf.column_box([0, pdf.cursor], :columns =>
|
56
|
-
@fetch
|
57
|
-
if (!value.nil?
|
58
|
-
pdf.pad_bottom(5) { pdf
|
59
|
-
|
60
|
-
pdf.pad_bottom(15) { pdf.text pdf_body_print(value), size: 10, align: :justify }
|
53
|
+
pdf.column_box([0, pdf.cursor], :columns => arg_hash[:columns], :width => pdf.bounds.width) do
|
54
|
+
@fetch.each do |key, value|
|
55
|
+
if (!value.nil?)
|
56
|
+
pdf.pad_bottom(5) { titlize(pdf, key) }
|
57
|
+
pdf.indent(20) { pdf_body_print(pdf, value) }
|
61
58
|
end
|
62
59
|
end
|
63
60
|
end
|
data/lib/api2pdf/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api2pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -91,7 +91,7 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
-
description:
|
94
|
+
description: Pretty-prints JSON-based API to PDF.
|
95
95
|
email:
|
96
96
|
- rebyn@me.com
|
97
97
|
executables: []
|
@@ -106,7 +106,7 @@ files:
|
|
106
106
|
- api2pdf.gemspec
|
107
107
|
- lib/api2pdf.rb
|
108
108
|
- lib/api2pdf/version.rb
|
109
|
-
homepage:
|
109
|
+
homepage: https://github.com/rebyn/api2pdf
|
110
110
|
licenses:
|
111
111
|
- MIT
|
112
112
|
post_install_message:
|
@@ -130,5 +130,5 @@ rubyforge_project:
|
|
130
130
|
rubygems_version: 1.8.25
|
131
131
|
signing_key:
|
132
132
|
specification_version: 3
|
133
|
-
summary:
|
133
|
+
summary: Pretty-prints JSON-based API to PDF.
|
134
134
|
test_files: []
|