pdf_info_agent.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. require 'open-uri'
  2. require 'hypdf'
  3. module Agents
  4. class PdfInfoAgent < Agent
  5. gem_dependency_check { defined?(HyPDF) }
  6. cannot_be_scheduled!
  7. description <<-MD
  8. The PDF Info Agent returns the metadata contained within a given PDF file, using HyPDF.
  9. #{'## Include the `hypdf` gem in your `Gemfile` to use PDFInfo Agents.' if dependencies_missing?}
  10. In order for this agent to work, you need to have [HyPDF](https://devcenter.heroku.com/articles/hypdf) running and configured.
  11. It works by acting on events that contain a key `url` in their payload, and runs the [pdfinfo](https://devcenter.heroku.com/articles/hypdf#pdfinfo) command on them.
  12. MD
  13. event_description <<-MD
  14. This will change based on the metadata in the pdf.
  15. { "Title"=>"Everyday Rails Testing with RSpec",
  16. "Author"=>"Aaron Sumner",
  17. "Creator"=>"LaTeX with hyperref package",
  18. "Producer"=>"xdvipdfmx (0.7.8)",
  19. "CreationDate"=>"Fri Aug 2 05",
  20. "32"=>"50 2013",
  21. "Tagged"=>"no",
  22. "Pages"=>"150",
  23. "Encrypted"=>"no",
  24. "Page size"=>"612 x 792 pts (letter)",
  25. "Optimized"=>"no",
  26. "PDF version"=>"1.5",
  27. "url": "your url"
  28. }
  29. MD
  30. def working?
  31. !recent_error_logs?
  32. end
  33. def default_options
  34. {}
  35. end
  36. def receive(incoming_events)
  37. incoming_events.each do |event|
  38. interpolate_with(event) do
  39. url_to_scrape = event.payload['url']
  40. check_url(url_to_scrape, event.payload) if url_to_scrape =~ /^https?:\/\//i
  41. end
  42. end
  43. end
  44. def check_url(in_url, payload)
  45. return unless in_url.present?
  46. Array(in_url).each do |url|
  47. log "Fetching #{url}"
  48. info = HyPDF.pdfinfo(open(url))
  49. create_event :payload => info.merge(payload)
  50. end
  51. end
  52. end
  53. end