Skip to content

Commit

Permalink
Changed API request to send JSON string as body
Browse files Browse the repository at this point in the history
* Discarded RestClient. Just Net::HTTP
  • Loading branch information
Max Williams committed Feb 11, 2010
1 parent 32892e5 commit 8a29dbe
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/pusher.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'rest_client'
require 'json'

class Pusher
Expand All @@ -18,27 +17,39 @@ def self.[](channel_id)

class Channel
def initialize(key, id)
@http = RestClient::Resource.new(
"http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}"
)
@uri = URI.parse("http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}")
@http = Net::HTTP.new(@uri.host, @uri.port)
end

def trigger(event_name, data)
begin
@http.post(:event => JSON.generate({
:event => event_name,
:data => data
}))
@http.post(
@uri.path,
self.class.turn_into_json({
:event => event_name,
:data => data
}),
{'Content-Type'=> 'application/json'}
)
rescue StandardError => e
handle_error e
end
end

def self.turn_into_json(data)
j = if Object.const_defined?('ActiveSupport')
data.to_json
else
JSON.generate(data)
end
j
end

private

def handle_error(e)
puts e.inspect
end
def handle_error(e)
puts e.inspect
end

end

Expand Down

0 comments on commit 8a29dbe

Please sign in to comment.