pdf_info_agent.rb 1.8 KB

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