Class: SelectPdfApi

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/select_pdf_api.rb', line 8

def config
  @config
end

#save_toObject

Returns the value of attribute save_to.



8
9
10
# File 'lib/select_pdf_api.rb', line 8

def save_to
  @save_to
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/select_pdf_api.rb', line 8

def url
  @url
end

Instance Method Details

#downloadObject



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

#paramsObject



41
42
43
44
45
46
# File 'lib/select_pdf_api.rb', line 41

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

#success?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/select_pdf_api.rb', line 48

def success?
  @success
end