Class: SelectPdfApi
- Inherits:
-
Object
- Object
- SelectPdfApi
- Defined in:
- lib/select_pdf_api.rb,
lib/select_pdf_api/version.rb,
lib/select_pdf_api/exceptions.rb,
lib/select_pdf_api/yaml_file_config.rb
Defined Under Namespace
Classes: ConfigError, DownloadError, YamlFileConfig
Constant Summary collapse
- API_URL =
'http://selectpdf.com/api'- DEFAULT_OPTIONS =
TODO: Refactor this…
{ config_file: "select-pdf-config.yml", save_to: "document.pdf" }
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#save_to ⇒ Object
Returns the value of attribute save_to.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #download ⇒ Object
-
#initialize(user_opts = {}) ⇒ SelectPdfApi
constructor
A new instance of SelectPdfApi.
- #params ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(user_opts = {}) ⇒ SelectPdfApi
Returns a new instance of SelectPdfApi.
18 19 20 21 22 23 24 25 |
# File 'lib/select_pdf_api.rb', line 18 def initialize(user_opts={}) opts = DEFAULT_OPTIONS.merge user_opts @url = opts[:url] @config = opts[:config] || SelectPdfApi::YamlFileConfig.new(opts[:config_file]) @save_to = opts[:save_to] @success = false end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/select_pdf_api.rb', line 8 def config @config end |
#save_to ⇒ Object
Returns the value of attribute save_to.
8 9 10 |
# File 'lib/select_pdf_api.rb', line 8 def save_to @save_to end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/select_pdf_api.rb', line 8 def url @url end |
Instance Method Details
#download ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/select_pdf_api.rb', line 27 def download raise SelectPdfApi::DownloadError, "A URL must be specified." if @url.nil? request = "#{API_URL}/?#{params}" response = HTTParty.get request if response.success? File.open(@save_to, "wb") {|f| f.write response.parsed_response} @success = true else raise SelectPdfApi::DownloadError, "There was an error with the following request #{request}" end end |
#params ⇒ Object
41 42 43 44 45 46 |
# File 'lib/select_pdf_api.rb', line 41 def params result = [] @config..sort.map { |name, value| result << "#{name}=#{value}" } result << "url=#{@url}" result.join('&') end |
#success? ⇒ Boolean
48 49 50 |
# File 'lib/select_pdf_api.rb', line 48 def success? @success end |