Class: SelectPdfApi

Inherits:
Object
  • Object
show all
Defined in:
lib/select_pdf_api.rb,
lib/select_pdf_api/version.rb,
lib/select_pdf_api/env_config.rb,
lib/select_pdf_api/exceptions.rb,
lib/select_pdf_api/yaml_config.rb

Overview

Main Select PDF API class.

Defined Under Namespace

Classes: ConfigError, DownloadError, EnvConfig, YamlConfig

Constant Summary collapse

API_END_POINT =

Base API URL

'http://selectpdf.com/api'
VERSION =

Version of library.

"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_opts) ⇒ SelectPdfApi

Returns a new instance of SelectPdfApi.

Parameters:

  • opts (Hash)

    custom options provided by the user.

  • user_opts (Hash)

    a customizable set of options

Options Hash (user_opts):

  • url (String)

    the url to create PDF from.

  • config (Class)

    allows you to provide your own config class.

  • save_to (String)

    the file the output PDF file will be saved to.



31
32
33
34
35
36
# File 'lib/select_pdf_api.rb', line 31

def initialize(opts={})
	@url 		 = opts[:url]
	@config  = opts[:config] || SelectPdfApi::YamlConfig.new
	@save_to = opts[:save_to] || "document.pdf"
	@success = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/select_pdf_api.rb', line 13

def config
  @config
end

#save_toString

Returns File to save the output PDF.

Returns:

  • (String)

    File to save the output PDF.



17
18
19
# File 'lib/select_pdf_api.rb', line 17

def save_to
  @save_to
end

#urlString

Returns Url of the page to create PDF from.

Returns:

  • (String)

    Url of the page to create PDF from.



21
22
23
# File 'lib/select_pdf_api.rb', line 21

def url
  @url
end

Instance Method Details

#downloadObject

Sends a request to the API endpoint and downloads the file to the file system.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/select_pdf_api.rb', line 41

def download
	@success = false
	raise SelectPdfApi::DownloadError, "A URL must be specified." if @url.nil?

	request  = "#{API_END_POINT}/?#{params}"
	response = HTTParty.get request

	if response.success?
		save_pdf(response.parsed_response)
	else
		raise SelectPdfApi::DownloadError, "There was an error with the following request #{request}"
	end
end

#paramsObject

Sends a message to the Config Object being used and constructs the query string to be used by the download method.



57
58
59
60
61
62
# File 'lib/select_pdf_api.rb', line 57

def params
	result = []
	@config.options.sort.map { |name, value| result << "#{name}=#{value}" unless value.empty?	}
	result << "url=#{@url}"
	result.join('&')
end

#success?Boolean

Returns weather the latest download request was successful.

Returns:

  • (Boolean)

    returns weather the latest download request was successful.



66
67
68
# File 'lib/select_pdf_api.rb', line 66

def success?
	@success
end