1
0

basecamp_agent.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. module Agents
  2. class BasecampAgent < Agent
  3. include FormConfigurable
  4. include Oauthable
  5. valid_oauth_providers :'37signals'
  6. cannot_receive_events!
  7. description <<-MD
  8. The BasecampAgent checks a Basecamp project for new Events
  9. To be able to use this Agent you need to authenticate with 37signals in the [Services](/services) section first.
  10. MD
  11. event_description <<-MD
  12. Events are the raw JSON provided by the Basecamp API. Should look something like:
  13. {
  14. "creator": {
  15. "fullsize_avatar_url": "https://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/original.gif?r=3",
  16. "avatar_url": "http://dge9rmgqjs8m1.cloudfront.net/global/dfsdfsdfdsf/avatar.gif?r=3",
  17. "name": "Dominik Sander",
  18. "id": 123456
  19. },
  20. "attachments": [],
  21. "raw_excerpt": "test test",
  22. "excerpt": "test test",
  23. "id": 6454342343,
  24. "created_at": "2014-04-17T10:25:31.000+02:00",
  25. "updated_at": "2014-04-17T10:25:31.000+02:00",
  26. "summary": "commented on whaat",
  27. "action": "commented on",
  28. "target": "whaat",
  29. "url": "https://basecamp.com/12456/api/v1/projects/76454545-explore-basecamp/messages/76454545-whaat.json",
  30. "html_url": "https://basecamp.com/12456/projects/76454545-explore-basecamp/messages/76454545-whaat#comment_76454545"
  31. }
  32. MD
  33. default_schedule "every_10m"
  34. def default_options
  35. {
  36. 'project_id' => '',
  37. }
  38. end
  39. form_configurable :project_id, roles: :completable
  40. def complete_project_id
  41. service.prepare_request
  42. response = HTTParty.get projects_url, request_options.merge(query_parameters)
  43. response.map { |p| {text: "#{p['name']} (#{p['id']})", id: p['id']}}
  44. end
  45. def validate_options
  46. errors.add(:base, "you need to specify the basecamp project id of which you want to receive events") unless options['project_id'].present?
  47. end
  48. def working?
  49. (events_count.present? && events_count > 0)
  50. end
  51. def check
  52. service.prepare_request
  53. response = HTTParty.get events_url, request_options.merge(query_parameters)
  54. events = JSON.parse(response.body)
  55. if !memory[:last_event].nil?
  56. events.each do |event|
  57. create_event :payload => event
  58. end
  59. end
  60. memory[:last_event] = events.first['created_at'] if events.length > 0
  61. save!
  62. end
  63. private
  64. def base_url
  65. "https://basecamp.com/#{URI.encode(service.options[:user_id].to_s)}/api/v1/"
  66. end
  67. def events_url
  68. base_url + "projects/#{URI.encode(interpolated[:project_id].to_s)}/events.json"
  69. end
  70. def projects_url
  71. base_url + "projects.json"
  72. end
  73. def request_options
  74. {:headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)", "Authorization" => "Bearer \"#{service.token}\""}}
  75. end
  76. def query_parameters
  77. memory[:last_event].present? ? { :query => {:since => memory[:last_event]} } : {}
  78. end
  79. end
  80. end