pdfinfo 1.3.2 → 1.3.4

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZjFkYmExNjAxODg3ODMyM2VhNWU1ZDE1MTYyN2ZkNDYwODg1MTdlMA==
4
+ OWE5OGUxYjJmMWRjYjZhM2FjZjdlN2RiNDU0YWI0MGMyYzhhZGM2OQ==
5
5
  data.tar.gz: !binary |-
6
- MWE5NWIzMDZhZDE3YWQzYzRlMDYwMmQ4OTE4YjRlZjQwYzc3YjI4YQ==
6
+ NThkMTcwNjNiNDkxZGY1NTIwMWZiNDlhZjMyMTZkNWNhY2Y3Njg5Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTM2MDMzYThjYjlkY2MzZWYyNjYwNGY5M2YyN2MyM2VjYmQxMTVhYWE5Y2U0
10
- MjQ0ZTY3YjZmZGUxNjJjMDc2NWYzYjFiYTVmMjAyODA2YWI3Y2RlZmM3YjNk
11
- YTJlMjQ1YTMxYTQ3Y2I0MWYzMzFmMTI4MWJmZDcxYzBmZmVkNmU=
9
+ N2JmMGQ2MjcwNzA4NjU2YTdiODIwNTg5YjI0NDY0YzI3NDkzYjU1ZmVjMThh
10
+ NGFiNmY5YjliYzlhZGEwNGQ3MjgyNjhjZTY1ZmVmYTAyODVhN2Q0MWRlMjg0
11
+ NTA0YTg2ZjZjZDg0MTVlYWU2ZmU2Njk1NGE5M2ZiNjA1MWMzMDg=
12
12
  data.tar.gz: !binary |-
13
- NzAxZjU2MTUwMjg1ODQwZGE1ZDFiZDUwZGM5MTNkYWFiM2Y4NWVhNGZlMjk2
14
- YzkwMDIxYzY5NjdjNTlmOTExM2RkY2QzMzlmZGVlNDRhNTlhYjA3YWUwMjRl
15
- OWU1ZTg1YzA5ZjcyYTI2N2QwZDIzZmQ2YWNhYzk2ZGQyZmUzYmU=
13
+ YzNiYTUzMDc0NmRkNTY4ZTI4MTY0ZjgwMWEwNWJhOWRhMTBhMWZkODdhZWEz
14
+ ZTU0MDlhOWZiODJjZjc4ODI5MTE5ODVlM2EwMGFmZDYwMDNjMzM1MGI1MDIw
15
+ NGQxOGMzMmM4NzhhYWY4MmFhOTI5MTgyOWFiMjAzMWFkN2I2Y2I=
@@ -2,11 +2,10 @@ require 'open3'
2
2
  require 'shellwords'
3
3
  require 'date'
4
4
  require 'time'
5
- %w(object_to_hash errors page).each {|f| require File.expand_path("../pdfinfo/#{f}", __FILE__)}
5
+ Dir[File.expand_path("../pdfinfo/*", __FILE__)].each {|f| require f }
6
6
 
7
7
  class Pdfinfo
8
- include ObjectToHash
9
-
8
+ include ToHash
10
9
  attr_reader :pages, :title, :subject, :keywords, :author, :creator,
11
10
  :creation_date, :modified_date, :usage_rights, :producer,
12
11
  :form, :page_count, :width, :height, :file_size, :pdf_version
@@ -29,10 +28,10 @@ class Pdfinfo
29
28
  end
30
29
 
31
30
  def initialize(source_path, opts = {})
32
- @pages = []
33
31
 
34
32
  info_hash = parse_shell_response(exec(source_path, opts))
35
33
 
34
+ @pages = []
36
35
  info_hash.delete_if do |key, value|
37
36
  @pages << Page.from_string(value) if key.match(/Page\s+\d+\ssize/)
38
37
  end
@@ -45,12 +44,12 @@ class Pdfinfo
45
44
  @creator = presence(info_hash.delete('Creator'))
46
45
  @producer = presence(info_hash.delete('Producer'))
47
46
  @tagged = !!(info_hash.delete('Tagged') =~ /yes/)
47
+ @optimized = !!(info_hash.delete('Optimized') =~ /yes/)
48
48
  @encrypted = !!(encrypted_val =~ /yes/)
49
49
  @page_count = info_hash.delete('Pages').to_i
50
50
  @file_size = info_hash.delete('File size').to_i
51
51
  @form = info_hash.delete('Form')
52
52
  @pdf_version = info_hash.delete('PDF version')
53
- @optimized = !!(info_hash.delete('Optimized') =~ /yes/)
54
53
  @keywords = (info_hash.delete('Keywords') || '').split(/\s/)
55
54
  @creation_date = parse_time(info_hash.delete('CreationDate'))
56
55
  @modified_date = parse_time(info_hash.delete('ModDate'))
@@ -64,39 +63,32 @@ class Pdfinfo
64
63
  ur[:change] = booleanize_usage_right.call('change')
65
64
  ur[:add_notes] = booleanize_usage_right.call('addNotes')
66
65
  end
67
-
68
- # temporarily continue setting #width and #height on Pdfinfo object
69
- # to maintain legacy behavior
70
- @width = @pages[0].width
71
- @height = @pages[0].height
72
66
  end
73
67
 
74
- # @return [Boolean]
75
- def tagged?
76
- @tagged
68
+ %w(width height).each do |attr|
69
+ define_method(attr) { @pages[0].send(attr) }
77
70
  end
78
71
 
79
- # @return [Boolean]
80
- def encrypted?
81
- @encrypted
82
- end
83
-
84
- # @return [Boolean]
85
- def optimized?
86
- @optimized
72
+ # Feature checks
73
+ %w(tagged encrypted optimized).each do |flag|
74
+ define_method("#{flag}?") { instance_variable_get("@#{flag}")}
87
75
  end
88
76
 
77
+ # Usage rights checks
89
78
  %w(print copy change).each do |ur|
90
- # @return [Boolean]
91
79
  define_method("#{ur}able?") { @usage_rights[ur.to_sym] }
92
80
  end
93
81
  alias modifiable? changeable?
94
82
 
95
- # @return [Boolean]
96
83
  def annotatable?
97
84
  @usage_rights[:add_notes]
98
85
  end
99
86
 
87
+ def to_hash
88
+ super.tap {|h| h[:pages].map!(&:to_hash) }
89
+ end
90
+ alias_method :to_h, :to_hash
91
+
100
92
  private
101
93
  # executes pdfinfo command with supplied options
102
94
  # @param [String,Pathname] file_path
@@ -145,8 +137,9 @@ class Pdfinfo
145
137
  str.encode!(Encoding::UTF_8)
146
138
  end
147
139
 
140
+ # @return [NilClass,String] returns nil if string is empty
148
141
  def presence(val)
149
- (val.nil? || val.empty? ) ? nil : val
142
+ (val.nil? || val.empty?) ? nil : val
150
143
  end
151
144
 
152
145
  def parse_shell_response(response_str)
@@ -1,6 +1,8 @@
1
+ require File.expand_path("../to_hash", __FILE__)
2
+
1
3
  class Pdfinfo
2
4
  class Page
3
- include ObjectToHash
5
+ include Pdfinfo::ToHash
4
6
  MATCHER = /(?<=\s)?(\d+(?:\.\d+)?)(?=\s)/
5
7
 
6
8
  attr_reader :width, :height, :rotation
@@ -18,5 +20,9 @@ class Pdfinfo
18
20
  def rotated?
19
21
  0 != @rotation
20
22
  end
23
+
24
+ # def to_hash
25
+ # {width: width, height: height, rotation: rotation}
26
+ # end
21
27
  end
22
28
  end
@@ -0,0 +1,7 @@
1
+ class Pdfinfo
2
+ module ToHash
3
+ def to_hash
4
+ instance_variables.inject({}) { |h, var| h[var[1..-1].to_sym] = instance_variable_get(var); h }
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  class Pdfinfo
2
- VERSION = '1.3.2'
2
+ VERSION = '1.3.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdfinfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Venegas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-19 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Simple ruby wrapper around the pdfinfo executable
55
+ description: Object oriented ruby wrapper around the pdfinfo
56
56
  email: rvenegas2@gmail.com
57
57
  executables: []
58
58
  extensions: []
@@ -60,8 +60,8 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - lib/pdfinfo.rb
62
62
  - lib/pdfinfo/errors.rb
63
- - lib/pdfinfo/object_to_hash.rb
64
63
  - lib/pdfinfo/page.rb
64
+ - lib/pdfinfo/to_hash.rb
65
65
  - lib/pdfinfo/version.rb
66
66
  homepage: https://github.com/RyanV/pdfinfo
67
67
  licenses:
@@ -86,5 +86,5 @@ rubyforge_project:
86
86
  rubygems_version: 2.2.2
87
87
  signing_key:
88
88
  specification_version: 4
89
- summary: Simple ruby wrapper around the pdfinfo executable
89
+ summary: Simple ruby wrapper around the pdfinfo command line utility
90
90
  test_files: []
@@ -1,19 +0,0 @@
1
- class Pdfinfo
2
- module ObjectToHash
3
- def as_json
4
- instance_variables.inject({}) do |hash, var|
5
- val = instance_variable_get(var)
6
- hash[var[1..-1].to_sym] = (val.respond_to?(:as_json) ? val.as_json : val)
7
- hash
8
- end
9
- end
10
- alias to_hash as_json
11
- alias to_h as_json
12
- end
13
- end
14
-
15
- class Array
16
- def as_json
17
- map {|v| v.respond_to?(:as_json) ? v.as_json : v }
18
- end unless method_defined?(:as_json)
19
- end