basecamp_agent.rb 3.0 KB

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