docmago_client 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+ require 'zip/zipfilesystem'
4
+
5
+ module DocmagoClient
6
+ class HTMLResourceArchiver
7
+ def initialize(html, base_path='.')
8
+ @html = html
9
+ @base_path = base_path
10
+ @doc = Nokogiri::HTML(@html)
11
+ end
12
+
13
+ def create_zip(file_path)
14
+ Zip::ZipFile.open(file_path, Zip::ZipFile::CREATE) do |zipfile|
15
+ zipfile.file.open("document.html", "w") { |f| f.write @html }
16
+
17
+ fetch_uris.each do |uri|
18
+ if File.exists?(resolve_uri(uri))
19
+ zipfile.file.open(normalize_uri(uri), "w") { |f| f.write(File.read(resolve_uri(uri))) }
20
+ end
21
+ end
22
+ end
23
+
24
+ return file_path
25
+ end
26
+
27
+ private
28
+ def fetch_uris
29
+ @doc.xpath("//link[@rel='stylesheet']/@href", "//img/@src")
30
+ end
31
+
32
+ def normalize_uri(uri)
33
+ uri = URI.parse(uri)
34
+ uri.query = nil
35
+ uri.to_s
36
+ end
37
+
38
+ def resolve_uri(uri)
39
+ File.join(File.expand_path(@base_path), normalize_uri(uri))
40
+ end
41
+ end
42
+ end
@@ -14,9 +14,11 @@ module DocmagoClient
14
14
 
15
15
  ActionController::Renderers.add :pdf do |filename, options|
16
16
  default_options = {
17
- :name => filename||controller_name,
18
- :test_mode => !Rails.env.production?,
19
- :base_uri => url_for(:only_path => false)
17
+ :name => filename||controller_name,
18
+ :test_mode => !Rails.env.production?,
19
+ :base_uri => url_for(:only_path => false),
20
+ :zip_resources => true,
21
+ :resource_path => Rails.root.join('public')
20
22
  }
21
23
  options = default_options.merge(options)
22
24
  options[:content] ||= render_to_string(options)
@@ -1,3 +1,3 @@
1
1
  module DocmagoClient
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,9 +1,10 @@
1
- require "httparty"
1
+ require "httmultiparty"
2
2
  require "tempfile"
3
3
 
4
4
  require 'docmago_client/version'
5
5
  require 'docmago_client/exception'
6
6
  require 'docmago_client/error'
7
+ require 'docmago_client/html_resource_archiver'
7
8
 
8
9
  if defined?(Rails)
9
10
  if Rails.respond_to?(:version) && Rails.version =~ /^3/
@@ -14,9 +15,9 @@ if defined?(Rails)
14
15
  end
15
16
 
16
17
  module DocmagoClient
17
- include HTTParty
18
+ include HTTMultiParty
18
19
 
19
- base_uri ENV["DOCMAGO_URL"] || "https://docmago.com/api/"
20
+ base_uri ENV["DOCMAGO_URL"] || "https://docmago.com/api"
20
21
 
21
22
  def self.base_uri(uri = nil)
22
23
  default_options[:base_uri] = uri ? uri : default_options[:base_uri] || ENV["DOCMAGO_URL"]
@@ -51,7 +52,19 @@ module DocmagoClient
51
52
  raise_exception_on_failure = options[:raise_exception_on_failure]
52
53
  options.delete :raise_exception_on_failure
53
54
 
54
- response = post("/documents", body: { document: options }, basic_auth: { username: api_key })
55
+ if options[:zip_resources]
56
+ tmp_dir = Dir.mktmpdir
57
+ begin
58
+ resource_archiver = HTMLResourceArchiver.new(options[:content], options[:resource_path])
59
+ options[:content] = File.new(resource_archiver.create_zip("#{tmp_dir}/document.zip"))
60
+
61
+ response = post("/documents", body: { document: options }, basic_auth: { username: api_key })
62
+ ensure
63
+ FileUtils.remove_entry_secure tmp_dir
64
+ end
65
+ else
66
+ response = post("/documents", body: { document: options }, basic_auth: { username: api_key })
67
+ end
55
68
 
56
69
  if raise_exception_on_failure && !response.success?
57
70
  raise DocmagoClient::Exception::DocumentCreationFailure.new response.body, response.code
@@ -1109,3 +1109,85 @@ Completed 200 OK in 83ms (Views: 82.4ms | ActiveRecord: 0.0ms)
1109
1109
   (0.1ms) rollback transaction
1110
1110
   (0.0ms) begin transaction
1111
1111
   (0.0ms) rollback transaction
1112
+ Connecting to database specified by database.yml
1113
+  (0.2ms) begin transaction
1114
+  (0.0ms) rollback transaction
1115
+  (0.0ms) begin transaction
1116
+
1117
+
1118
+ Started GET "/home" for 127.0.0.1 at 2012-08-07 01:59:30 +0200
1119
+ Processing by HomeController#index as HTML
1120
+ Rendered home/index.html.erb within layouts/application (2.1ms)
1121
+ Completed 200 OK in 12ms (Views: 11.7ms | ActiveRecord: 0.0ms)
1122
+
1123
+
1124
+ Started GET "/home.pdf" for 127.0.0.1 at 2012-08-07 01:59:30 +0200
1125
+ Processing by HomeController#index as PDF
1126
+ Rendered home/index.pdf.erb (0.2ms)
1127
+ Completed 500 Internal Server Error in 5ms
1128
+  (0.0ms) rollback transaction
1129
+  (0.0ms) begin transaction
1130
+  (0.0ms) rollback transaction
1131
+ Connecting to database specified by database.yml
1132
+  (0.2ms) begin transaction
1133
+  (0.0ms) rollback transaction
1134
+  (0.0ms) begin transaction
1135
+
1136
+
1137
+ Started GET "/home" for 127.0.0.1 at 2012-08-07 01:59:57 +0200
1138
+ Processing by HomeController#index as HTML
1139
+ Rendered home/index.html.erb within layouts/application (1.6ms)
1140
+ Completed 200 OK in 9ms (Views: 8.4ms | ActiveRecord: 0.0ms)
1141
+
1142
+
1143
+ Started GET "/home.pdf" for 127.0.0.1 at 2012-08-07 01:59:57 +0200
1144
+ Processing by HomeController#index as PDF
1145
+ Rendered home/index.pdf.erb (0.2ms)
1146
+ Rendered text template (0.0ms)
1147
+ Sent data contents.pdf (2.7ms)
1148
+ Completed 200 OK in 1529ms (Views: 1528.5ms | ActiveRecord: 0.0ms)
1149
+  (0.1ms) rollback transaction
1150
+  (0.0ms) begin transaction
1151
+  (0.0ms) rollback transaction
1152
+ Connecting to database specified by database.yml
1153
+  (0.2ms) begin transaction
1154
+  (0.0ms) rollback transaction
1155
+  (0.0ms) begin transaction
1156
+
1157
+
1158
+ Started GET "/home" for 127.0.0.1 at 2012-08-07 02:19:20 +0200
1159
+ Processing by HomeController#index as HTML
1160
+ Rendered home/index.html.erb within layouts/application (1.6ms)
1161
+ Completed 200 OK in 9ms (Views: 8.2ms | ActiveRecord: 0.0ms)
1162
+
1163
+
1164
+ Started GET "/home.pdf" for 127.0.0.1 at 2012-08-07 02:19:20 +0200
1165
+ Processing by HomeController#index as PDF
1166
+ Rendered home/index.pdf.erb (0.2ms)
1167
+ Rendered text template (0.0ms)
1168
+ Sent data contents.pdf (1.7ms)
1169
+ Completed 200 OK in 1342ms (Views: 1341.5ms | ActiveRecord: 0.0ms)
1170
+  (0.1ms) rollback transaction
1171
+  (0.0ms) begin transaction
1172
+  (0.0ms) rollback transaction
1173
+ Connecting to database specified by database.yml
1174
+  (0.2ms) begin transaction
1175
+  (0.0ms) rollback transaction
1176
+  (0.0ms) begin transaction
1177
+
1178
+
1179
+ Started GET "/home" for 127.0.0.1 at 2012-08-12 00:02:14 +0200
1180
+ Processing by HomeController#index as HTML
1181
+ Rendered home/index.html.erb within layouts/application (2.4ms)
1182
+ Completed 200 OK in 12ms (Views: 11.8ms | ActiveRecord: 0.0ms)
1183
+
1184
+
1185
+ Started GET "/home.pdf" for 127.0.0.1 at 2012-08-12 00:02:14 +0200
1186
+ Processing by HomeController#index as PDF
1187
+ Rendered home/index.pdf.erb (0.3ms)
1188
+ Rendered text template (0.0ms)
1189
+ Sent data contents.pdf (2.0ms)
1190
+ Completed 200 OK in 1547ms (Views: 1546.6ms | ActiveRecord: 0.0ms)
1191
+  (0.1ms) rollback transaction
1192
+  (0.0ms) begin transaction
1193
+  (0.0ms) rollback transaction
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docmago_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
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: 2012-08-04 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -27,6 +27,54 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.7.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: httmultiparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.3.8
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.8
46
+ - !ruby/object:Gem::Dependency
47
+ name: nokogiri
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.5.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.5
62
+ - !ruby/object:Gem::Dependency
63
+ name: rubyzip
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.9
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.9
30
78
  - !ruby/object:Gem::Dependency
31
79
  name: rails
32
80
  requirement: !ruby/object:Gem::Requirement
@@ -84,6 +132,7 @@ extra_rdoc_files: []
84
132
  files:
85
133
  - lib/docmago_client/error.rb
86
134
  - lib/docmago_client/exception.rb
135
+ - lib/docmago_client/html_resource_archiver.rb
87
136
  - lib/docmago_client/railtie.rb
88
137
  - lib/docmago_client/version.rb
89
138
  - lib/docmago_client.rb
@@ -142,12 +191,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
191
  - - ! '>='
143
192
  - !ruby/object:Gem::Version
144
193
  version: '0'
194
+ segments:
195
+ - 0
196
+ hash: 4003460714454216066
145
197
  required_rubygems_version: !ruby/object:Gem::Requirement
146
198
  none: false
147
199
  requirements:
148
200
  - - ! '>='
149
201
  - !ruby/object:Gem::Version
150
202
  version: '0'
203
+ segments:
204
+ - 0
205
+ hash: 4003460714454216066
151
206
  requirements: []
152
207
  rubyforge_project:
153
208
  rubygems_version: 1.8.23