Class: UrlDataModel

Inherits:
ApplicationRecord show all
Defined in:
app/models/url_data_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ UrlDataModel


Name: initialize

Purpose: To provide a constructor for the class.

Precondition: url_data_model has been instantiated.

Postcondition: url_data_model has been initialized.

-----

Calls: N/A

Called By: class

-----

Additional Comments:

Programmer: ND Guthrie Date: 20190305



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/url_data_model.rb', line 34

def initialize(attributes = {})

	# if(attributes[:url] != nil)
	# 	@url 			= attributes[:url]
	# end

	# if(attributes[:pdf_version] != nil)
	# 	@pdf_version 	= attributes[:pdf_version]
	# end

	# if(attributes[:producer] != nil)		
	# 	@producer 		= attributes[:producer]
	# end
		
	# if(attributes[:title] != nil)
	# 	@title 			= attributes[:title]
	# end
		
	# if(attributes[:metadata] != nil)
	# 	@metadata 		= attributes[:metadata]
	# end
		
	# if(attributes[:page_count] != nil)
	# 	@page_count 	= attributes[:page_count]
	# end

	buildModelFromURLViaPDF
end

Instance Attribute Details

#idObject

Returns the value of attribute id


4
5
6
# File 'app/models/url_data_model.rb', line 4

def id
  @id
end

#metadataObject

Returns the value of attribute metadata


4
5
6
# File 'app/models/url_data_model.rb', line 4

def 
  @metadata
end

#page_countObject

Returns the value of attribute page_count


4
5
6
# File 'app/models/url_data_model.rb', line 4

def page_count
  @page_count
end

#pdf_versionObject

Returns the value of attribute pdf_version


4
5
6
# File 'app/models/url_data_model.rb', line 4

def pdf_version
  @pdf_version
end

#producerObject

Returns the value of attribute producer


4
5
6
# File 'app/models/url_data_model.rb', line 4

def producer
  @producer
end

#titleObject

Returns the value of attribute title


4
5
6
# File 'app/models/url_data_model.rb', line 4

def title
  @title
end

#urlObject

Returns the value of attribute url


4
5
6
# File 'app/models/url_data_model.rb', line 4

def url
  @url
end

Instance Method Details

#after_initializeObject


Name: after_initialize

Purpose: To provide a constructor for the class.

Precondition: An instance of UrlDataModel, url_data_model, exists.

Postcondition: url_data_model is populated by buildModelFromURLViaPDF

-----

Calls: buildModelFromURLViaPDF

Called By: class

-----

Additional Comments:

Programmer: ND Guthrie Date: 20190305



93
94
95
96
97
# File 'app/models/url_data_model.rb', line 93

def after_initialize()

	# buildModelFromURLViaPDF

end

#buildModelFromURLViaPDFObject


Name: buildModelFromURLViaPDF

Purpose: To provide a control architecture for the functionality required to convert an HTML file located at a given URL to a PDF.

Precondition: HTML file located at given URL exists.

Postcondition: HTML file is converted to PDF.

-----

Calls: convertURLToPDF() readPDFData()

Called By: printable_controller.rb

-----

Additional Comments: This imethod is for the UrlDataModel controller based on the DocRaptor example at docraptor.com/blog/adding-pdf-generation-to-your-rails-app/

Programmer: ND Guthrie Date: 20190305



131
132
133
134
135
136
137
# File 'app/models/url_data_model.rb', line 131

def buildModelFromURLViaPDF

	convertURLToPDF()
	readPDFData()
	deletePDF()
	
end

#convertURLToPDFObject


Name: convertURLToPDF()

Purpose: To convert a URL to PDF

Precondition: URL exists

Postcondition: PDF converted from the given URL exists and is located in ./storage/PDFs.

-----

Calls: N/A

Called By: buildModelFromURLViaPDF

-----

Additional Comments: This is a modified version of the async.rb file

								used for asynchronous HTML to PDF conversion.

Error logging has been added, and the converted file is stored in ./storage/PDFs.

Programmer: ND Guthrie Date: 20190304



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/models/url_data_model.rb', line 171

def convertURLToPDF()

	require "bundler/setup"
	Bundler.require

	DocRaptor.configure do |dr|

	  dr.username  = "YOUR_API_KEY_HERE" # this key works for test documents
	  # dr.debugging = true

	end

	$docraptor = DocRaptor::DocApi.new

	begin

		logPathName 		= "./storage/Logs/standardOutput/output.txt"
		errorLogPathName	= "./storage/Logs/Error/"
		pathName 			= "./storage/PDFs/"
		# url 				= "http://docraptor.com/examples/invoice.html"
		url 				= "http://www.docraptor.com"
		@url = url

		fileNamePDF 		= "docraptor-ruby.pdf"


	  	create_response = $docraptor.create_async_doc(
		    test:             		true,                               # test documents are free but watermarked
		    document_url:   		url, 								# or use a url
		    name:             		fileNamePDF,                        # help you find a document later
		    document_type:    		"pdf",                              # pdf or xls or xlsx
	  	)

	  	loop do
		    status_response = $docraptor.get_async_doc_status(create_response.status_id)

		    #puts "doc status: #{status_response.status}"
		    
		    case status_response.status
		    
		    when "completed"
		      	doc_response = $docraptor.get_async_doc(status_response.download_id)
		      	File.open("./storage/PDFs/docraptor-ruby.pdf", "wb") do |file|
        			file.write(doc_response)
				end
		      	#puts "Wrote PDF to " + pathName + fileNamePDF

		      break
		    
		    when "failed"

		      	#puts "FAILED"
		      	#puts status_response
		      	break

		    else

		      	sleep 1

	    end
	 
	end

	rescue DocRaptor::ApiError => error
	  	
	    #puts "#{error.class}: #{error.message}"
	    #puts error.code          # HTTP response code
	    #puts error.response_body # HTTP response body
	    #puts error.backtrace[0..3].join("\n")
	end		
	
end

#deletePDFObject


Name: deletePDF()

Purpose: To delete created PDF after reading

Precondition: PDF exists in ./storage/PDFs/docraptor-ruby.pdf

Postcondition: ./storage/PDFs/ is empty.

-----

Calls: N/A

Called By: index

-----

Additional Comments: This function utilizes the pdfreader gem as per design specification requirements. It extracts the

Programmer: ND Guthrie Date: 20190305



322
323
324
325
326
327
328
# File 'app/models/url_data_model.rb', line 322

def deletePDF
	
	require 'fileutils'

	FileUtils.rm_rf('./storage/PDFs/docraptor-ruby.pdf')

end

#readPDFDataObject


Name: readPDFData()

Purpose: To read the data from the newly created PDF

Precondition: PDF exists

Postcondition: PDF converted from the given URL exists and is located in ./storage/PDFs.

-----

Calls: N/A

Called By: buildModelFromURLViaPDF

-----

Additional Comments: This function utilizes the pdfreader gem as per design specification requirements. It extracts the pdf_version, metadata, and page_count, provided they exist.

Programmer: ND Guthrie Date: 20190304



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/models/url_data_model.rb', line 275

def readPDFData

	require 'rubygems'
	require 'pdf/reader'

	fileName = "./storage/PDFs/docraptor-ruby.pdf"

	PDF::Reader.open(fileName) do |reader|
		
		@pdf_version 	= reader.pdf_version
		# @producer 		= reader.producer
		# @title 			= reader.title
		@metadata 		= reader.
		@page_count 	= reader.page_count

	end
	
end