caterpillar-ruby 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/caterpillar_ruby.gemspec +33 -0
- data/lib/caterpillar-ruby.rb +52 -0
- data/lib/caterpillar-ruby/error.rb +5 -0
- data/lib/caterpillar-ruby/version.rb +3 -0
- data/lib/core_ext/blank.rb +140 -0
- data/lib/core_ext/hash.rb +23 -0
- data/lib/core_ext/inflectors.rb +13 -0
- data/lib/core_ext/string.rb +7 -0
- data/lib/generators/caterpillar/initializer.rb +5 -0
- data/lib/generators/caterpillar/install_generator.rb +9 -0
- metadata +106 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: e13990d758ec7cbc03b733047c4c6b0e978538a9
|
|
4
|
+
data.tar.gz: d33f0e4ba4a3af69db7e0a51901bf9a8c8ab0355
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8184a6b5c113608bf45873196189205508b529c37800f1139d86988c10b94b3cbefce0b4d7272a180dd6a6dbd10bc1c78c3b568e7fb77a96b881bbc85f228b12
|
|
7
|
+
data.tar.gz: 5167c96f7c3e0aee48d9dd7d19ec5fef43322bb1bb7551b8fd56c160574f403d523c80456c1a33a7cfbeddc9295d16885bcea98b2496fb29766dd674698466cb
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Nitish Sharma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
# Caterpillar-Ruby
|
|
2
|
+
|
|
3
|
+
This Ruby gem is a simple wrapper around the Caterpillar API. [Caterpillar](https://caterpillar.io) is a web service that allows you to convert html to pdf.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'caterpillar-ruby'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install caterpillar-ruby
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Set the following attributes in you Ruby app:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
Caterpillar.api_key = 'YOUR_CATERPILLAR_API_KEY'
|
|
27
|
+
Caterpillar.api_version = 'v1'
|
|
28
|
+
Caterpillar.base_uri = 'https://api.caterpillar.io'
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
For a Rails app you can execute:
|
|
32
|
+
|
|
33
|
+
$ rails generate caterpillar:install
|
|
34
|
+
|
|
35
|
+
or create an initializer `config/initializers/caterpillar.rb`
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
Caterpillar.configure do |config|
|
|
39
|
+
config.api_key = 'YOUR_CATERPILLAR_API_KEY'
|
|
40
|
+
config.api_version = 'v1'
|
|
41
|
+
config.base_uri = 'https://api.caterpillar.io'
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Once set, you can create a PDF document by calling:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
Caterpillar.create(source: content, grayscale: true, no_images: true)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
You can also create test PDF documents by calling (header and footer options donot work in test mode):
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
Caterpillar.create(source: content, test: true)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `create` call can also take a block, like so:
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
Caterpillar.create(source: content) do |file, response|
|
|
61
|
+
#file is a tempfile holding the response body
|
|
62
|
+
#reponse is the HTTParty response object
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`create` will return an [HTTParty](https://github.com/jnunemaker/httparty) response object, which will be the new file (or errors, if the request was not valid).
|
|
67
|
+
|
|
68
|
+
The only required parameter is:
|
|
69
|
+
* `:source` - a string containing the HTML or URL for creating the document
|
|
70
|
+
|
|
71
|
+
You might want to set other options in that hash:
|
|
72
|
+
|
|
73
|
+
| Option | Type | Description |
|
|
74
|
+
|:---|:---|:---|
|
|
75
|
+
`:copies` | `Number` | Number of copies to print into the pdf
|
|
76
|
+
`:grayscale` | `Boolean` | PDF will be generated in grayscale
|
|
77
|
+
`:margin_bottom` | `Number` | Set the page bottom margin
|
|
78
|
+
`:margin_left` | `Number` | Set the page left margin (default 10mm)
|
|
79
|
+
`:margin_right` | `Number` | Set the page right margin (default 10mm)
|
|
80
|
+
`:margin_top` | `Number` | Set the page top margin
|
|
81
|
+
`:orientation` | `String` | Set orientation to Landscape or Portrait (default Portrait)
|
|
82
|
+
`:page_size` | `String` | Set paper size to: A4, Letter, etc. (default A4)
|
|
83
|
+
`:background` | `Boolean` | Do print background (default true)
|
|
84
|
+
`:no_background` | `Boolean` | Do not print background
|
|
85
|
+
`:disable_external_links` | `Boolean` | Do not make links to remote web pages
|
|
86
|
+
`:enable_external_links` | `Boolean` | Make links to remote web pages (default true)
|
|
87
|
+
`:disable_forms` | `Boolean` | Do not turn HTML form fields into pdf form fields (default true)
|
|
88
|
+
`:enable_forms` | `Boolean` | Turn HTML form fields into pdf form fields
|
|
89
|
+
`:images` | `Boolean` | Do load or print images (default true)
|
|
90
|
+
`:no_images` | `Boolean` | Do not load or print images
|
|
91
|
+
`:disable_internal_links` | `Boolean` | Do not make local links
|
|
92
|
+
`:enable_internal_links` | `Boolean` | Make local links (default true)
|
|
93
|
+
`:disable_javascript` | `Boolean` | Do not allow web pages to run javascript
|
|
94
|
+
`:enable_javascript` | `Boolean` | Do allow web pages to run javascript (default true)
|
|
95
|
+
`:javascript_delay` | `Number` | Wait some milliseconds for javascript finish (default 200)
|
|
96
|
+
`:viewport_size` | `String` | Set viewport size if you have custom scrollbars or css attribute overflow to emulate window size
|
|
97
|
+
`:zoom` | `Number` | Use this zoom factor (default 1)
|
|
98
|
+
`:footer_center` | `Number`/`String` | Centered footer text
|
|
99
|
+
`:footer_font_name` | `Number`/`String` | Set footer font name (default Arial)
|
|
100
|
+
`:footer_font_size` | `Number`/`String` | Set footer font size (default 12)
|
|
101
|
+
`:footer_html` | `String` | Adds a html footer
|
|
102
|
+
`:footer_left` | `Number`/`String` | Left aligned footer text
|
|
103
|
+
`:footer_line` | `Boolean` | Display line above the footer
|
|
104
|
+
`:no_footer_line` | `Boolean` | Do not display line above the footer (default true)
|
|
105
|
+
`:footer_right` | `Number`/`String` | Right aligned footer text
|
|
106
|
+
`:footer_spacing` | `Number` | Spacing between footer and content in mm (default 0)
|
|
107
|
+
`:header_center` | `Number`/`String` | Centered header text
|
|
108
|
+
`:header_font_name` | `Number`/`String` | Set header font name (default Arial)
|
|
109
|
+
`:header_font_size` | `Number`/`String` | Set header font size (default 12)
|
|
110
|
+
`:header_html` | `String` | Adds a html header
|
|
111
|
+
`:header_left` | `Number`/`String` | Left aligned header text
|
|
112
|
+
`:header_line` | `Boolean` | Display line below the header
|
|
113
|
+
`:no_header_line` | `Boolean` | Do not display line below the header (default true)
|
|
114
|
+
`:header_right` | `Number`/`String` | Right aligned header text
|
|
115
|
+
`:header_spacing` | `Number` | Spacing between header and content in mm (default 0)
|
|
116
|
+
`:password_protect` | `Hash` | Refer next section for more details
|
|
117
|
+
|
|
118
|
+
## Password Protect
|
|
119
|
+
You can passowrd protect PDF using AES 256, AES 128, RC4 (128 & 40) encryption algorithms supported by Adobe Reader.
|
|
120
|
+
|
|
121
|
+
To encrypt PDF you need to pass `:password_protect` hash in the `create` method call along with the following **required parameters**:
|
|
122
|
+
* `:password` - a string containing the secret password which will be further used to unlock the PDF
|
|
123
|
+
* `:key_length` - a number which defines the encryption alogirthm to be used. Values can be **40, 128 and 256** only.
|
|
124
|
+
|
|
125
|
+
You might want to set other options for each encryption alogrithm inside `:restrections` hash:
|
|
126
|
+
|
|
127
|
+
*Key Length:* **40**
|
|
128
|
+
|
|
129
|
+
| Option | Value | Description |
|
|
130
|
+
|:---|:---|:---|
|
|
131
|
+
`:print` | `'y', 'n'` | Determines whether or not to allow printing.
|
|
132
|
+
`:modify` | `'y', 'n'` | Determines whether or not to allow document modification.
|
|
133
|
+
`:extract` | `'y', 'n'` | Determines whether or not to allow text/image extraction.
|
|
134
|
+
`:annotate` | `'y', 'n'` | Determines whether or not to allow comments and form fill-in and signing.
|
|
135
|
+
|
|
136
|
+
*Key Length:* **128**
|
|
137
|
+
|
|
138
|
+
| Option | Value | Description |
|
|
139
|
+
|:---|:---|:---|
|
|
140
|
+
`:print` | `'full', 'low', 'none'` | **full**: allow full printing. **low**: allow low-resolution printing only. **none**: disallow printing.
|
|
141
|
+
`:modify` | `'all', 'annotate', 'form', 'assembly', 'none'` | **all:** allow full document modification. **annotate:** allow comment authoring and form operations. **form:** allow form field fill-in and signing. **assembly:** allow document assembly only. **none:** allow no modifications.
|
|
142
|
+
`:extract` | `'y', 'n'` | Determines whether or not to allow text/image extraction.
|
|
143
|
+
`:use_aes` | `'y', 'n'` | AES encryption will be used instead of RC4 encryption.
|
|
144
|
+
`:accessibility` | `'y', 'n'` | Determines whether or not to allow accessibility to visually impaired.
|
|
145
|
+
|
|
146
|
+
*Key Length:* **256**
|
|
147
|
+
|
|
148
|
+
| Option | Value | Description |
|
|
149
|
+
|:---|:---|:---|
|
|
150
|
+
`:print` | `'full', 'low', 'none'` | **full**: allow full printing. **low**: allow low-resolution printing only. **none**: disallow printing.
|
|
151
|
+
`:modify` | `'all', 'annotate', 'form', 'assembly', 'none'` | **all:** allow full document modification. **annotate:** allow comment authoring and form operations. **form:** allow form field fill-in and signing. **assembly:** allow document assembly only. **none:** allow no modifications.
|
|
152
|
+
`:extract` | `'y', 'n'` | Determines whether or not to allow text/image extraction.
|
|
153
|
+
`:accessibility` | `'y', 'n'` | Determines whether or not to allow accessibility to visually impaired.
|
|
154
|
+
|
|
155
|
+
**Example:**
|
|
156
|
+
```
|
|
157
|
+
Caterpillar.create(
|
|
158
|
+
source: content,
|
|
159
|
+
password_protect: {
|
|
160
|
+
pasword: 'YOUR_PASSWORD_HERE',
|
|
161
|
+
key_length: 128,
|
|
162
|
+
restrictions: {
|
|
163
|
+
print: 'low',
|
|
164
|
+
use_aes: 'y'
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Meta
|
|
171
|
+
|
|
172
|
+
Maintained by [KeepWorks](http://www.keepworks.com/)
|
|
173
|
+
|
|
174
|
+
## Contributing
|
|
175
|
+
|
|
176
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/keepworks/caterpillar.
|
|
177
|
+
|
|
178
|
+
## License
|
|
179
|
+
|
|
180
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "caterpillar"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'caterpillar-ruby/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "caterpillar-ruby"
|
|
8
|
+
spec.version = Caterpillar::VERSION
|
|
9
|
+
spec.authors = ["Nitish Sharma", "Nishit Hirani"]
|
|
10
|
+
spec.email = ["nitish.sharma@keepworks.com", "nishit@keepworks.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{A simple ruby client for the Caterpillar API}
|
|
13
|
+
spec.description = %q{Easily convert HTML to PDF in your Ruby app using Caterpillar API.}
|
|
14
|
+
spec.homepage = "https://caterpillar.io"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
|
19
|
+
# if spec.respond_to?(:metadata)
|
|
20
|
+
# spec.metadata['allowed_push_host'] = "https://rubygems.org/"
|
|
21
|
+
# else
|
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
32
|
+
spec.add_development_dependency "httparty"
|
|
33
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
|
|
3
|
+
['caterpillar-ruby', 'core_ext'].each do |dir|
|
|
4
|
+
Dir[File.dirname(__FILE__) + '/' + dir + '/*.rb'].each { |file| require file }
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module Caterpillar
|
|
8
|
+
class << self
|
|
9
|
+
attr_accessor :api_key
|
|
10
|
+
attr_accessor :api_version
|
|
11
|
+
attr_accessor :base_uri
|
|
12
|
+
|
|
13
|
+
def configure
|
|
14
|
+
yield self
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def create(options = {})
|
|
18
|
+
raise NoApiKeyError.new('API Key is blank') if api_key.blank?
|
|
19
|
+
raise NoSourceError.new('Source is blank') if options[:source].blank?
|
|
20
|
+
|
|
21
|
+
options.deep_modify_keys!(:camelize)
|
|
22
|
+
|
|
23
|
+
query = {
|
|
24
|
+
source: options.delete(:source),
|
|
25
|
+
data: {
|
|
26
|
+
apiKey: api_key
|
|
27
|
+
}.merge!(options)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
response = HTTParty.post("#{base_uri}/#{api_version}/documents/convert", query: query).force_encoding('UTF-8')
|
|
31
|
+
|
|
32
|
+
parsed_response = response.is_caterpillar_json?
|
|
33
|
+
raise APIError.new(parsed_response['message']) if parsed_response.present? && parsed_response['status'] == 0
|
|
34
|
+
|
|
35
|
+
if block_given?
|
|
36
|
+
return_value = nil
|
|
37
|
+
|
|
38
|
+
Tempfile.open('caterpillar') do |f|
|
|
39
|
+
f.sync = true
|
|
40
|
+
f.write(response)
|
|
41
|
+
f.rewind
|
|
42
|
+
|
|
43
|
+
return_value = yield f, response
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
return_value
|
|
47
|
+
else
|
|
48
|
+
response
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
class Object
|
|
2
|
+
# An object is blank if it's false, empty, or a whitespace string.
|
|
3
|
+
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
|
4
|
+
#
|
|
5
|
+
# This simplifies
|
|
6
|
+
#
|
|
7
|
+
# !address || address.empty?
|
|
8
|
+
#
|
|
9
|
+
# to
|
|
10
|
+
#
|
|
11
|
+
# address.blank?
|
|
12
|
+
#
|
|
13
|
+
# @return [true, false]
|
|
14
|
+
def blank?
|
|
15
|
+
respond_to?(:empty?) ? !!empty? : !self
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# An object is present if it's not blank.
|
|
19
|
+
#
|
|
20
|
+
# @return [true, false]
|
|
21
|
+
def present?
|
|
22
|
+
!blank?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Returns the receiver if it's present otherwise returns +nil+.
|
|
26
|
+
# <tt>object.presence</tt> is equivalent to
|
|
27
|
+
#
|
|
28
|
+
# object.present? ? object : nil
|
|
29
|
+
#
|
|
30
|
+
# For example, something like
|
|
31
|
+
#
|
|
32
|
+
# state = params[:state] if params[:state].present?
|
|
33
|
+
# country = params[:country] if params[:country].present?
|
|
34
|
+
# region = state || country || 'US'
|
|
35
|
+
#
|
|
36
|
+
# becomes
|
|
37
|
+
#
|
|
38
|
+
# region = params[:state].presence || params[:country].presence || 'US'
|
|
39
|
+
#
|
|
40
|
+
# @return [Object]
|
|
41
|
+
def presence
|
|
42
|
+
self if present?
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class NilClass
|
|
47
|
+
# +nil+ is blank:
|
|
48
|
+
#
|
|
49
|
+
# nil.blank? # => true
|
|
50
|
+
#
|
|
51
|
+
# @return [true]
|
|
52
|
+
def blank?
|
|
53
|
+
true
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class FalseClass
|
|
58
|
+
# +false+ is blank:
|
|
59
|
+
#
|
|
60
|
+
# false.blank? # => true
|
|
61
|
+
#
|
|
62
|
+
# @return [true]
|
|
63
|
+
def blank?
|
|
64
|
+
true
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class TrueClass
|
|
69
|
+
# +true+ is not blank:
|
|
70
|
+
#
|
|
71
|
+
# true.blank? # => false
|
|
72
|
+
#
|
|
73
|
+
# @return [false]
|
|
74
|
+
def blank?
|
|
75
|
+
false
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
class Array
|
|
80
|
+
# An array is blank if it's empty:
|
|
81
|
+
#
|
|
82
|
+
# [].blank? # => true
|
|
83
|
+
# [1,2,3].blank? # => false
|
|
84
|
+
#
|
|
85
|
+
# @return [true, false]
|
|
86
|
+
alias_method :blank?, :empty?
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
class Hash
|
|
90
|
+
# A hash is blank if it's empty:
|
|
91
|
+
#
|
|
92
|
+
# {}.blank? # => true
|
|
93
|
+
# { key: 'value' }.blank? # => false
|
|
94
|
+
#
|
|
95
|
+
# @return [true, false]
|
|
96
|
+
alias_method :blank?, :empty?
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
class String
|
|
100
|
+
CAT_BLANK_RE = /\A[[:space:]]*\z/
|
|
101
|
+
|
|
102
|
+
# A string is blank if it's empty or contains whitespaces only:
|
|
103
|
+
#
|
|
104
|
+
# ''.blank? # => true
|
|
105
|
+
# ' '.blank? # => true
|
|
106
|
+
# "\t\n\r".blank? # => true
|
|
107
|
+
# ' blah '.blank? # => false
|
|
108
|
+
#
|
|
109
|
+
# Unicode whitespace is supported:
|
|
110
|
+
#
|
|
111
|
+
# "\u00a0".blank? # => true
|
|
112
|
+
#
|
|
113
|
+
# @return [true, false]
|
|
114
|
+
def blank?
|
|
115
|
+
CAT_BLANK_RE === self
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
class Numeric #:nodoc:
|
|
120
|
+
# No number is blank:
|
|
121
|
+
#
|
|
122
|
+
# 1.blank? # => false
|
|
123
|
+
# 0.blank? # => false
|
|
124
|
+
#
|
|
125
|
+
# @return [false]
|
|
126
|
+
def blank?
|
|
127
|
+
false
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
class Time #:nodoc:
|
|
132
|
+
# No Time is blank:
|
|
133
|
+
#
|
|
134
|
+
# Time.now.blank? # => false
|
|
135
|
+
#
|
|
136
|
+
# @return [false]
|
|
137
|
+
def blank?
|
|
138
|
+
false
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
class Hash
|
|
2
|
+
def deep_modify_keys(modification = :symbolize)
|
|
3
|
+
if modification == :camelize
|
|
4
|
+
Hash[
|
|
5
|
+
self.map do |key, value|
|
|
6
|
+
[key.to_camelcase.to_sym, value.is_a?(Hash) ? value.deep_modify_keys(:camelize) : value]
|
|
7
|
+
end
|
|
8
|
+
]
|
|
9
|
+
elsif modification == :underscorize
|
|
10
|
+
Hash[
|
|
11
|
+
self.map do |key, value|
|
|
12
|
+
[key.to_underscore.to_sym, value.is_a?(Hash) ? value.deep_modify_keys(:underscorize) : value]
|
|
13
|
+
end
|
|
14
|
+
]
|
|
15
|
+
else
|
|
16
|
+
Hash[self.map { |key, value| [key.to_sym, value.is_a?(Hash) ? value.deep_modify_keys : value] }]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def deep_modify_keys!(modification = :symbolize)
|
|
21
|
+
self.replace(deep_modify_keys(modification))
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module Inflectors
|
|
2
|
+
def to_camelcase
|
|
3
|
+
string = self.to_s.downcase
|
|
4
|
+
string.include?('_') ? string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize.to_sym } : string.to_sym
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def to_underscore
|
|
8
|
+
self.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
|
9
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').tr('-', '_').downcase
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
[String, Object].each { |klass| klass.include(Inflectors) }
|
metadata
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: caterpillar-ruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nitish Sharma
|
|
8
|
+
- Nishit Hirani
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: exe
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2016-04-07 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - "~>"
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '1.10'
|
|
21
|
+
type: :development
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "~>"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '1.10'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: rake
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - "~>"
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '10.0'
|
|
35
|
+
type: :development
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - "~>"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '10.0'
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: httparty
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
type: :development
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
description: Easily convert HTML to PDF in your Ruby app using Caterpillar API.
|
|
57
|
+
email:
|
|
58
|
+
- nitish.sharma@keepworks.com
|
|
59
|
+
- nishit@keepworks.com
|
|
60
|
+
executables: []
|
|
61
|
+
extensions: []
|
|
62
|
+
extra_rdoc_files: []
|
|
63
|
+
files:
|
|
64
|
+
- ".gitignore"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- Gemfile
|
|
67
|
+
- LICENSE.txt
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- bin/console
|
|
71
|
+
- bin/setup
|
|
72
|
+
- caterpillar_ruby.gemspec
|
|
73
|
+
- lib/caterpillar-ruby.rb
|
|
74
|
+
- lib/caterpillar-ruby/error.rb
|
|
75
|
+
- lib/caterpillar-ruby/version.rb
|
|
76
|
+
- lib/core_ext/blank.rb
|
|
77
|
+
- lib/core_ext/hash.rb
|
|
78
|
+
- lib/core_ext/inflectors.rb
|
|
79
|
+
- lib/core_ext/string.rb
|
|
80
|
+
- lib/generators/caterpillar/initializer.rb
|
|
81
|
+
- lib/generators/caterpillar/install_generator.rb
|
|
82
|
+
homepage: https://caterpillar.io
|
|
83
|
+
licenses:
|
|
84
|
+
- MIT
|
|
85
|
+
metadata: {}
|
|
86
|
+
post_install_message:
|
|
87
|
+
rdoc_options: []
|
|
88
|
+
require_paths:
|
|
89
|
+
- lib
|
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: '0'
|
|
100
|
+
requirements: []
|
|
101
|
+
rubyforge_project:
|
|
102
|
+
rubygems_version: 2.4.8
|
|
103
|
+
signing_key:
|
|
104
|
+
specification_version: 4
|
|
105
|
+
summary: A simple ruby client for the Caterpillar API
|
|
106
|
+
test_files: []
|