api2pdf 0.0.1 → 0.0.2

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.
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ .DS_Store
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
data/README.md CHANGED
@@ -1,6 +1,19 @@
1
- # Api2pdf
1
+ # API2PDF
2
2
 
3
- TODO: Write a gem description
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
@@ -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 = "Save API JSON as PDF"
12
- spec.summary = "Save API JSON as PDF"
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($/)
@@ -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
- def pdf_body_print(obj)
21
- output = ""
22
- # Field value can be a string, hash, array of string, hash, etc
23
- if obj.class == Array
24
- obj.each_with_index do |ele, index|
25
- output << pdf_body_print(ele)
26
- output << ", " if (index != obj.length-1)
27
- end
28
- elsif obj.class == Hash
29
- obj.each_with_index do |(key, value), index|
30
- if value.class == Array
31
- output << "#{ActiveSupport::Inflector.humanize(key)}: "
32
- output << pdf_body_print(value)
33
- else
34
- output << "#{ActiveSupport::Inflector.humanize(key)}: #{value}"
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
- output << obj.to_s
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
- # Strip out non-ascii characters and stops
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
- pdf.pad_bottom(20) {
51
- pdf.text "#{arg_hash[:h1]}", size: 20, align: :center, style: :bold
52
- pdf.text "#{@fetch["response"]["#{arg_hash[:h2]}"] || @fetch["response"]["_id"]}", size: 14, align: :center, style: :bold
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 => 2, :width => pdf.bounds.width) do
56
- @fetch["response"].each do |key, value|
57
- if (!value.nil? && key != "_id")
58
- pdf.pad_bottom(5) { pdf.text "#{ActiveSupport::Inflector.humanize(key).upcase}:",
59
- style: :bold, size: 9 }
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
@@ -1,3 +1,3 @@
1
1
  module Api2pdf
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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-20 00:00:00.000000000 Z
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: Save API JSON as PDF
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: Save API JSON as PDF
133
+ summary: Pretty-prints JSON-based API to PDF.
134
134
  test_files: []