Class: Marshmallow::Connection
- Inherits:
-
Object
- Object
- Marshmallow::Connection
- Defined in:
- lib/god/contacts/campfire.rb
Instance Method Summary collapse
- #base_url ⇒ Object
- #find_room_id_by_name(room) ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #speak(room, message) ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
20 21 22 23 24 |
# File 'lib/god/contacts/campfire.rb', line 20 def initialize() raise "Required option :subdomain not set." unless [:subdomain] raise "Required option :token not set." unless [:token] @options = end |
Instance Method Details
#base_url ⇒ Object
26 27 28 29 30 |
# File 'lib/god/contacts/campfire.rb', line 26 def base_url scheme = @options[:ssl] ? 'https' : 'http' subdomain = @options[:subdomain] "#{scheme}://#{subdomain}.campfirenow.com" end |
#find_room_id_by_name(room) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/god/contacts/campfire.rb', line 32 def find_room_id_by_name(room) url = URI.parse("#{base_url}/rooms.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if @options[:ssl] req = Net::HTTP::Get.new(url.path) req.basic_auth(@options[:token], 'X') res = http.request(req) case res when Net::HTTPSuccess rooms = JSON.parse(res.body) room = rooms['rooms'].select { |x| x['name'] == room } rooms.empty? ? nil : room.first['id'] else raise res.error! end end |
#speak(room, message) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/god/contacts/campfire.rb', line 52 def speak(room, ) room_id = find_room_id_by_name(room) raise "No such room: #{room}." unless room_id url = URI.parse("#{base_url}/room/#{room_id}/speak.json") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if @options[:ssl] req = Net::HTTP::Post.new(url.path) req.basic_auth(@options[:token], 'X') req.set_content_type('application/json') req.body = { 'message' => { 'body' => } }.to_json res = http.request(req) case res when Net::HTTPSuccess true else raise res.error! end end |