lib/economic/pdf.rb in economic-rest-0.5.0 vs lib/economic/pdf.rb in economic-rest-0.5.1

- old
+ new

@@ -1,5 +1,20 @@ module Economic class Pdf < Base field :self + field :download + + def download_pdf(path: nil) + raise StandardError, "There is no download url set" if download.nil? + + response = Economic::BaseRepo.send_request(method: :get, url: download) + + return response.body if path.nil? + + dirname = File.dirname(path) + unless File.directory?(dirname) + FileUtils.mkdir_p(dirname) + end + File.open(path, "wb") { |f| f.puts response.body } + end end end