Permalink
Browse files

Automatically set up cosm triggers

  • Loading branch information...
Floppy committed May 12, 2012
1 parent 69b7fa5 commit 476cab8542891f95cc65b663329658756238a535
Showing with 23 additions and 0 deletions.
  1. +23 −0 app/models/feed.rb
View
@@ -1,3 +1,5 @@
+require 'net/http'
+
class Feed < ActiveRecord::Base
#has_amee_profile
@@ -9,4 +11,25 @@ class Feed < ActiveRecord::Base
attr_accessible :external_id, :energy_type, :current_value, :current_carbon
+ after_create :create_cosm_trigger
+
+ def create_cosm_trigger
+ # Prepare post data
+ env, stream = external_id.split(':')
+ post_data = {
+ :url => "http://socialmeter.floppy.org.uk/users/#{user.id}/feeds/#{id}/cosm_trigger",
+ :trigger_type => "change",
+ :environment_id => env,
+ :stream_id => stream
+ }
+ # Do post
+ uri = URI('http://api.cosm.com/v2/triggers/')
+ Net::HTTP.start(uri.host, uri.port) do |http|
+ request = Net::HTTP::Post.new uri.request_uri
+ request['X-ApiKey'] = COSM_API_KEY
+ request.body = post_data.to_json
+ http.request request
+ end
+ end
+
end

0 comments on commit 476cab8

Please sign in to comment.