basecamp_agent.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. module Agents
  2. class BasecampAgent < Agent
  3. cannot_receive_events!
  4. include Oauthable
  5. valid_oauth_providers '37signals'
  6. description <<-MD
  7. The BasecampAgent checks a Basecamp project for new Events
  8. To be able to use this Agent you need to authenticate with 37signals in the [Services](/services) section first.
  9. You need to provide the `project_id` of the project you want to monitor.
  10. If you have your Basecamp project opened in your browser you can find the user_id and project_id as follows:
  11. `https://basecamp.com/123456/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. 'project_id' => '',
  41. }
  42. end
  43. def validate_options
  44. errors.add(:base, "you need to specify the basecamp project id of which you want to receive events") unless options['project_id'].present?
  45. end
  46. def working?
  47. (events_count.present? && events_count > 0)
  48. end
  49. def check
  50. self.service.prepare_request
  51. reponse = HTTParty.get request_url, request_options.merge(query_parameters)
  52. memory[:last_run] = Time.now.utc.iso8601
  53. if last_check_at != nil
  54. JSON.parse(reponse.body).each do |event|
  55. create_event :payload => event
  56. end
  57. end
  58. save!
  59. end
  60. private
  61. def request_url
  62. "https://basecamp.com/#{URI.encode(self.service.options[:user_id].to_s)}/api/v1/projects/#{URI.encode(interpolated[:project_id].to_s)}/events.json"
  63. end
  64. def request_options
  65. {:headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)", "Authorization" => "Bearer \"#{self.service.token}\""}}
  66. end
  67. def query_parameters
  68. memory[:last_run].present? ? { :query => {:since => memory[:last_run]} } : {}
  69. end
  70. end
  71. end