Class: SelectPdfApi
- Inherits:
-
Object
- Object
- SelectPdfApi
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#save_to ⇒ String
File to save the output PDF.
-
#url ⇒ String
Url of the page to create PDF from.
Instance Method Summary collapse
-
#download ⇒ Object
Sends a request to the API endpoint and downloads the file to the file system.
-
#initialize(user_opts) ⇒ SelectPdfApi
constructor
A new instance of SelectPdfApi.
-
#params ⇒ Object
Sends a message to the Config Object being used and constructs the query string to be used by the download method.
-
#success? ⇒ Boolean
Returns weather the latest download request was successful.
Constructor Details
#initialize(user_opts) ⇒ SelectPdfApi
Returns a new instance of SelectPdfApi.
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/select_pdf_api.rb', line 13 def config @config end |
#save_to ⇒ String
Returns File to save the output PDF.
17 18 19 |
# File 'lib/select_pdf_api.rb', line 17 def save_to @save_to end |
#url ⇒ String
Returns 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
#download ⇒ Object
Sends a request to the API endpoint and downloads the file to the file system.
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 |
#params ⇒ Object
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..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.
66 67 68 |
# File 'lib/select_pdf_api.rb', line 66 def success? @success end |