basecamp_agent.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. module Agents
  2. class BasecampAgent < Agent
  3. cannot_receive_events!
  4. description <<-MD
  5. The BasecampAgent checks a Basecamp project for new Events
  6. It is required that you enter your Basecamp credentials (`username` and `password`).
  7. You also need to provide your Basecamp `user_id` and the `project_id` of the project you want to monitor.
  8. If you have your Basecamp project opened in your browser you can find the user_id and project_id as follows:
  9. `https://basecamp.com/`
  10. user_id
  11. `/projects/`
  12. project_id
  13. `-explore-basecamp`
  14. MD
  15. event_description <<-MD
  16. Events are the raw JSON provided by the Basecamp API. Should look something like:
  17. {
  18. "creator": {
  19. "fullsize_avatar_url": "https://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/original.gif?r=3",
  20. "avatar_url": "http://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/avatar.gif?r=3",
  21. "name": "Dominik Sander",
  22. "id": 123456
  23. },
  24. "attachments": [],
  25. "raw_excerpt": "test test",
  26. "excerpt": "test test",
  27. "id": 6454342343,
  28. "created_at": "2014-04-17T10:25:31.000+02:00",
  29. "updated_at": "2014-04-17T10:25:31.000+02:00",
  30. "summary": "commented on whaat",
  31. "action": "commented on",
  32. "target": "whaat",
  33. "url": "https://basecamp.com/12456/api/v1/projects/76454545-explore-basecamp/messages/76454545-whaat.json",
  34. "html_url": "https://basecamp.com/12456/projects/76454545-explore-basecamp/messages/76454545-whaat#comment_76454545"
  35. }
  36. MD
  37. default_schedule "every_10m"
  38. def default_options
  39. {
  40. 'username' => '',
  41. 'password' => '',
  42. 'user_id' => '',
  43. 'project_id' => '',
  44. }
  45. end
  46. def validate_options
  47. errors.add(:base, "you need to specify your basecamp username") unless options['username'].present?
  48. errors.add(:base, "you need to specify your basecamp password") unless options['password'].present?
  49. errors.add(:base, "you need to specify your basecamp user id") unless options['user_id'].present?
  50. errors.add(:base, "you need to specify the basecamp project id of which you want to receive events") unless options['project_id'].present?
  51. end
  52. def working?
  53. (events_count.present? && events_count > 0)
  54. end
  55. def check
  56. reponse = HTTParty.get request_url, request_options.merge(query_parameters)
  57. memory[:last_run] = Time.now.utc.iso8601
  58. if last_check_at != nil
  59. JSON.parse(reponse.body).each do |event|
  60. create_event :payload => event
  61. end
  62. end
  63. save!
  64. end
  65. private
  66. def request_url
  67. "https://basecamp.com/#{URI.encode(interpolated[:user_id].to_s)}/api/v1/projects/#{URI.encode(interpolated[:project_id].to_s)}/events.json"
  68. end
  69. def request_options
  70. {:basic_auth => {:username => interpolated[:username], :password => interpolated[:password]}, :headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}}
  71. end
  72. def query_parameters
  73. memory[:last_run].present? ? { :query => {:since => memory[:last_run]} } : {}
  74. end
  75. end
  76. end