lib/messenger/campfire.rb in messenger-0.2.0 vs lib/messenger/campfire.rb in messenger-0.3.0
- old
+ new
@@ -1,51 +1,47 @@
-require 'httparty'
+require 'typhoeus'
require 'json'
-module Messenger
+class Messenger::Campfire
- class Campfire
+ def self.valid_url?(url)
+ !!matcher(url)
+ rescue NoMethodError
+ false
+ end
- def self.valid_url?(url)
- !!matcher(url)
- rescue NoMethodError
- false
- end
+ # URL format:
+ # campfire://api-key:room-id@subdomain.campfirenow.com
+ def self.deliver(url, body, options={})
+ raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
+ ssl, api_key, room, subdomain = matcher(url)
+ options[:headers] ||= {}
+ response = Typhoeus::Request.post(
+ "http#{ssl ? "s" : ""}://#{api_key}:x@#{subdomain}.campfirenow.com/room/#{room}/speak.json",
+ :headers => { "Content-Type" => "application/json"}.merge(options[:headers]),
+ :body => { "message" => { "body" => body } }.to_json
+ )
+ Messenger::Result.new(success?(response), response)
+ end
- # URL format:
- # campfire://api-key:room-id@subdomain.campfirenow.com
- def self.send(url, body, options={})
- raise URLError, "The URL provided is invalid" unless valid_url?(url)
- ssl, api_key, room, subdomain = matcher(url)
- response = HTTParty.post(
- "http#{ssl ? "s" : ""}://#{subdomain}.campfirenow.com/room/#{room}/speak.json",
- :basic_auth => { :username => api_key, :password => "x" },
- :headers => { "Content-Type" => "application/json" },
- :body => { "message" => { "body" => body } }.to_json
- )
- Result.new(success?(response), response)
- end
+ def self.obfuscate(url)
+ raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url)
+ ssl, api_key, room, subdomain = matcher(url)
+ "campfire#{ssl ? "-ssl" : ""}://xxxx:#{room}@#{subdomain}.campfirenow.com"
+ end
- def self.obfuscate(url)
- raise URLError, "The URL provided is invalid" unless valid_url?(url)
- ssl, api_key, room, subdomain = matcher(url)
- "campfire#{ssl ? "-ssl" : ""}://xxxx:#{room}@#{subdomain}.campfirenow.com"
- end
+private
- private
+ def self.matcher(url)
+ url.match(/^campfire(-ssl)?:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,4]
+ end
- def self.matcher(url)
- url.match(/^campfire(-ssl)?:\/\/([^:]+):([^@]+)@([^\.]+).campfirenow.com/)[1,4]
+ def self.success?(response)
+ case response.code
+ when 200, 201: true
+ else
+ false
end
-
- def self.success?(response)
- case response.code
- when 200, 201: true
- else
- false
- end
- end
-
end
end