basecamp_agent.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. service.prepare_request
  51. reponse = HTTParty.get request_url, request_options.merge(query_parameters)
  52. events = JSON.parse(reponse.body)
  53. if !memory[:last_event].nil?
  54. events.each do |event|
  55. create_event :payload => event
  56. end
  57. end
  58. memory[:last_event] = events.first['created_at'] if events.length > 0
  59. save!
  60. end
  61. private
  62. def request_url
  63. "https://basecamp.com/#{URI.encode(service.options[:user_id].to_s)}/api/v1/projects/#{URI.encode(interpolated[:project_id].to_s)}/events.json"
  64. end
  65. def request_options
  66. {:headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)", "Authorization" => "Bearer \"#{service.token}\""}}
  67. end
  68. def query_parameters
  69. memory[:last_event].present? ? { :query => {:since => memory[:last_event]} } : {}
  70. end
  71. end
  72. end